javascript - 仅当每个值都为 true 时,如何将对象转换为数组

标签 javascript

我已在我的州选择了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/

相关文章:

javascript - 在 ngrx 中的订阅内部进行管道传输

javascript - 如何扩展ExtJS SplitButton?

javascript - 使用表单外的按钮提交动态创建的表单

javascript - Highcharts - 具有多色图(点)的单系列散点图

javascript - 堆积柱注释谷歌图表

javascript - 从视频中提取海报图像

javascript - 是否可以在其他 then for() 循环中使用 await ?

javascript - 如何在 angularJS 或 javascript 中将两个不同的数组值转换为字符串

javascript - array.push(SomeObject) 不会复制 SomeObject 中包含的数组

javascript - 从ajax读取文本文件并将字符串发送到html文件