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

151
Views
manipulación de objetos complejos

tengo una matriz:

 const arr = [ { countries : {countryCode :"US", value: true}, vendors: [{vendorName: 'TES', value: true}, {vendorName: 'HPEFS', value: true}] }, { countries : {countryCode :"CA", value: true}, vendors: [{vendorName: 'TES', value: true}, {vendorName: 'HPEFS', value: false}] } ]; expected result: [{vendor: "TES", countries: [US, CA]}, {vendor: "HPEFS", countries: [US]}]

Se agradece cualquier idea, gracias de antemano

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

0

Puede lograr fácilmente el resultado usando Map y forEach

 const arr = [ { countries: { countryCode: "US", value: true }, vendors: [ { vendorName: "TES", value: true }, { vendorName: "HPEFS", value: true }, ], }, { countries: { countryCode: "CA", value: true }, vendors: [ { vendorName: "TES", value: true }, { vendorName: "HPEFS", value: false }, ], }, ]; const map = new Map(); arr.forEach(({ countries, vendors }) => { const { countryCode, value } = countries; vendors.forEach((o) => { if (o.value) { !map.has(o.vendorName) ? map.set(o.vendorName, [countryCode]) : map.get(o.vendorName).push(countryCode); } }); }); const result = []; for (let [vendor, countries] of map) result.push({ vendor, countries }); console.log(result);
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Aquí está la solución:

 const arr = [ { countries: { countryCode: "US", value: true }, vendors: [{ vendorName: 'TES', value: true }, { vendorName: 'HPEFS', value: true }] }, { countries: { countryCode: "CA", value: true }, vendors: [{ vendorName: 'TES', value: true }, { vendorName: 'HPEFS', value: false }] } ]; // expected result: [{vendor: "TES", countries: [US, CA]}, {vendor: "HPEFS", countries: [US]}] const loookup = arr.reduce((acc, curr) => { curr.vendors.forEach(item => { if (item.value) { if (acc[item.vendorName] && !acc[item.vendorName].includes(curr.countries)) { acc[item.vendorName] = [...acc[item.vendorName], curr.countries.countryCode] } else { acc[item.vendorName] = [curr.countries.countryCode] } } }) return acc; }, {}); const result = Object.keys(loookup).map(key => { return { vendor: key, countries: loookup[key] } }) console.log(result)
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

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!