计算数组中对象中两个键的出现次数(count occurrences of two keys in objects in array)
我有以下数组与对象,并使用以下代码创建一个键“id”的计数器:
var arr=[ { id: 123, title: "name1", status: "FAILED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 1234, title: "name2", status: "FAILED" }, { id: 1234, title: "name2", status: "PASSED" } ]; const test =arr.reduce((tally, item) => { if (!tally[item.id]) { tally[item.id] = 1; } else { tally[item.id] = tally[item.id] + 1; } return tally; }, {}); console.log(test);
现在我想要做的是修改计数以考虑关键状态,所以结果将是这样的:
[ {id:123, status:"PASSED", tally:3}, {id:123, status:"FAILED", tally:1}, {id:1234, status:"PASSED", tally:1}, {id:1234, status:"FAILED", tally:1} ]
任何想法? 谢谢!
I have the following array with objects and used the following code to creating a tally with the key "id":
var arr=[ { id: 123, title: "name1", status: "FAILED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 123, title: "name1", status: "PASSED" }, { id: 1234, title: "name2", status: "FAILED" }, { id: 1234, title: "name2", status: "PASSED" } ]; const test =arr.reduce((tally, item) => { if (!tally[item.id]) { tally[item.id] = 1; } else { tally[item.id] = tally[item.id] + 1; } return tally; }, {}); console.log(test);
Now what I want to do is to modify the tally to take in consideration the key status as well so the result will be somthing like:
[ {id:123, status:"PASSED", tally:3}, {id:123, status:"FAILED", tally:1}, {id:1234, status:"PASSED", tally:1}, {id:1234, status:"FAILED", tally:1} ]
Any idea? Thanks!
原文:https://stackoverflow.com/questions/51153174
更新时间:2023-12-18 15:12
最满意答案
如何放置以下代码片段
... System.out.print("Water is a " + returnMessage + " at" + temperature + " degrees.");
printTemp
方法作为最后一行? 然后在main
输出中什么也没有,你只需写:... printTemp(input.nextDouble());
What about to put the following code snippet
... System.out.print("Water is a " + returnMessage + " at" + temperature + " degrees.");
to the
printTemp
method as the last line? Then in themain
outputs nothing and you just write:... printTemp(input.nextDouble());
相关问答
更多-
java在void中做什么?(What does void do in java?)[2022-05-25]
如果没有void ,代码将无法工作的原因是因为System.out.println(String string)方法不会返回任何内容,只会将提供的参数打印到标准输出终端(在大多数情况下是计算机监视器)。 当一个方法返回“Nothing”时,你必须通过在其签名中加入void关键字来指定。 你可以在这里看到System.out.println的文档: http://download.oracle.com/javase/6/docs/api/java/io/PrintStream.html#println%28j ... -
如果你的方法没有返回值,不要使用Function接口。 改用Consumer
。 public Consumer runner = Platform::runLater; 它represents an operation that accepts a single input argument and returns no result. If your method has no return value, don't use the Function inte ... -
在这一点上它只是退出了这个方法。 一旦执行return ,代码的其余部分将不被执行。 例如。 public void test(int n) { if (n == 1) { return; } else if (n == 2) { doStuff(); return; } doOtherStuff(); } 请注意,编译器足够聪明,可以告诉您无法达到一些代码: if (n == 3) { return; ...
-
如何在Java8中指定void(不是Void)方法的函数类型?(How to specify function types for void (not Void) methods in Java8?)[2019-11-21]
您正在尝试使用错误的界面类型。 在这种情况下, Function类型不合适,因为它接收到一个参数并具有返回值。 相反,您应该使用Consumer (以前称为Block) 函数类型被声明为 interface Function{ R apply(T t); } 但是,消费者类型与您正在寻找的兼容: interface Consumer { void accept(T t); } 因此,消费者与接收T并且不返回任何东西(void)的方法兼容。 这就是你想要的。 例如,如果我想显示 ... -
如果由于某种原因你不能在你的例子中显示Foo.test()摘要(例如因为Foo扩展了一个具体的类)并且你确定它永远不会被调用,抛出运行时异常可能更好没有合理的默认值: protected Integer test() { throw new UnsupportedOperationException("Calling test on Foo is not supported"); } Java核心API中有这样的示例,例如,参见UnsupportedOperationException - 这个有 ...
-
问题 :不必要的转换为int 。 解决方案 :不要不必要地将它们转换为int 。 double principalLoan = Double.parseDouble(tempTextField3.getText()); double loanInterest = Double.parseDouble(tempTextField1.getText()); double loanTerm = Double.parseDouble(tempTextField2.getText( ...
-
如何放置以下代码片段 ... System.out.print("Water is a " + returnMessage + " at" + temperature + " degrees."); printTemp方法作为最后一行? 然后在main输出中什么也没有,你只需写: ... printTemp(input.nextDouble()); What about to put the following code snippet ... System.out.print("Water is a ...
-
看起来像这是一个强制使用java 8流的情况。 相反,你可以用forEach来实现它。 List
objList = ... objList.forEach(e -> transform(e, e.getUuid())); return objList; Seems like this is a case of forced usage of java 8 stream. Instead you can achieve it with forEach. List o ... -
如何调用函数
.apply()?(How to call Function [2022-05-30].apply()?) Void 实例相当矛盾。 尝试传递null 。 如果你没有得到NullPointerException那么这可能是正确的事情。 (如果有的话,请阅读文档)。 A Void instance is rather contradictory. Try passing null. If you don't get a NullPointerException then that's probably the correct thing to do. (Else read the documentation if ... -
Java Void to Scala Unit(Java Void to Scala Unit)[2021-11-08]
Scala的Unit就像Java的void原语,但它并不完全相同。 Scala的Unit类型和Java的Void类型完全不相关。 存在Java的Void类,因此您可以将其用作类似于原始void的类型参数,就像Integer作为原始int的类似物一样存在。 在Scala中, Unit履行了Void和void的角色。 Unit和Void之间的隐式转换不可能存在,因为隐式转换适用于实例,而不适用于类型。 此外,Java的Void类型不能有实例。 (Scala的Unit确实有一个实例:( () 。) 长话短说,Sc ...