簡體   English   中英

通過 ID 區分數組 object

[英]Differentiate the array object by their ID

這是 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"},
]

我希望數組看起來像這樣:

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"
     }
    ]
  },
]

這就像數組必須根據它的 id 進行破壞,並且類似 id 的對象將被寫入名為“info”的數組中。 請給我解決方案。 謝謝

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)

您可以簡單地對其進行排序:

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}
}

應該進行細微的調整

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM