[英]Best way to combine data of object/array to an attribute in JavaScript
We have an array of objects with the attributes "description" and "id".我们有一组具有属性“description”和“id”的对象。
foo[0].id // "45g-332"
foo[0].id2 // "45G-000332"
foo[0].description // "tomatoes"
foo[1].id // "45f-842"
foo[1].id2 // "45F-000842"
foo[1].description // "cherries"
foo[2].id // "45g-332"
foo[2].id2 // "45G-000332"
foo[2].description // "peaches"
I need a variable or object in which all descriptions for the same id are combined to have a result like this:我需要一个变量或 object,其中对同一 id 的所有描述都组合在一起以获得如下结果:
bar[0].id // "45g-332"
bar[0].id2 // "45G-000332"
bar[0].description // "tomatoes; peaches"
bar[1].id // "45f-842"
bar[1].id2 // "45F-000842"
bar[1].description // "cherries"
… or with associative labels …或带有关联标签
bar["45g-332"].description // "tomatoes; peaches"
bar["45f-842"].description // "cherries"
bar["45g-332"].id2 // "45G-000332"
The only passably slim solution I came up with is (→ jsFiddle ):我想出的唯一还过得去的苗条解决方案是(→ jsFiddle ):
let foo = [];
foo[0] = [];
foo[1] = [];
foo[2] = [];
foo[0].id = "45g-332";
foo[0].id2 = "45G-000332";
foo[0].description = "tomatoes";
foo[1].id = "45f-842";
foo[1].id2 = "45F-000842";
foo[1].description = "cherries";
foo[2].id = "45g-332";
foo[2].id2 = "45G-000332";
foo[2].description = "peaches";
let bar = [];
for (let i in foo) { // Loop through source (foo)
if (bar[foo[i].id]) { // Check if sink (bar) with id already exists
bar[foo[i].id].description += "; " + foo[i].description; // Add description to existing bar element
} else {
bar[foo[i].id] = []; // Create new bar element
bar[foo[i].id].description = foo[i].description;
};
bar[foo[i].id].id2 = foo[i].id2; // Added by edit
};
for (let i in bar) {
console.log("id: " + i + " has: " + bar[i].description + " and id2: " + bar[i].id2);
};
// Result is
// "id: 45g-332 has: tomatoes; peaches and id2: 45G-000332"
// "id: 45f-842 has: cherries and id2: 45F-000842"
I'm pretty sure that there's a more plain way to do this.我很确定有一种更简单的方法可以做到这一点。 What's gold standard?什么是黄金标准?
You can use reduce你可以使用减少
const foo = [{ id: '45g-332', id2: '45G-000332', description: 'tomatoes' }, { id: '45f-842', id2: '45F-000842', description: 'cherries' }, { id: '45g-332', id2: '45G-000332', description: 'peaches' }, { id: '45g-332', id2: '45G-000332', description: 'x' } ] const bar = foo.reduce((acc, { id, id2, description }) => { if (acc[id]) { acc[id].id2.push(id2) acc[id].description.push(description) } else { acc[id] = { id2: [id2], description: [description] } } return acc }, {}) console.log(bar)
For your expected result 2 here is a simple method对于您的预期结果 2,这里有一个简单的方法
const foo = [ {id: "45g-332", description: "tomatoes"}, {id: "45f-842", description: "cherries"}, {id: "45g-332", description: "peaches"} ] const bar = {} foo.forEach(d => { bar[d.id] = bar[d.id]? bar[d.id] + "; " + d. description: d.description }); console.log(bar)
For your expected result 1. you need to convert it back to an array对于您的预期结果 1. 您需要将其转换回数组
const foo = [ {id: "45g-332", description: "tomatoes"}, {id: "45f-842", description: "cherries"}, {id: "45g-332", description: "peaches"} ] const bar = {} foo.forEach(d => { bar[d.id] = bar[d.id]? bar[d.id] + "; " + d. description: d.description }); const data = Object.keys(bar).map(key => ({id:key, description: bar[key]})) console.log(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.