Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 创建具有相同密钥的对象数组的步骤_Javascript_Arrays_Object - Fatal编程技术网

Javascript 创建具有相同密钥的对象数组的步骤

Javascript 创建具有相同密钥的对象数组的步骤,javascript,arrays,object,Javascript,Arrays,Object,我需要通过组合具有相同类型的对象来获得一个数组-添加它们的权重-- const arr = [ {type: "a", weight: 10}, {type: "b", weight: 15}, {type: "a", weight: 20},] 预期结果 [ {type: "a", weight: 30}, {type: "b", weight: 15},] 给你!如果您需要解

我需要通过组合具有相同类型的对象来获得一个数组-添加它们的权重--

const arr = [ {type: "a", weight: 10}, {type: "b", weight: 15}, {type: "a", weight: 20},]
    
预期结果

  [ {type: "a", weight: 30}, {type: "b", weight: 15},]

给你!如果您需要解释,请进行注释,因为代码仅用于循环

const arr=[{type:“a”,权重:10},{type:“b”,权重:15},{type:“a”,权重:20},]
for(设i=0;iconsole.log(arr)
请首先提供您尝试过的内容及其结果的示例。SO旨在帮助您解决代码中遇到的错误和困难,以便能够澄清和解释概念,而不是为您工作。您可以通过使用数组方法
array.reduce()
来实现这一点。如果你遇到问题,你自己试试,然后回来。这是你期望的答案。。无论如何,对于您的代码,您只需要修改键名。。这里的工作示例是:这是否回答了您的问题?这是一个重复的问题,已经有多个解决方案可用,因此请仅标记。此外,不需要嵌套循环。