简体   繁体   中英

Find value from JSON and assign it to the Object [Node.js]

var obj = {
    name: "apple",
    color: "red",
    size: 12
}

This is my object, I want to find and take apple's price value from JSON and assign it to this object.

var prices = {"banana":12,"orange":8,"apple":22}

Now I need to take apple's price and assign it to the object. How do I do that?

var prices = {"banana":12,"orange":8,"apple":22}

var obj = {
    name: "apple",
    color: "red",
    size: 12
}

obj.price = prices[obj.name]

 var obj = { name:"apple", color: "red", size: 12 } var prices = { "banana":12, "orange":8, "apple":22 } obj.price=prices[obj.name]; console.log(obj.price); 

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