首页 \ 问答 \ 计算数组中对象中两个键的出现次数(count occurrences of two keys in objects in array)

计算数组中对象中两个键的出现次数(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 the main outputs nothing and you just write:

...
printTemp(input.nextDouble());

相关问答

更多
  • 如果没有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; ...
  • 您正在尝试使用错误的界面类型。 在这种情况下, 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 ...
  • 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 ...
  • Scala的Unit就像Java的void原语,但它并不完全相同。 Scala的Unit类型和Java的Void类型完全不相关。 存在Java的Void类,因此您可以将其用作类似于原始void的类型参数,就像Integer作为原始int的类似物一样存在。 在Scala中, Unit履行了Void和void的角色。 Unit和Void之间的隐式转换不可能存在,因为隐式转换适用于实例,而不适用于类型。 此外,Java的Void类型不能有实例。 (Scala的Unit确实有一个实例:( () 。) 长话短说,Sc ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)