所以实例我有以下功能:
function merge(arr, key) {
return arr.reduce((r, o) => {
if (r[o.key]) {
r[o.key].real += o.real;
} else {
r[o.key] = { key: o.key, real: o.real };
}
return r;
}, {});
}
我想要使用它10次,但每次{ key: o.key, real: o.real }
对于例如{ key: o.key, valueX: o.valueX }
等都不同。我怎样才能将此对象生成为函数参数?所以,如果我要运行
merge(arr, key, ['key, real']) // object assign will happen to key and real
我有点困惑
案例1:
var arrayOne = [{ someKey: '22', value: 345 }, { someKey: '22', value: 543 }]
function merge(arr, key) {
return arr.reduce((r, o) => {
if (r[o.key]) {
r[o.key].value += o.value;
} else {
r[o.key] = { key: o.someKey, value: o.value };
}
return r;
}, {});
}
console.log(merge(arrayOne, 'someKey'))

案例2:
var arrayTwo = [{ someKeyY: '22', valueZ: 345 }, { someKeyY: '22', valueZ: 543 }]
function merge(arr, key) {
return arr.reduce((r, o) => {
if (r[o.key]) {
r[o.key].valueZ += o.valueZ;
} else {
r[o.key] = { key: o.someKeyY, valueZ: o.valueZ };
}
return r;
}, {});
}
console.log(merge(arrayTwo, 'someKeyY'))

我想要通用功能,而不是一遍又一遍地编写相同的功能,只改变赋值对象......
答案 0 :(得分:0)
合并所有:
var arrayTwo = [{ someKeyY: '22', valueZ: 345 }, { someKeyY: '22', valueZ: 543 }]
function merge(arr) {
return arr.reduce((obj, newobj) => {
for(key in newobj){
obj[key]=obj[key]||0;
obj[key]+=newobj[key];
}
return obj;
},{});
}
console.log(merge(arrayTwo))
合并某些值:
function merge(arr,keys) {
return arr.reduce((obj, newobj) => {
for(key in keys){
obj[key]=obj[key]||0;
if(newobj[key]){//prevent crashing
obj[key]+=newobj[key];
}
}
return obj;
},{});
}
console.log(merge(arrayTwo,["someKeyY","valueZ"]))
答案 1 :(得分:0)
您也可以将value
作为参数传递给泛型函数,它可以返回实际的合并数组而不是地图。
function merge(arr, key, value) {
var result = arr.reduce((r, o) => {
if (r[o[key]]) { //key = someKey, o[key] = 22
r[o[key]][value] += o[value];
} else {
r[o[key]] = o;
}
return r;
}, {});
return Object.values(result);
}
var arrayOne = [{ someKey: '22', value: 345 }, { someKey: '22', value: 543 }, { someKey: '23', value: 666 }];
console.log(merge(arrayOne, 'someKey', 'value'));
var arrayTwo = [{ someKeyY: '22', valueZ: 345 }, { someKeyY: '22', valueZ: 543 }, { someKeyY: '23', valueZ: 666 }];
console.log(merge(arrayTwo, 'someKeyY', 'valueZ'));
答案 2 :(得分:0)
根据我的理解,您希望传递以下参数
function (arrayToMerge, mergeKey, sumKey, objKeyVal)
您可以使用纯JavaScript算法并执行以下操作:
var arr = [{ categoryID: 'A', timestamp: '1', name: 'Name', real: 10 }, { categoryID: 'B', timestamp: '1', name: 'Name', real: 10 }, { categoryID: 'B', timestamp: '1', name: 'Name', real: 10 }]
function mergeCats(categories) {
return categories.reduce((r, o) => {
if (r[o.categoryID]) {
r[o.categoryID].real += o.real;
} else {
r[o.categoryID] = { timestamp: o.timestamp, real: o.real, categoryID: o.categoryID, name: o.name };
}
return r;
}, {});
}
console.log('merge', mergeCats(arr))
function masterMerger(a, b, c, d) {
return a.reduce((r, o) => {
if (r[o[b]]) {
r[o[b]][c] += o[c];
} else {
const obj = {};
for (let i = 0; i < d.length; i += 1) {
obj[d[i]] = o[d[i]]
}
r[o[b]] = obj;
}
return r;
}, {})
}
console.log('merge 2', masterMerger(arr, 'categoryID', 'real', ['timestamp', 'real', 'categoryID', 'name']))
然后只使用masterMerger
masterMerget(arrayToMerge, mergeKey, sumKey, objKeyVal)