从JSON中查找值并将其分配给Object [Node.js]

时间:2017-10-27 17:41:37

标签: javascript json node.js object

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

这是我的目标,我想从JSON中找到并获取苹果公司的价格值并将其分配给此对象。

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

现在我需要拿苹果的价格并将其分配给对象。我该怎么做?

2 个答案:

答案 0 :(得分:0)

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

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

obj.price = prices[obj.name]

答案 1 :(得分:-3)

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);