Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looping through array of objects and reducing on multiple key/pairs

I have a large array of objects and I am trying to consolidate to where I have one array of objects that looks like below:
Each item(ex:banana) will be in 2 separate objects as I am trying to aggregate all buy orders and sell orders, and these are different prices/data.

I have tried grabbing the unique pairings (banana, apple, orange, etc.) and mapping through that and inside another map of the data, but I can’t figure it out.

let uniquePairing = Array.from([...new Set(data.map(item => item["name"]))])


**let data = [{
name:'banana,
price:( average cost)
type:buy
items: ( total bananas)
fee: ( total fees)
},
{
name:'banana,
price:( average cost)
type:sell
items: ( total bananas)
fee: ( total fees)
},
{ apples...
]**

Example of data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let data = [
{
name:"banana",
price:1,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:2,
type: "sell",
fee: 0.5,
items:25
},
{
name:"apple",
price:1,
type: "buy"
fee: 0.5
items:25
},
{
name:"apple",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"apple",
price:2,
type: "sell",
fee: 0.5,
items:25
}
]

>Solution :

You could do it like this:

   let data = [
{
name:"banana",
price:1,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"banana",
price:2,
type: "sell",
fee: 0.5,
items:25
},
{
name:"apple",
price:1,
type: "buy",
fee: 0.5,
items:25
},
{
name:"apple",
price:1.2,
type: "buy",
fee: 0.5,
items:25
},
{
name:"apple",
price:2,
type: "sell",
fee: 0.5,
items:25
}
]

const getFruitData = (fruits, fruit) => fruits.filter(fr=> fr.name===fruit.name && fr.type=== fruit.type)

const getTotal = (fruits, field) => fruits.reduce((acc,cur)=> acc+=cur[field], 0)

const isRegistered = (fruits, value) => fruits.some(fruit=> fruit.type === value.type && fruit.name===value.name)

const getFruits = (fruits) => {
  return fruits.reduce((acc, cur)=> {
if(isRegistered(acc, cur)){
  return acc
}
acc.push({name: cur.name, type: cur.type})
return acc
  }, [])
}

function mergeData(arr){
  const fruits = getFruits(arr)
  return fruits.map(fruit=> {
const data = getFruitData(arr, fruit)
return {
  name: fruit.name,
  price: getTotal(data, "price"),
  type: fruit.type,
  fee: getTotal(data, "fee"),
  items: getTotal(data, "items")
}
  })
}

console.log(mergeData(data))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading