gpt4 book ai didi

javascript - 根据给定值通过键过滤对象

转载 作者:行者123 更新时间:2023-12-02 01:56:16 25 4
gpt4 key购买 nike

我正在尝试根据给定值将接受的值与对象隔离acceptedValues = ["250", "300"],因此结果应该是两个不同的数组arrAcceptedValues, notArrAcceptedValues 我想我仍然缺少查找不匹配值的逻辑!希望有人能解释一下

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

最佳答案

您可以使用 Object.keys 轻松获得结果, forEachincludes

您甚至可以制作一个衬垫:

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

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

关于javascript - 根据给定值通过键过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69580639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com