Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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_Jquery_Arrays_Object_Ecmascript 6 - Fatal编程技术网

Javascript 对象从数组而不是硬代码赋值

Javascript 对象从数组而不是硬代码赋值,javascript,jquery,arrays,object,ecmascript-6,Javascript,Jquery,Arrays,Object,Ecmascript 6,我正在尝试按多个属性对数据进行分组并求和它们的值 以下是我根据 我跟进了这个问题: const arr=[{“shape”:“square”,“color”:“red”,“used”:1,“instances”:1},{“shape”:“color”:“red”,“used”:2,“instances”:1},{“shape”:“circle”,“color”:“color”:“used”:0,“instances”:0},{“shape”:“color”:“blue”,“used”:4,“i

我正在尝试按多个属性对数据进行分组并求和它们的值

以下是我根据

我跟进了这个问题:

const arr=[{“shape”:“square”,“color”:“red”,“used”:1,“instances”:1},{“shape”:“color”:“red”,“used”:2,“instances”:1},{“shape”:“circle”,“color”:“color”:“used”:0,“instances”:0},{“shape”:“color”:“blue”,“used”:4,“instances”:4},{“shape”:“color”,“color”:“red,“实例”:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:5},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:2,“实例”:1}];
const result=[…arr.reduce((r,o)=>{
常量键=o.shape+'-'+o.color;
const item=r.get(key)| | Object.assign({},o{
使用:0,
实例:0
});
使用的物品+=未使用的物品;
item.instances+=o.instances;
返回r.set(键、项);
},新映射)。值();
console.log(结果);

如果
映射
对象
具有
键,则循环通过
totalKeys
并使用当前对象的数据增加累加器中的对象。如果该对象是新的
,则将该对象的副本添加到映射中

if (r.has(key)) {
  const item = r.get(key)
  totalKeys.forEach(k => item[k] += o[k])
} else {
  r.set(key, { ...o })
}
下面是一个片段:

const arr=[{“形状”:“正方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“蓝色”,“使用过”:0,“实例”:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:4},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,”实例:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:5},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:2,“实例”:1}];
函数组和(数组、totalKeys){
常量组=arr.reduce((r,o)=>{
常量键=o.shape+'-'+o.color;
如果(r.has(钥匙)){
const item=r.get(键)
forEach(k=>item[k]+=o[k])
}否则{
r、 集合(键,{…o})
}
返回r;
},新地图);
返回数组.from(group.values())
}
console.log(
groupSum(arr,['used','instances']))
)

也许这是一种方法:

const arr=[{“形状”:“正方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“正方形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“蓝色”,“使用过”:0,“实例”:0},{“形状”:“正方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:4},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,“实例”:1},{“形状”:“圆形”,“颜色”:“红色”,“使用过”:1,”实例:0},{“形状”:“方形”,“颜色”:“蓝色”,“使用过”:4,“实例”:5},{“形状”:“方形”,“颜色”:“红色”,“使用过”:2,“实例”:1}],
nums=[“已使用”、“实例”]
函数求和(ar,cnts){//cnts:这些属性的合计计数
const grp=Object.keys(ar[0]).filter(k=>cnts.indexOf(k){
常量k=grp.map(g=>c[g]).join(“|”);
if(a[k])cnts.forEach(p=>a[k][p]+=c[p])
否则a[k]={……c};
归还
},{}))
}
常数res=总和(arr,nums);

console.log(res);
您也可以通过不使用array.reduce()与map()的组合来大大简化数据集,而只需通过使用array.forEach()循环原始数组的所有元素来构建新数组

我添加了您对gee数组的使用,作为您想要添加的数字字段列表…包括确保它们存在于结果数组的每个对象上…无论它们是否存在于arr中以前的每个对象上

const arr=[{
“形状”:“正方形”,
“颜色”:“红色”,
“已用”:1,
“实例”:1
}, {
“形状”:“正方形”,
“颜色”:“红色”,
“已用”:2,
“实例”:1
}, {
“形状”:“圆形”,
“颜色”:“蓝色”,
“已使用”:0,
“实例”:0
}, {
“形状”:“正方形”,
“颜色”:“蓝色”,
“已使用”:4,
“实例”:4
}, {
“形状”:“圆形”,
“颜色”:“红色”,
“已用”:1,
“实例”:1
}, {
“形状”:“圆形”,
“颜色”:“红色”,
“已用”:1,
“实例”:0,
“testProp”:1
}, {
“形状”:“正方形”,
“颜色”:“蓝色”,
“已使用”:4,
“实例”:5
}, {
“形状”:“正方形”,
“颜色”:“红色”,
“已用”:2,
“实例”:1
}];
让gee=['used','instances','testProp'];
让结果=[];
arr.forEach((o)=>{
//设置TempSource,因为并非所有o都可能在gee中包含所有元素
设tempSource={};
gee.forEach((键)=>{
如果(o.hasOwnProperty(键)){
tempSource[key]=o[key];
}否则{
tempSource[键]=0;
}
});
//查看结果数组是否已具有相同形状/颜色的对象
const matchingObject=result.find(元素=>{
让returnValue=true;
returnValue&=(element.shape==o.shape);
returnValue&=(element.color==o.color);
返回值;
});
if(匹配对象){
//匹配的对象已存在…因此增加值
gee.forEach((键)=>{
匹配对象[key]+=tempSource[key];
});
}否则{
//缺少匹配对象,因此合并新对象并插入
设newObj={};
赋值对象(newObj、o、tempSource);
结果:推送(newObj);
}
});
console.log(结果);

它们有一个要求和的属性数组。
让gee=['used','instances']
。他们不想硬编码,比如
t.used+=c.used
啊-谢谢你让我知道。那么,我如何识别求和的属性?!你可以发送一个属性数组进行求和。循环数组并动态获取属性。你甚至可以使用一个属性数组按
[“形状”、“颜色”]分组
。检查我答案中的片段