【问题标题】:How to move element in nested array如何移动嵌套数组中的元素
【发布时间】:2018-04-17 14:33:37
【问题描述】:

我想移动嵌套数组中的元素。所以,这是我的数据:

let products = [
    {     
      "product_name": "A",
      "_id": "5ace995c14a759325776aab1",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 100,
          "price": 2000
        },
        {
          "_id": "5ad3a274ac827c165a510f99",
          "qty": 80,
          "price": 1500
        },
      ]
    },
    {
      "product_name": "B",
      "_id": "5ace995914a759325776aab0",
      "transactions": [
        {
          "_id": "5ad3a274ac827c165a510f9b",
          "qty": 80,
          "price": 1500
        }
      ],
    }
  ]

我期望的输出:

[
  {
    "_id": "5ad3a274ac827c165a510f99",
    "qty": 100,
    "price": 2000,
    "product_name": "A",
  },
  {
    "_id": "5ad3a274ac827c165a510f99",
    "qty": 80,
    "price": 1500,
    "product_name": "A",
  },
  {
    "_id": "5ad3a274ac827c165a510f9b",
    "qty": 80,
    "price": 1500,
    "product_name": "B",
  }
]

然后,我的解决代码:

function move() {
  var result = []
  for (product of products) {
    for (transaction of product.transactions) {
      transaction.product_name = product.product_name
      result.push(transaction)
    }
  }
  return result
}
product = move()

是否有任何有效的方法来创建输出,可能使用数组映射或其他任何方法?谢谢。

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    您可以将transactionsArray#reduce 合并,并使用Object.assign 添加product_name

    也用过:

    var products = [{ product_name: "A", _id: "5ace995c14a759325776aab1", transactions: [{ _id: "5ad3a274ac827c165a510f99", qty: 100, price: 2000 }, { _id: "5ad3a274ac827c165a510f99", qty: 80, price: 1500 }] }, { product_name: "B", _id: "5ace995914a759325776aab0", transactions: [{ _id: "5ad3a274ac827c165a510f9b", qty: 80, price: 1500 }] }],
        result = products.reduce((r, { transactions, product_name }) =>
            r.concat(transactions.map(t => Object.assign({}, t, { product_name }))),
            []
        );
      
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      您可以在reducemap 的交易中添加产品名称

      let result = products.reduce((c,v)=>{                            //Loop the array using reduce
          let transactions = v.transactions.map(o=>{                   //Loop thru each transactions using map
              return Object.assign(o,{"product_name":v.product_name}); //Clone the transaction and add the property product_name
          });
          return c.concat(transactions);                               //Merge the current array and the transactions
      },[]);
      

      这是一个sn-p:

      //Your array
      let products=[{"product_name":"A","_id":"5ace995c14a759325776aab1","transactions":[{"_id":"5ad3a274ac827c165a510f99","qty":100,"price":2000},{"_id":"5ad3a274ac827c165a510f99","qty":80,"price":1500},]},{"product_name":"B","_id":"5ace995914a759325776aab0","transactions":[{"_id":"5ad3a274ac827c165a510f9b","qty":80,"price":1500}],}]
      
      //The short version
      let result = products.reduce((c, v) => c.concat(v.transactions.map(o =>Object.assign(o, {"product_name": v.product_name}))), []);
      
      console.log(result);

      【讨论】:

        【解决方案3】:

        只要使用js方法就可以得到你想要的输出

        const products = [
            {     
              "product_name": "A",
              "_id": "5ace995c14a759325776aab1",
              "transactions": [
                {
                  "_id": "5ad3a274ac827c165a510f99",
                  "qty": 100,
                  "price": 2000
                },
                {
                  "_id": "5ad3a274ac827c165a510f99",
                  "qty": 80,
                  "price": 1500
                },
              ]
            },
            {
              "product_name": "B",
              "_id": "5ace995914a759325776aab0",
              "transactions": [
                {
                  "_id": "5ad3a274ac827c165a510f9b",
                  "qty": 80,
                  "price": 1500
                }
              ],
            }
          ]
          let output =[];
          products.forEach(elm => elm.transactions.forEach(transaction => {
        transaction.product_name = elm.product_name;
        output.push(transaction)}));
            console.log(output);
        
          

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 1970-01-01
          • 2020-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-26
          相关资源
          最近更新 更多