Home > OS >  Differentiate the array object by their ID
Differentiate the array object by their ID

Time:09-10

This is the array of javascript objects.

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'. Kindly give me the solution to it. Thanks

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

You could use recude method to reformat the array:

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);

CodePudding user response:

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);

  • Related