[英]Differentiate the array object by their ID
This is the array of javascript objects.这是 javascript 对象的数组。
array= [
{id: "e60ff4a3", name: "Alex", work: "constructor"},
{id: "fbd59f52", name: "Morgan", work: "engineer"},
{id: "e60ff4a3", name: "Ana", work: "teacher"},
{id: "715f0686", name: "Smith", work: "doctor"},
{id: "715f0686", name: "David", work: "keeper"},
{id: "fbd59f52", name: "Emma", work: "nurse"},
]
I want the array will be seen like this:我希望数组看起来像这样:
array= [
{
id: "e60ff4a3",
info: [
{
name: "Alex",
work: "constructor",
},
{
name: "Ana",
work: "teacher",
}
]
},
{
id: "fbd59f52",
info: [
{
name: "Morgan",
work: "engineer",
},
{
name: "Emma",
work: "nurse"
}
]
},
{
id: "715f0686",
info: [
{
name: "Smith",
work: "doctor",
},
{
name: "David",
work: "keeper"
}
]
},
]
It's like the array has to break according to its id and a similar id's objects will be written into an array named 'info'.这就像数组必须根据它的 id 进行破坏,并且类似 id 的对象将被写入名为“info”的数组中。 Kindly give me the solution to it.请给我解决方案。 Thanks谢谢
let arr = [
{id: "e60ff4a3", name: "Alex", work: "constructor"},
{id: "fbd59f52", name: "Morgan", work: "engineer"},
{id: "e60ff4a3", name: "Ana", work: "teacher"},
{id: "715f0686", name: "Smith", work: "doctor"},
{id: "715f0686", name: "David", work: "keeper"},
{id: "fbd59f52", name: "Emma", work: "nurse"},
];
let output = Object.entries(arr.reduce((acc, elem) => {
let obj = {name: elem.name, work: elem.work};
if(elem.id in acc) {
acc[elem.id].push(obj)
} else {
acc[elem.id] = [obj]
}
return acc;
}, {})).map(a => ({id: a[0], info: a[1]}));
console.log(output)
You could simply sort it:您可以简单地对其进行排序:
const sortedArray = array.sort((a, b) => a.id - b.id);
const groupedArray = [];
const encounterdIds = [];
for(let i = 0; i < sortedArray.length - 1; i++) {
if (!groupedArray.includes(sortedArray[i].id)) {
encounterdIds.push(sortedArray[i].id); // this pushes the first instance of the id
const newItem = {
id: sortedArray[i].id,
info: [{ name: sortedArray[i].name, work: sortedArray[i].work }]
}
groupedArray.push(newItem); // pushes the object
continue;
}
// find() to find object in array
// push new {name: xxx, work: xxx}
}
Should work with minor tweaks应该进行细微的调整
You could use recude
method to reformat the array:您可以使用recude
方法重新格式化数组:
const array= [ {id: "e60ff4a3", name: "Alex", work: "constructor"}, {id: "fbd59f52", name: "Morgan", work: "engineer"}, {id: "e60ff4a3", name: "Ana", work: "teacher"}, {id: "715f0686", name: "Smith", work: "doctor"}, {id: "715f0686", name: "David", work: "keeper"}, {id: "fbd59f52", name: "Emma", work: "nurse"}, ] const sortedArray = array.reduce((result, item) => { let existItem = result.find(existItem => existItem.id === item.id); let target = existItem; if (:existItem) { target = { id. item,id: info; [] }. result;push(target). } target.info:push({ name. item,name: work. item;work }) return result, }. []) console;log(sortedArray);
const list = [ {id: "e60ff4a3", name: "Alex", work: "constructor"}, {id: "fbd59f52", name: "Morgan", work: "engineer"}, {id: "e60ff4a3", name: "Ana", work: "teacher"}, {id: "715f0686", name: "Smith", work: "doctor"}, {id: "715f0686", name: "David", work: "keeper"}, {id: "fbd59f52", name: "Emma", work: "nurse"}, ]; const itemsById = {}; for (let item of list) { const id = item.id; itemsById[id] = itemsById[id] || []; itemsById[id].push(item); } const listOfGroups = []; for (let id in itemsById) { const items = itemsById[id]; const itemsWithoutIds = items.map(item => { delete item.id; return item; }); listOfGroups.push({ id, info: itemsWithoutIds }); } console.log(listOfGroups);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.