我有一个名为files
的数组:
files = [
{
id: "1",
title: "aras",
price: 100,
size: 25,
tools: [
"Hammer",
"Zip",
"Line",
"Nexti"
]
},
{
id: "2",
title: "yasio",
price: 150,
size: 30,
tools: [
"Hammer",
"Zip",
]
},
{
id: "3",
title: "janio",
price: 200,
size: 30,
tools: [
"Line",
"Nexti"
]
},
{
id: "4",
title: "chashio",
price: 400,
size: 35,
tools: [
"Nexti"
]
},
]
例如,我要过滤此对象以获取数组:90
至150
之间的价格和25
至40
之间的尺寸以及包含{ {1}}和Hammer
。
输出必须为:
Zip
我该怎么做?
答案 0 :(得分:1)
files.filter(function(file){
return ((file.price>= 90 && file.price<=150) && (file.size >= 25 && file.size <= 40))
})
答案 1 :(得分:1)
您需要使用Array.prototype.filter()
返回满足以下条件的项目:
(price>=90 && price <= 150) && (size >= 25 && size <= 40)
var files = [
{id: "1", title: "aras", price: 100, size: 25, tools: ["Hammer","Zip","Line","Nexti"] },
{id: "2", title: "aras", price: 150, size: 30, tools: ["Hammer","Zip"]},
{id: "3", title: "aras", price: 160, size: 35, tools: ["Line","Nexti"]},
{id: "4", title: "aras", price: 90, size: 40, tools: ["Nexti"]},
{id: "5", title: "aras", price: 200, size: 45, tools: []}
]
var res = files.filter(f => (f.price>=90 && f.price <= 150)
&& (f.size >= 25 && f.size <= 40)
&& (f.tools.includes('Hammer') && f.tools.includes('Zip')));
console.log(res);