在嵌套对象中向对象添加属性

时间:2018-03-03 19:15:44

标签: javascript arrays json ecmascript-6 javascript-objects

我有一个包含对象的数组,例如:

data1 = [
{
    "name": "Name-A",
    "image": "images/a.png",
    "prices": [
        {
            "name": "c1",
            "buy": "10",
            "sell": "11"
        },
        {
            "name": "c2",
            "buy": "16",
            "sell": "15"
        }
    ]
},
{
    "name": "Name-B",
    "image": "images/b.png",
    "prices": [
        {
            "name": "c1",
            "buy": 11,
            "sell": 12
        }
    ]
},
{
    "name": "Name-C",
    "image": "images/c.png",
    "prices": [
        {
            "name": "c3",
            "buy": 123,
            "sell": 124
        }
    ]
}]

在这里,我想根据存储在其他API链接中的排名将排名添加到特定名称,例如:

data2=[{
    "name": "cs1",
    "symbol": "C1",
    "id": 1
},
{
    "name": "cs2",
    "symbol": "C2",
    "rank": 2,
    "id": 1027
},
 {...}, 
 {...}
 ....]

所以结果数组应该是,

[ {
    "name": "Name-A",
    "image": "images/a.png",
    "prices": [
        {
            "name": "c1",
            "rank": 1,
            "buy": "10",
            "sell": "11"
        },
        {
            "name": "c2",
            "rank": 2,
            "buy": "16",
            "sell": "15"
        }
    ]
},
{
    "name": "Name-B",
    "image": "images/b.png",
    "prices": [
        {
            "name": "c1",
            "rank": 1,
            "buy": 11,
            "sell": 12
        }
    ]
},
{
    "name": "Name-C",
    "image": "images/c.png",
    "prices": [
        {
            "name": "c3",
            "rank": 3,
            "buy": 123,
            "sell": 124
        }
    ]
}]

我需要比较前两个对象数组,并需要在第三个数组中获取rank。 前两个数组分别与前两个数组的namesymbol进行比较。 我尝试在javascript中使用map,但结果数组在所有对象中包含相同的rank,可能是最后一个对象的等级。

我的代码:

{
 data1.map((cd)=>{
 cd.prices.map((p)=>{
  data2.map((r)=>{
   if(r.symbol.toUpperCase() === p.name.toUpperCase()){
     cd.prices.forEach(function(cs){
     cs.rank = r.rank;
  });
  }
})

}) }

如何做到这一点,以便得到具有等级的结果对象数组?我坚持这个。

1 个答案:

答案 0 :(得分:0)

您可以使用forEach循环,使用map功能以避免data1.prices修改,使用find查找具有rank值的特定对象。< / p>

&#13;
&#13;
var data1 = [{    "name": "Name-A",    "image": "images/a.png",    "prices": [        {            "name": "c1",            "buy": "10",            "sell": "11"        },        {            "name": "c2",            "buy": "16",            "sell": "15"        }    ]},{    "name": "Name-B",    "image": "images/b.png",    "prices": [        {            "name": "c1",            "buy": 11,            "sell": 12        }    ]},{    "name": "Name-C",    "image": "images/c.png",    "prices": [        {            "name": "c3",            "buy": 123,            "sell": 124        }    ]}];
var data2=[{    "name": "cs1",    "symbol": "C1",    "rank": 1,    "id": 1},{    "name": "cs2",    "symbol": "C2",    "rank": 2,    "id": 1027},{    "name": "cs3",    "symbol": "C3",    "rank": 3,    "id": 10276}];

var result = data1.map((o) => {
    // This is to avoid data1 modification.
  var newObj = { ...o, prices: o.prices.map((pp) => ({...pp})) }; 
  
  newObj.prices.forEach((p) => {
    var found = data2.find((d) => d.symbol.toUpperCase() === p.name.toUpperCase());
    if (found) p.rank = found.rank;
  });
  
  return newObj;
});

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;