在JavaScript数组中组合重复键的值
我有一个包含一些键/值的数组,其中一个值是一个数组,我希望将数组中所有记录的键相同的数组的值组合在一起 下面是一个简单的例子来演示,我无法构建它的逻辑,因此寻求帮助来构建它的逻辑在JavaScript数组中组合重复键的值,javascript,arrays,object,Javascript,Arrays,Object,我有一个包含一些键/值的数组,其中一个值是一个数组,我希望将数组中所有记录的键相同的数组的值组合在一起 下面是一个简单的例子来演示,我无法构建它的逻辑,因此寻求帮助来构建它的逻辑 [{"somekey":"Some Value Pushed"},{"somekey":"Second Value"}] 我想要这样的结果 [{"somekey":["Some Value Pushed&qu
[{"somekey":"Some Value Pushed"},{"somekey":"Second Value"}]
我想要这样的结果
[{"somekey":["Some Value Pushed","Second Value"]}]
您可以尝试以下方法:
var array = [{
name: "foo1",
value: "val1"
}, {
name: "foo1",
value: ["val2", "val3"]
}, {
name: "foo2",
value: "val4"
}];
var output = [];
array.forEach(function(item) {
var existing = output.filter(function(v, i) {
return v.name === item.name;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
output[existingIndex].value = output[existingIndex].value.concat(item.value);
} else {
if (typeof item.value === 'string')
item.value = [item.value];
output.push(item);
}
});
或者,使用
可能是这样的:
常数数据=[
{somekey:somevalue-Pushed},
{somekey:Second值,otherkey:1},
{otherkey:2}
];
const merge_和_group=obj1,obj2=>
Object.entriesobj2.reduce
acc,[键,值]=>{
acc[键]??=[];
acc[key].pushval;
返回acc;
},
obj1
;
const res=data.u和_组,{};
console.logres
常数arr=[{
somekey:somevalue
}, {
somekey2:第二个值2
}, {
somekey:somevalue
}, {
somekey2:第二个值3
}]
常量newarr={}
arr.forEachobj=>{
对于Object.entriesobj的常量[key,value]{
如果newarr[key]newarr[key].pushvalue
else newarr[键]=[值]
}
}
console.lognewarrArray.prototype.reduce是一个可能的选项
reduce方法执行reducer函数,该函数作为数组中每个元素的输入提供,并返回单个输出值
常量数组=[{somekey:Some-Value-push},{somekey:Second-Value}];
常数res=array.reduceac,el=>{
常量[key,value]=Object.entriesel[0];
acc[键]| | acc[键]=[]推送值;
返回acc;
}, {};
logres假设数组中的每个元素都是具有单个键的对象 常量数组=[ {somekey:somevalue-Pushed}, {somekey:Second Value}, {foo:bar}, {foo:baz}, {somekey:Third Value}, ]; 常量结果=[]; array.forEachel=>{ let[key,value]=Object.entriesel[0]; 如果输入el,则让el显示结果{ el[key].pushvalue; 回来 } result.push{[key]:[value]}; }; console.dirresult JavaScript中Array对象的reduce函数可以将任意数组合并成单个对象。 我写了一行代码来解决这个问题。 我用数组更新了结果 常数arr=[{ somekey:somevalue推送, }, { somekey2:第二个值2, }, { somekey:somevalue推送, }, { somekey2:第二个值3, }, { somekey3:, }, {}, ]; 常数ansObj=arr.reduce prv,cur=>{ Object.entriescur.forEach[key,v]=>prv中的键?prv[key].pushv:prv[key]=[v]; 返回prv; }, {} const ansArray=Object.entriesansObj.map[key,value]=>{[key]:value};
console.logansArray 如果数组中只有somekey作为键,则可以使用以下映射方法: 常量数组=[{somekey:Some-Value-push},{somekey:Second-Value}]; const valuesArray=array.mapobj=>obj.somekey; 结果=[{somekey:valuesArray}];
console.logresult如果您的数组有其他键和somekey,并且您希望分离仅与somekey对应的值,请尝试以下操作: 常量数组=[{somekey:Some-Value-push},{somekey:Second-Value},{otherkey:other-Value}]; 常量filteredArray=array.filterobj=>{ 返回obj中的somekey }, []; 常量值array=filteredArray.mapobj=>obj.somekey; 结果=[{somekey:valuesArray}];
console.logresultdo这能回答您的问题吗?由于缺乏明确性和对给出的答案缺乏回应而被否决。唯一被提升的答案甚至没有按要求返回数组。请向数组和结果中添加更多具有不同键的对象,以便理解您的要求。如果还有其他键和somekey,请在使用Map之前使用filter,但不按要求返回数组。@MikeM请检查更新的代码。
function mergeNames (arr) {
return _.chain(arr).groupBy('name').mapValues(function (v) {
return _.chain(v).pluck('value').flattenDeep();
}).value();
}