简体   繁体   English

根据给定的值通过键过滤对象

[英]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!我试图根据给定的值从对象中分离出可接受的值arrAcceptedValues acceptedValues = ["250", "300"]所以结果应该是两个不同的数组arrAcceptedValuesnotArrAcceptedValues我想我仍然缺少寻找不匹配值的逻辑! 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块引用

You can easily achieve the resuls using Object.keys , forEach and includes您可以使用Object.keysforEachincludes轻松实现结果

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

You can use Object.entries() with Array.prototype.reduce()您可以将Object.entries()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)

Following the decpk answer, here is the solution with map.在 decpk 答案之后,这是带有地图的解决方案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM