Home > Software design >  Updating an Array that's present inside an Object
Updating an Array that's present inside an Object

Time:04-12

I'm trying to update an array (Array name is "Variables" please refer the attached screenshot) which presents inside an Object, so I want to update that array if there is word called "placeholder" in alertMessage(it's a different property presents in the same Object)

I'd appreciate any help on how to update this array in question, I tried using pop method but it didn't go as planned and I've attached screenshots of the Objects for reference

enter image description here

CodePudding user response:

You can retrieve the string placeholder like this data['alertMessage']['en_US']['all'] and then use a conditional statement to make changes to the array inside the data object.

let data = {
  alertOne: '',
  alertTwo: '',
  alertMessage: {
    en_US: {all: 'placeholder'}
  },
  variables: [
     {id: 0, uuid: '123'},
     {id: 1, uuid: '223'},
     {id: 2, uuid: '323'}
  ]
}

let all = data['alertMessage']['en_US']['all']

// if condition is met add a new object to the array
if(all === 'placeholder'){
  data.variables = [...data.variables, {id: 3, uuid: '423'}] 
}

console.log(data)

CodePudding user response:

Your question is too confusing. Try to reformulate it so we can help.

  • Related