[英]Get an Object in an Array that has a property which is an Array that contains a matching value
[英]get the object which property contains an array as the value using javascript
我有一个包含一些像这样的对象的数组:
var lists = [{
"id": 1,
"status": 1,
"inputs": [],
"outputs": [
""
]
}, {
"id": "",
"status": 1,
"inputs": [
"test",
""
],
"outputs": [
""
]
}, {
"id": "",
"status": 1,
"inputs": [
"",
"test1"
],
"outputs": [
""
]
}, {
"id": "",
"status": 1,
"inputs": [
"gfg",
""
],
"outputs": [
""
]
}];
从这个数组中,我想获取inputs
属性至少包含一个元素的所有对象。 我怎样才能做到这一点?
你几乎拥有它:
function getObjects(lists) {
return lists.filter(function (el) {
return el.inputs.length;
});
}
var arr = getObjects(lists);
您只需要过滤数组并仅返回输入数组长度大于零的项目。 例如可以像这样实现
var x=lists.filter(function(x){return x.inputs.length>=1});
现在 x 是只包含输入数组不为空的对象的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.