Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
filter an object by its keys based on given values

I am trying to isolate accepted value from an object based on given values acceptedValues = ["250", "300"] so the result should be two different arrays arrAcceptedValues, notArrAcceptedValues i think I still missing the logic in finding the unmatched values! I hope someone can explain it

const acceptedValues = ["250", "300"];

const myObj = {
  250: [1222, 1330, 1334, 1336, 6130, 6154, 6186, 6311, 6314],
  300: [1112, 1146, 1186, 1224, 6657],
  315: [710, 717],
  545: [4778],
  700: [637, 969, 1321, 6534, 1324, 6898, 738, 6899],
  750: [6323],
  900: [1717, 1718, 1720],
  1500: [5824, 5830, 5831, 5835, 5919, 5920, 5989],
};

let arrAcceptedValues = [];
let notArrAcceptedValues = [];
for (let iterator of acceptedValues) {
  // find all values that IS contains in the myObj array ["250", "300"]
  if (myObj[iterator]) {
    const returnData = myObj[iterator];
    arrAcceptedValues.push(returnData);
  }
  // find all values that NOT contains in the myObj array ["250", "300"]
  for (const keyIterator of Object.keys(myObj)) {
    if (keyIterator !== iterator) {
      notArrAcceptedValues.push(Object.values(myObj));
    }
  }
}
console.log("arrAcceptedValues", arrAcceptedValues);
console.log("notArrAcceptedValues", notArrAcceptedValues);

Blockquote

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can easily achieve the resuls using Object.keys, forEach and includes

You can even make a one liner also as:

Object.keys(myObj).forEach((k) =>(acceptedValues.includes(k) ? arrAcceptedValues : notArrAcceptedValues).push(myObj[k]));

const acceptedValues = ["250", "300"];

const myObj = {
  250: [1222, 1330, 1334, 1336, 6130, 6154, 6186, 6311, 6314],
  300: [1112, 1146, 1186, 1224, 6657],
  315: [710, 717],
  545: [4778],
  700: [637, 969, 1321, 6534, 1324, 6898, 738, 6899],
  750: [6323],
  900: [1717, 1718, 1720],
  1500: [5824, 5830, 5831, 5835, 5919, 5920, 5989],
};

let arrAcceptedValues = [];
let notArrAcceptedValues = [];

Object.keys(myObj).forEach((k) => {
  acceptedValues.includes(k)
    ? arrAcceptedValues.push(myObj[k])
    : notArrAcceptedValues.push(myObj[k]);
});

console.log(arrAcceptedValues);
console.log(notArrAcceptedValues);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

or

const acceptedValues = ["250", "300"];

const myObj = {
  250: [1222, 1330, 1334, 1336, 6130, 6154, 6186, 6311, 6314],
  300: [1112, 1146, 1186, 1224, 6657],
  315: [710, 717],
  545: [4778],
  700: [637, 969, 1321, 6534, 1324, 6898, 738, 6899],
  750: [6323],
  900: [1717, 1718, 1720],
  1500: [5824, 5830, 5831, 5835, 5919, 5920, 5989],
};

let arrAcceptedValues = [];
let notArrAcceptedValues = [];

Object.keys(myObj).forEach((k) =>(acceptedValues.includes(k) ? arrAcceptedValues : notArrAcceptedValues).push(myObj[k]));

console.log(arrAcceptedValues);
console.log(notArrAcceptedValues);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Object.entries() with Array.prototype.reduce()

Code:

const myObj = {
  250: [1222, 1330, 1334, 1336, 6130, 6154, 6186, 6311, 6314],
  300: [1112, 1146, 1186, 1224, 6657],
  315: [710, 717],
  545: [4778],
  700: [637, 969, 1321, 6534, 1324, 6898, 738, 6899],
  750: [6323],
  900: [1717, 1718, 1720],
  1500: [5824, 5830, 5831, 5835, 5919, 5920, 5989],
}

const acceptedValues = ['250', '300']

const result = Object.entries(myObj).reduce((a, [k, v]) => {
  (acceptedValues.includes(k) ? a.arrAcceptedValues : a.notArrAcceptedValues).push(v)
  return a
}, { arrAcceptedValues: [], notArrAcceptedValues: [] })

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Following the decpk answer, here is the solution with map.

const acceptedValues = ["250", "300"];

const myObj = {
  250: [1222, 1330, 1334, 1336, 6130, 6154, 6186, 6311, 6314],
  300: [1112, 1146, 1186, 1224, 6657],
  315: [710, 717],
  545: [4778],
  700: [637, 969, 1321, 6534, 1324, 6898, 738, 6899],
  750: [6323],
  900: [1717, 1718, 1720],
  1500: [5824, 5830, 5831, 5835, 5919, 5920, 5989],
};

const arrAcceptedValues = [];
const notArrAcceptedValues = [];

Object.keys(myObj).map((key) => {
  (acceptedValues.includes(key) ? arrAcceptedValues : notArrAcceptedValues).push(myObj[key])
})

console.log(arrAcceptedValues);
console.log(notArrAcceptedValues);

about 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda