简体   繁体   中英

Updating an Array that's present inside an Object

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

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM