This is my array
const arr = [
{ id: "1", hello: "hello1" },
{ id: "2", hello: "hello2" },
];
I want this code
const finalarr = [
{ id: "1", hello: "hello1", bye: 'bye1' },
{ id: "2", hello: "hello2", bye: 'bye2' },
];
This is my array
const arr = [
{ id: "1", hello: "hello1" },
{ id: "2", hello: "hello2" },
];
I want this code
const finalarr = [
{ id: "1", hello: "hello1", bye: 'bye1' },
{ id: "2", hello: "hello2", bye: 'bye2' },
];
Try with map:
var arr = [{ id: "1", hello: "hello1" },{ id: "2", hello: "hello2" }];
result = arr.map(elem=>({...elem, 'bye':`bye${elem.id}`})); // with attaching the id
result2 = arr.map((elem,i)=>({...elem, 'bye':`bye${i+1}`})); // by generating id;
console.log(result);
console.log(result2);