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

306
Views
javascript: combine el objeto si el valor de su campo es el mismo

A continuación se muestran los datos actuales.

 const data = [ { id: 1, type: "call", strike: 3000, volume: 50000 }, { id: 2, type: "call", strike: 5000, volume: 30000 }, { id: 3, type: "call", strike: 7000, volume: 20000 }, { id: 4, type: "put", strike: 3000, volume: 50000 }, { id: 5, type: "put", strike: 5000, volume: 10000 }, { id: 6, type: "put", strike: 7000, volume: 7000 } ]

Quiero hacer un procesamiento de los data basado en strike , lo que significa que el objeto con el mismo valor en strike se combinará, de modo que se convierta en algo como

 [ { strike: 3000, callVolume: 50000, putVolume: 50000 }, { strike: 5000, callVolume: 30000, putVolume: 10000 }, { strike: 7000, callVolume: 20000, putVolume: 7000 }, ]

¿Cómo debo hacerlo en javascript?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Esto es realmente solo un 'agrupar por' por strike con un nombre de propiedad calculado basado en el type .

 const data = [ { id: 1, type: 'call', strike: 3000, volume: 50000 }, { id: 2, type: 'call', strike: 5000, volume: 30000 }, { id: 3, type: 'call', strike: 7000, volume: 20000 }, { id: 4, type: 'put', strike: 3000, volume: 50000 }, { id: 5, type: 'put', strike: 5000, volume: 10000 }, { id: 6, type: 'put', strike: 7000, volume: 7000 }, ]; const result = Object.values( data.reduce((a, { type, strike, volume }) => { (a[strike] ??= { strike })[`${type}Volume`] = volume; return a; }, {}) ); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Puede recopilar el volume de cada grupo.

 const data = [{ id: 1, type: "call", strike: 3000, volume: 50000 }, { id: 2, type: "call", strike: 5000, volume: 30000 }, { id: 3, type: "call", strike: 7000, volume: 20000 }, { id: 4, type: "put", strike: 3000, volume: 50000 }, { id: 5, type: "put", strike: 5000, volume: 10000 }, { id: 6, type: "put", strike: 7000, volume: 7000 }], result = Object.values(data.reduce((r, { type, strike, volume }) => { r[strike] ??= { strike, callVolume: 0, putVolume: 0 }; r[strike][type + 'Volume'] += volume; return r; }, {})); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar un bucle forEach/for para iniciar un elemento en el objeto de resultado . Al usar Array.find , el uso puede verificar que el resultado existe o no para iniciar un nuevo elemento de resultado.

 const data = [ { id: 1, type: "call", strike: 3000, volume: 50000 }, { id: 2, type: "call", strike: 5000, volume: 30000 }, { id: 3, type: "call", strike: 7000, volume: 20000 }, { id: 4, type: "put", strike: 3000, volume: 50000 }, { id: 5, type: "put", strike: 5000, volume: 10000 }, { id: 6, type: "put", strike: 7000, volume: 7000 } ] let result = []; data.forEach(item => { let result_item = result.find(e => e.strike == item.strike); var isNew = false; if (!result_item) { result_item = { strike: item.strike }; isNew = true; } if (item.type == "call") result_item.callVolume = item.volume; else result_item.putVolume = item.volume; if (isNew) result.push(result_item); }); console.log(result);

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!