[英]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.