简体   繁体   中英

unable to insert a new key value pair to the existing pair

We have name value pair field in our table.These field can be modified ie either existing values can be changed or a new pair might get added . We have written the below script to update existing values . Please help on how to add ,new pair to the existing .

for (var name in u_service_characteristics) {
    if (parsed.service_characteristics[name] != null &&
        parsed.service_characteristics[name] != undefined) {
        u_service_characteristics[name] = parsed.service_characteristics[name];
    }
}

Above code only modifies the existing names ,how to insert if the incoming name doesnt exist.

无需遍历目标的键,而只需遍历源的键:

for(var name in parsed.service_characteristics)

I am guessing this is what you need

var existings = Object.getOwnPropertyNames(u_service_characteristics);
for (var name in parsed.service_characteristics) {
    if (!existings.includes(name)) {
        u_service_characteristics[name] = parsed.service_characteristics[name];
    }
}

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