简体   繁体   English

使用javascript获取属性包含数组的对象作为值

[英]get the object which property contains an array as the value using javascript

I have an array containing some objects like this :我有一个包含一些像这样的对象的数组:

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": [
    ""
  ]
}];

From this array I want to get all objects in which the inputs property contains at least one element.从这个数组中,我想获取inputs属性至少包含一个元素的所有对象。 How can I do this?我怎样才能做到这一点?

You almost had it:你几乎拥有它:

function getObjects(lists) {
  return lists.filter(function (el) {
    return el.inputs.length;
  });
}

var arr = getObjects(lists);

DEMO演示

You just need to filter the array and return only items which has length of inputs array greater than zero .您只需要过滤数组并仅返回输入数组长度大于零的项目。 It can be achieved for example like this例如可以像这样实现

var x=lists.filter(function(x){return x.inputs.length>=1});

Now x is the array that containts only objcets whose input array isnt empty.现在 x 是只包含输入数组不为空的对象的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取具有属性的数组中的对象,该属性是包含匹配值的数组 - Get an Object in an Array that has a property which is an Array that contains a matching value JSON / Javascript:返回哪个数组对象包含某个属性 - JSON/Javascript: return which array object contains a certain property 如何检查数组是否包含值,这是对象的属性? - How to check if array contains value, which is a property of an an object? 在 JavaScript 中检查数组是否包含具有特定属性值的对象? - Check if an array contains an object with a certain property value in JavaScript? 获取包含NULL值的JavaScript / angularJS对象键 - Get JavaScript/angularJS object keys which contains NULL value 从包含特定值的数组中获取对象 - Get an object from array which contains a specific value 如何获取包含在嵌套数组中给定值的对象中的项目 - How get items in object which contains in nested array given value 获取包含嵌套数组中的值的对象的索引-JavaScript - Get index of object that contains value in a nested array - JavaScript 如何为Javascript对象中的属性设置值,该属性由键数组标识 - How to set value to a property in a Javascript object, which is identified by an array of keys JavaScript获得对象数组的值或属性(如果对象具有该属性) - JavaScript get value or property of an Array of objects if object has that property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM