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

问题描述 投票:0回答:1

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

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

}) }

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

javascript arrays json ecmascript-6 javascript-objects
1个回答
0
投票

您可以使用forEach循环,函数map以避免data1.prices修改和find以查找具有rank值的特定对象。

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
© www.soinside.com 2019 - 2024. All rights reserved.