Home > Software engineering >  How to change value in array with objects in JS
How to change value in array with objects in JS

Time:10-24

I have an array and want to change name in object { id: 4, name: 'name4' } to 'name6'

const example = [
    {
      id: '1234',
      desc: 'sample1',
      items: [
        { id: 1, name: 'name1' },
        { id: 2, name: 'testItem2' }
       ]
      },    
    {
        id: '3456',
        desc: 'sample2',
      items: [
         { id: 4, name: 'name4' },
         { id: 5, name: 'testItem5' }
       ]
    },

I try in this way but it isn't working

const name = 'name4';
const result = example?.forEach((group) =>
      group.items.forEach((item) =>        
         if (item.name === name) {
           return item.name === 'name6';
         }
         return null;
      })
    );

CodePudding user response:

forEach doesn't return any value.

Instead of return item.name === 'name6' you can simply set new value to item.name.

CodePudding user response:

You could either change the value by simply assigning a new value.

example[1].items[0].name = 'name6'

But you can also iterate through all items and search for the name you want to change. I created a function that goes through an array and loops over its nested items arrays searching for any given name (targetName) and replacing it with a new one (newName):

function changeName(array, targetName, newName) {
  // Loop through the elements of array
  array.forEach((element) => {
    // Check each item: change the name if it matches the target
    element.items.forEach((item) => {
      if (item.name === targetName) item.name = newName;
    });
  });
}

// This function will check example array and change
// every name that has a value 'name4' into 'name6'
changeName(example, "name4", "name6");

CodePudding user response:

The for...of statement is my recommendation for readability and loop optimisation.

const example = [
  {
    id: '1234',
    desc: 'sample1',
    items: [
      { id: 1, name: 'name1' },
      { id: 2, name: 'testItem2' },
    ],
  },
  {
    id: '3456',
    desc: 'sample2',
    items: [
      { id: 4, name: 'name4' },
      { id: 5, name: 'testItem5' },
    ],
  },
];

const oldName = 'name4';
const newName = 'name6';

for (const group of example) {
  for (const item of group.items) {
    if (item.name === oldName) {
      item.name === newName;
      break
    }
  }
}

You could even go a step further and terminate the outer loop with a label if you only need to change the name in a single group.

outerLoop: for (const group of example) {
  for (const item of group.items) {
    if (item.name === oldName) {
      item.name === newName;
      break outerLoop;
    }
  }
}

Hope this helps.

CodePudding user response:

Why not like this?

const example = [{
    id: '1234',
    desc: 'sample1',
    items: [{
        id: 1,
        name: 'name1'
      },
      {
        id: 2,
        name: 'testItem2'
      }
    ]
  },
  {
    id: '3456',
    desc: 'sample2',
    items: [{
        id: 4,
        name: 'name4'
      },
      {
        id: 5,
        name: 'testItem5'
      }
    ]
  },
]

example[1].items[0].name = 'name6'
console.log(example)

  • Related