Home > OS >  filter an object by its keys based on given values
filter an object by its keys based on given values

Time:10-15

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Blockquote

CodePudding user response:

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; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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.push(v)
      : a.notArrAcceptedValues.push(v)
    return a
  },
  { arrAcceptedValues: [], notArrAcceptedValues: [] }
)

console.log(result)
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related