-3

I am new to programming world. I would like to know how can I do the following. array of objects

const arrOfObj = [{ id: "id1", name: "A1", rollno: "1"}, {id: "id2", name: "A2", rollno: "2"}, { id: "id3", name: "A3", rollno: "3"}]

another object list

const obj = {"id1": "absent", "id2": "present"}

create a new array of obj by mapping through "obj" and map through "arrOfObj" and check if "id" matches then create

const newArrOfObj = [{ id: "id1", name: "A1", attendance: "absent"}, {id: "id2", name: "A2", attendance: "present"}]

not sure how to do

const newArrOfObj = Object.entries(obj).map()

1 Answers1

0

You can use Array.prototype.map() method to create a new Array of objects based on the mapping between the arrOfObj and obj.

Example:

const arrOfObj = [
 { id: "id1", name: "A1", rollno: "1"},
 {id: "id2", name: "A2", rollno: "2"}
];

const obj = {"id1": "absent", "id2": "present"};

const newArrOfObj = arrOfObj.map(item => {
  return {
    id: item.id,
    name: item.name,
    attendance: obj[item.id]
  };
});

console.log(newArrOfObj);
Filip Huhta
  • 2,043
  • 7
  • 25