通过空属性筛选数组
Filter array by empty property
我在Ajax响应中得到如下所示的对象数组。
{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
},
{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": {
"latitude": 54.4,
"longitude": -32.7
}
}
{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": ""
}
我想通过warehouseLocation
过滤这些对象,这意味着我只需要warehouseLocation
不为空的对象。
可以使用Array.prototype.filter函数:
var data = [{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
}, {
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": {
"latitude": 54.4,
"longitude": -32.7
}
},
{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": ""
}
];
var warehouseData = data.filter(function(val) {
return val.warehouseLocation != "";
});
这是一个开始的例子。
var arr = [{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
"tags": ["cold", "ice"],
"dimensions": {
"length": 7.0,
"width": 12.0,
"height": 9.5
},
"warehouseLocation": {
"latitude": -78.75,
"longitude": 20.4
}
}, {
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": {
"latitude": 54.4,
"longitude": -32.7
}
}, {
"id": 3,
"name": "A blue mouse",
"price": 25.50,
"dimensions": {
"length": 3.1,
"width": 1.0,
"height": 1.0
},
"warehouseLocation": ""
}]
var filteredArr = arr.filter(function(val) {
if (val.warehouseLocation == "")
return false;
return true;
});
console.log(filteredArr);
相关文章:
- 根据多个值筛选数组
- 使用特定值筛选数组
- 在javascript中按字符串长度筛选数组
- 如何筛选数组数组
- 如何获取输入字段的值并使用它来筛选数组
- 基于键和值的筛选数组
- 对多个选项进行筛选数组以在新选择中返回选项
- AngularJS ngOptions按布尔字段筛选数组
- 使用用户输入的维度,根据每个元素中的字符数筛选数组
- 需要有关创建筛选数组内容的复选框的帮助
- 使用jQuery筛选数组
- 如何使用foreach绑定将select选项中的参数传递给触发器和筛选数组可观测值
- 如何使用jQuery.grep()动态筛选数组
- 数组的严格筛选数组
- 在PHP中使用关键字筛选数组
- 基于javascript中的另一个数组筛选数组
- 基于索引数组筛选数组
- 为提前输入筛选数组的最快方法
- Angularjs基于array类型的属性来筛选数组
- 返回只包含与给定值匹配的元素的筛选数组