簡體   English   中英

如何從數組內的 object 內的數組中添加 2 個值並將其顯示在外的字段中?

[英]How can I add 2 values from array inside object inside array and show it in field outside?

我對數組內部的 object 內部的數組感到困惑,想在presents中添加一個字段,將其sumP presentsData在內部使用數組prices獲取數組中的值的總和。
我嘗試減少但它不起作用,我嘗試使用 map 並找到並且我沒有得到一個數字我得到一個 object,並且無法弄清楚如何循環這兩個值
這是我的代碼:

    let presentsData= [
        {
          name: "Peter",
          presents: ["coffee","holidays"],
          money: 7000
        },
        {
          name: "Mario",
          presents: ["car","coal"],
          money: 300
        },
        {
          name: "Amanda",
          presents: ["computer","coal"],
          money: 300
        },
        {
          name: "David",
          presents: ["clothes", "car"],
          money: 2000
        }
    ]
    const prices= [
        {
          present: "coffee",
          price: 1
        },
        {
          present: "holidays",
          price: 1000
        },
        {
          present: "videogames",
          price: 40
        },
        {
          present: "computer",
          price: 600
        },
        {
          present: "tattoo",
          price: 30
        },
        {
          present: "clothes",
          price: 80
        },
        {
          present: "car",
          price: 6000
        },
        {
          present: "phone",
          price: 800
        },
        {
          present: "motorbike",
          price: 3500
        }
      ]
    const res =  presentsData.map(s => 
    ({
            ...s,
            sumP: prices.find(e=>e.present === s.presents[0] ? e.price: 0)
         
    }));
    console.log(res)

這是我的預期:

[
    {
      name: "Peter",
      presents: ["coffee","holidays"],
      money: 7000
      sumP: (/*SUM OF PRESENTS'S VALUE*/)
    },
    {
      name: "Mario",
      presents: ["car","coal"],
      money: 300
      sumP: (/*SUM OF PRESENTS'S VALUE*/)
    },
    {
      name: "Amanda",
      presents: ["computer","coal"],
      money: 300
      sumP: (/*SUM OF PRESENTS'S VALUE*/)
    },
    {
      name: "David",
      presents: ["clothes", "car"],
      money: 2000
      sumP: (/*SUM OF PRESENTS'S VALUE*/)
    }
]

present價格將prices轉換為 Map 或 object price 使用Array.map()迭代數據,並將每個項目的禮物減少到一個數字,從price Map 中獲取prices

 const fn = (data, prices) => { // creat a Map of price by present const pricesMap = new Map(prices.map(({ present, price }) => [present, price])) // map the data return data.map(o => ({...o, // add sum by reducing the presents and taking the prices from the Map sumP: o.presents.reduce((acc, p) => acc + (pricesMap.get(p)?? 0), 0) })) } const presentsData = [{"name":"Peter","presents":["coffee","holidays"],"money":7000},{"name":"Mario","presents":["car","coal"],"money":300},{"name":"Amanda","presents":["computer","coal"],"money":300},{"name":"David","presents":["clothes","car"],"money":2000}] const prices = [{"present":"coffee","price":1},{"present":"holidays","price":1000},{"present":"videogames","price":40},{"present":"computer","price":600},{"present":"tattoo","price":30},{"present":"clothes","price":80},{"present":"car","price":6000},{"present":"phone","price":800},{"present":"motorbike","price":3500}] const result = fn (presentsData, prices) console.log(result)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM