0

I am confused with array inside object inside array, want to add a field inside presentsData call it sumP and inside get the sum of the value inside array presents using the array prices.
I tried reduce but it wasn't working, I tried with map and find and I am not getting a number I am getting an object, and can't figure out how to loop the both values
This is my code:

    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)

this is what I expected:

[
    {
      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*/)
    }
]

1 Answer 1

2

Convert the prices to a Map or object of price by present. Iterate the data with Array.map(), and for each item reduce the presents to a number, taking the price from the prices Map.

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.