Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅在对象的每个值为true时将其转换为数组_Javascript - Fatal编程技术网

Javascript 如何仅在对象的每个值为true时将其转换为数组

Javascript 如何仅在对象的每个值为true时将其转换为数组,javascript,Javascript,我已选择我所在州的SizeList selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false} 如何将selectedSizeList转换为此数组 [2,3,4] 我有比这个更好的算法吗?(我认为我的解决方案不正确) 尝试此(O(n)解决方案): selectedSizeList={1:false,2:true,3:true,4:true,5:false}; var-arr=[]; for(在selectedSizeL

我已选择我所在州的SizeList

selectedSizeList = {1: false, 2: true, 3: true, 4: true, 5: false}
如何将selectedSizeList转换为此数组

[2,3,4]

我有比这个更好的算法吗?(我认为我的解决方案不正确)

尝试此(O(n)解决方案):

selectedSizeList={1:false,2:true,3:true,4:true,5:false};
var-arr=[];
for(在selectedSizeList中输入var键){
如果(已选择大小列表[键]){
arr.push(按键);
}
}
控制台日志(arr)尝试此(O(n)解决方案):

selectedSizeList={1:false,2:true,3:true,4:true,5:false};
var-arr=[];
for(在selectedSizeList中输入var键){
如果(已选择大小列表[键]){
arr.push(按键);
}
}

控制台日志(arr)
首先使用获取数组键,然后使用仅过滤像这样的
true

var-selectedSizeList={1:false,2:true,3:true,4:true,5:false};
var keys=Object.keys(selectedSizeList);
var filtered=keys.filter(函数(键){
返回selectedSizeList[键]
});

console.log(过滤)
首先使用获取数组键,然后使用仅过滤像这样的
true

var-selectedSizeList={1:false,2:true,3:true,4:true,5:false};
var keys=Object.keys(selectedSizeList);
var filtered=keys.filter(函数(键){
返回selectedSizeList[键]
});
console.log(过滤)
您可以使用
array#filter
过滤掉所有具有
true
值的键

const selectedSizeList={1:false,2:true,3:true,4:true,5:false};
常量结果=对象
.键(selectedSizeList)
.filter(k=>selectedSizeList[k])
.地图(编号);
控制台日志(结果)
您可以使用
array#filter
过滤掉所有具有
true
值的键

const selectedSizeList={1:false,2:true,3:true,4:true,5:false};
常量结果=对象
.键(selectedSizeList)
.filter(k=>selectedSizeList[k])
.地图(编号);
控制台日志(结果)您可以使用返回一个`数组,然后在上使用该数组返回一个新数组

selectedSizeList={1:false,2:true,3:true,4:true,5:false};
让数组=Object.keys(selectedSizeList).filter((项)=>{
if(selectedSizeList[项目])返回项目;
});
//返回[2,3,4]
您可以使用它返回一个`数组,然后可以使用它返回一个新数组

selectedSizeList={1:false,2:true,3:true,4:true,5:false};
让数组=Object.keys(selectedSizeList).filter((项)=>{
if(selectedSizeList[项目])返回项目;
});

//返回[2,3,4]
您可以使用Object.keys获取一个数组,然后您可以筛选等于true的值,该值将以字符串形式返回一个数字数组。然后,您可以使用Number在数组中进行映射,以将数字字符串转换为数字,这些数字可能是必需的,也可能不是必需的,具体取决于您计划如何处理此结果。我把它放进一个函数中,你可以重复使用,或者至少不用硬编码你的对象

var-selectedSizeList={1:false,2:true,3:true,4:true,5:false};
//不绑定到特定对象的可重用函数
常量filterForTrueValue=(obj)=>对象
.钥匙(obj)
.filter(k=>obj[k])
.地图(编号);

log(filterForTrueValue(selectedSizeList))您可以使用Object.keys获取一个数组,然后可以筛选等于true的值,该值将以字符串形式返回一个数字数组。然后,您可以使用Number在数组中进行映射,以将数字字符串转换为数字,这些数字可能是必需的,也可能不是必需的,具体取决于您计划如何处理此结果。我把它放进一个函数中,你可以重复使用,或者至少不用硬编码你的对象

var-selectedSizeList={1:false,2:true,3:true,4:true,5:false};
//不绑定到特定对象的可重用函数
常量filterForTrueValue=(obj)=>对象
.钥匙(obj)
.filter(k=>obj[k])
.地图(编号);
log(filterForTrueValue(selectedSizeList))
let array = [];
Objects.keys(selectedSizeList).maps((item) => {
    if(item.value) array.push(item.value)
);
return array;