展平嵌套对象

时间:2018-03-13 01:58:18

标签: javascript object promise

我正在处理GET请求,该请求会查找用户在数据库中保存的ID和馆藏数量。它接受该id并调用一个函数来检索有关该项的更新信息。我试图做的还包括GET请求中的馆藏,但是,我希望它在对象内部具有更新的信息。基本上我想要做的是展平这些对象,使它成为两个对象的数组,而不是对象中的对象。以下是我使用的代码和接收的输出。我尝试过更换Object.assign部分,但在大多数情况下,我都失去了如何实现这一目标。

当前

const getFullCryptoPortfolio = () =>
  CryptoPortfolio.find()
    .then(portfolios =>
      Promise.all(portfolios.map(portfolio => getCoins(portfolio.id).then(item =>
        Object.assign({}, item, {
          holdings: portfolio.holdings
        })))));

[
    {
        "0": {
            "id": "bitcoin",
            "name": "Bitcoin",
            "symbol": "BTC",
            "rank": "1",
            "price_usd": "9289.45",
            "price_btc": "1.0",
            "24h_volume_usd": "6536340000.0",
            "market_cap_usd": "157138942782",
            "available_supply": "16915850.0",
            "total_supply": "16915850.0",
            "max_supply": "21000000.0",
            "percent_change_1h": "0.36",
            "percent_change_24h": "-3.7",
            "percent_change_7d": "-18.82",
            "last_updated": "1520905166"
        },
        "holdings": 1
    },
    {
        "0": {
            "id": "ethereum",
            "name": "Ethereum",
            "symbol": "ETH",
            "rank": "2",
            "price_usd": "704.491",
            "price_btc": "0.0765146",
            "24h_volume_usd": "1773830000.0",
            "market_cap_usd": "69147117523.0",
            "available_supply": "98151882.0",
            "total_supply": "98151882.0",
            "max_supply": null,
            "percent_change_1h": "0.53",
            "percent_change_24h": "-3.12",
            "percent_change_7d": "-17.17",
            "last_updated": "1520905152"
        },
        "holdings": 2
    }
]

预期输出示例:

 [
    {
            "id": "bitcoin",
            "name": "Bitcoin",
            "symbol": "BTC",
            "rank": "1",
            "price_usd": "9289.45",
            "price_btc": "1.0",
            "24h_volume_usd": "6536340000.0",
            "market_cap_usd": "157138942782",
            "available_supply": "16915850.0",
            "total_supply": "16915850.0",
            "max_supply": "21000000.0",
            "percent_change_1h": "0.36",
            "percent_change_24h": "-3.7",
            "percent_change_7d": "-18.82",
            "last_updated": "1520905166",
            "holdings": 1
    },
    {
            "id": "ethereum",
            "name": "Ethereum",
            "symbol": "ETH",
            "rank": "2",
            "price_usd": "704.491",
            "price_btc": "0.0765146",
            "24h_volume_usd": "1773830000.0",
            "market_cap_usd": "69147117523.0",
            "available_supply": "98151882.0",
            "total_supply": "98151882.0",
            "max_supply": null,
            "percent_change_1h": "0.53",
            "percent_change_24h": "-3.12",
            "percent_change_7d": "-17.17",
            "last_updated": "1520905152",
            "holdings": 2
    }
]

2 个答案:

答案 0 :(得分:1)

问题似乎是getCoins方法解析了您尝试与馆藏合并的一系列对象。您可以修改Object.assign,如下所示 -

Object.assign({}, ...item, {
  holdings: portfolio.holdings
});

这会将对象传播到item数组中。请注意,如果多个项具有相同的属性,则后者将覆盖它。

如果getCoins始终返回单个项目,您可以改为 -

Object.assign({}, item[0], {
  holdings: portfolio.holdings
});

或者你可以保留数组,并为每个对象分配馆藏。

item.map(obj => Object.assign({}, obj , {
  holdings: portfolio.holdings
}))

答案 1 :(得分:0)

  1. 将“0”的所有属性分配给对象'item';
  2. 将“馆藏”属性分配给对象'item';
  3. 退货。

    function flatten(input) {
      return input.map(function(x){
        let item = x["0"];
        item["holdings"] = x["holdings"];
        return item;
      });
    }