How to find and replace value in array of objects with JavaScript

1 Answer

0 votes
const arr = [
    {
        id: 1,
        s: 'javascript'
    },
    {
        id: 2,
        s: 'php'
    },
    {
        id: 3,
        s: 'c'
    },
    {
        id: 4,
        s: 'c++'
    },
];

const index = arr.findIndex((e) => e.id === 3);

arr[index] = {
    id: 12,
    s: 'java'
};

console.log(arr); 





/*
run:

[{
  id: 1,
  s: "javascript"
}, {
  id: 2,
  s: "php"
}, {
  id: 12,
  s: "java"
}, {
  id: 4,
  s: "c++"
}]

*/

 



answered Feb 14, 2021 by avibootz

Related questions

...