2

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

Gass
  • 7,536
  • 3
  • 37
  • 41
Noob
  • 25
  • 6
  • 2
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/a/285557/12402732) == post a snippet with your code with an example of what you did. – Simone Rossaini Apr 12 '22 at 06:05

1 Answers1

1

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)
Gass
  • 7,536
  • 3
  • 37
  • 41
  • Thank you for the help but sadly I'm getting "cannot read properties of undefined (reading 'en_US')", did I miss anything? – Noob Apr 12 '22 at 07:24