【问题标题】:Return matching objects from array of objects从对象数组中返回匹配的对象
【发布时间】:2018-10-17 23:59:52
【问题描述】:

我想根据 searchString 从给定数组中搜索匹配的值对象。例如,座位字符串将是“对象 0”。

var objArray = [
   { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
   { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
   { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
   { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' },
   { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];

现在我只想搜索那些以Object 为值的数组。意味着它应该返回我 0,1 & 3 对象

我们将不胜感激

【问题讨论】:

  • Array#filterObject.values - 换句话说,objArray.filter(item => Object.values(item).includes('Object 0'));
  • 工作示例stackblitz.com/edit/… 使用array.filter
  • @Pradeep Jain,如果我只想搜索“对象”怎么办?它返回给我空数组
  • @PrasannaSasne 你可以使用简单的循环。检查答案

标签: javascript arrays


【解决方案1】:

您可以遍历数组中的每个对象,获取每个对象的键以防止您自己访问对象中的动态属性,然后检查该键的值是否具有Object 作为子字符串。请注意以下代码中的obj[key].toString()。使用 toString() 是因为您的 id 具有整数值,而 indexOf() 适用于字符串值。因此,使用它可以防止错误。

var objArray = [
   { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
   { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
   { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
   { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 1' },
   { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
];

var res = [];
objArray.filter((obj) => {
  Object.keys(obj).forEach((key)=>{
    if(obj[key].toString().indexOf('Object') !== -1){
      res.push(obj);
    }
  });
});

console.log(res);

【讨论】:

    【解决方案2】:

    您可以使用array.filter 方法和在此过滤器方法中的所有属性上循环来做到这一点。

    var objArray = [
       { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
       { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
       { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
       { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
       { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
    ];
    
    var filteredArray = objArray.filter((obj) => {
      for (var key in obj) {
        if (obj[key] === 'Object 0') {
          return true;
        }
      }
      return false;
    });
    

    【讨论】:

    • 我不想搜索完全匹配。搜索字符串也可以小写。我尝试使用'==',但它不起作用。它作为空数组返回
    • == 不忽略大小写。您必须将字符串转换为小写进行比较。只需将 if 语句替换为:if (obj[key].toLowerCase() === 'object 0')。对于其他比较,您可能必须使用正则表达式。
    • 如果您想使用正则表达式,可以将 if 语句替换为 javscript match 函数:if (obj[key].match(/object/i)) { ...
    【解决方案3】:

    您可以通过在数组中使用filter() 来做到这一点。

    这是您正在寻找的样本

    var objArray = [
       { id: 0, name: 'Object 0', otherProp: '321', secondVal:'stack' },
       { id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow' },
       { id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context' },
       { id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0' },
       { id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test' }
    ];
    
    var resultArray = objArray.filter(function (d) { 
        return Object.values(d).indexOf('Object 0') != -1 
    });
    

    d 方法中 filter() 的值从对象数组中获取每个对象。 Object.values() 返回与对象中每个 keys 关联的值的数组。 indexOf() 本质上检查您要匹配的字符串是否在数组中可用。如果匹配,则将结果放入resultArray,否则将被忽略。最终的resultArray 如下所示:

    [
        {id: 0, name: "Object 0", otherProp: "321", secondVal: "stack"},
        {id: 1, name: "O1", otherProp: "Object 0", secondVal: "Overflow"},
        {id: 3, name: "Almost There", otherProp: "046", secondVal: "Object 0"}
    ]
    

    【讨论】:

      【解决方案4】:

      您可以使用array.filter()array.includes()Object.values() 来执行此操作。

      var objArray = [
            {id: 0, name: 'Object 0', otherProp: '321', secondVal: 'stack'},
            {id: 1, name: 'O1', otherProp: 'Object 0', secondVal: 'Overflow'},
            {id: 2, name: 'Another Object', otherProp: '850', secondVal: 'Context'},
            {id: 3, name: 'Almost There', otherProp: '046', secondVal: 'Object 0'},
            {id: 4, name: 'Last Obj', otherProp: '78', secondVal: 'test'},
          ];
      
      
      const result = objArray.filter((object) => {
        const values = Object.values(object);
        return values.includes('Object 0');
      });
      
      console.log(result);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        • 2021-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多