我已在我的州选择了SizeList。
selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false}
如何将 selectedSizeList 转换为该数组?
[2,3,4]
我有比这个更好的算法吗? (我认为我的解决方案不正确)
let array = [];
Objects.keys(selectedSizeList).maps((item) => {
if(item.value) array.push(item.value)
);
return array;
最佳答案
您可以使用array#filter
过滤掉所有具有true
值的键。
const selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false};
const result = Object
.keys(selectedSizeList)
.filter(k => selectedSizeList[k])
.map(Number);
console.log(result);
关于javascript - 仅当每个值都为 true 时,如何将对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149180/