Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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

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
}
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If I understand your question correctly, you can use the reduce function:

let uniquePairing = data.reduce((total, currentObj) => {
  const name = currentObj.name,
  type = currentObj.type;

  // Group by both `type` and `name`
  let relatedObj = total.find(obj => obj.name === name && obj.type === type);
  if (!relatedObj) {
    total.push({
    ...currentObj,

    // Adding `totalPrice` and `objCount` to track the cost average
    totalPrice: currentObj.price,
    objCount: 1
  })
} else {
  relatedObj.totalPrice += currentObj.price;
  relatedObj.objCount++;
  relatedObj.price = relatedObj.totalPrice / relatedObj.objCount;
  relatedObj.items += currentObj.items;
  relatedObj.fee += currentObj.fee;
}

  return total;
}, []);
about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!