• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

88
Vistas
complex object manipulation

I have an array:

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]}]

Any idea is appreciated, Thanks in advance

almost 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can easily achieve the result using Map and 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; }

almost 3 years ago · Juan Pablo Isaza Denunciar

0

Here is the solution:

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; }

almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda