no se pueden cambiar los elementos según la identificación y el resultado esperado debe estar en formato de salida
const items = [ { id: 1, value: "first" }, { id: 2, value: "second" }, { id: 3, value: "third" } ]; const expectedOutput = [ { id: 1, value: "first" }, { id: 2, value: "newvalue" }, { id: 3, value: "third" } ] function getData(value, id) { return items.map((_each)=> { if(_each.id === id) { //need to update items with id=2 } }) } console.log(getData("newvalue", 2)) let inputArr = { "data": [{ "id": 1, "value": "first", "row": "A" }, { "id": 2, "value": "second", "row": "A" }, { "id": 3, "value": "Third", "row": "B" }, { "id": 4, "value": "Fourth", "row": "B" } ] } function format(inputArr) { let arr = [] let obj = {}; inputArr.data.forEach(el => { obj = { ...obj, [el.row]: [...(obj[el.row] || []) , el.value], } }); arr.push(obj); return arr; } let outputArr = format(inputArr) console.log(outputArr) let expectedOutput = [{ "A": ["first", "second"] }, { "B": ["Third", "Fourth"] }]@chidananda, la devolución de llamada del mapa debería devolver el elemento actualizado. ¡Una modificación menor a su código funcionaría!
const items = [ { id: 1, value: "first" }, { id: 2, value: "second" }, { id: 3, value: "third" } ]; const expectedOutput = [ { id: 1, value: "first" }, { id: 2, value: "newvalue" }, { id: 3, value: "third" } ] function getData(value, id) { return items.map((_each)=> { if(_each.id === id) { _each.value = value; } return _each; // Return the modified item }) } console.log(getData("newvalue", 2))