过滤所有属性值上的对象数组

时间:2016-10-02 14:08:06

标签: javascript arrays filter

我真的很惊讶我找不到与我的问题相关的任何内容。我正在寻找一种基于用户文本输入过滤我的对象数组的快速方法。

假设我有这个数组:

comb_sets = [a.intersection(b) for a, b in comb]
comb_sets

用户写“s”,预期结果为:

[{2}, set(), {4}]

我可以这样使用过滤功能:

let data = [{
  "id": 1,
  "first_name": "Jean",
  "last_name": "Owens",
  "email": "jowens0@google.ru",
  "gender": "Female"
}, {
  "id": 2,
  "first_name": "Marie",
  "last_name": "Morris",
  "email": "mmorris1@engadget.com",
  "gender": "Female"
}, {
  "id": 3,
  "first_name": "Larry",
  "last_name": "Wallace",
  "email": "lwallace2@example.com",
  "gender": "Male"
}];

所以我想知道这个解决方案是否有更好的替代方案?

- 感谢KoolShams

,这是一个有效的JsFiddle

- Plunker用于基准测试目的(使用2k数据测试)

3 个答案:

答案 0 :(得分:5)

您可以改用Object.keys()some()



let data = [{
  "id": 1,
  "first_name": "Jean",
  "last_name": "Owens",
  "email": "jowens0@google.ru",
  "gender": "Female"
}, {
  "id": 2,
  "first_name": "Marie",
  "last_name": "Morris",
  "email": "mmorris1@engadget.com",
  "gender": "Female"
}, {
  "id": 3,
  "first_name": "Larry",
  "last_name": "Wallace",
  "email": "lwallace2@example.com",
  "gender": "Male"
}];

var result = data.filter(function(o) {
  return Object.keys(o).some(function(k) {
    return o[k].toString().toLowerCase().indexOf('s') != -1;
  })
})

console.log(result)




答案 1 :(得分:1)

您可以使用Object.keys并省略hasOwnProperty

此解决方案具有arrow functions

let data = [{ "id": 1, "first_name": "Jean", "last_name": "Owens", "email": "jowens0@google.ru", "gender": "Female" }, { "id": 2, "first_name": "Marie", "last_name": "Morris", "email": "mmorris1@engadget.com", "gender": "Female" }, { "id": 3, "first_name": "Larry", "last_name": "Wallace", "email": "lwallace2@example.com", "gender": "Male" }],
    searchText = "s",
    result = data.filter(o => 
        Object.keys(o).some(k => 
            o[k].toString().toLowerCase().indexOf(searchText) !== -1));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

为了获得更好的性能,您可以提前存储密钥并迭代密钥,如果它们是统一的,则不对数组中的所有对象使用Object.keys

let data = [{ "id": 1, "first_name": "Jean", "last_name": "Owens", "email": "jowens0@google.ru", "gender": "Female" }, { "id": 2, "first_name": "Marie", "last_name": "Morris", "email": "mmorris1@engadget.com", "gender": "Female" }, { "id": 3, "first_name": "Larry", "last_name": "Wallace", "email": "lwallace2@example.com", "gender": "Male" }],
    keys = Object.keys(data[0]),
    searchText = "s",
    result = data.filter(o => 
        keys.some(k => 
            o[k].toString().toLowerCase().indexOf(searchText) !== -1));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

好吧,你可以使用任何 lodash< x> 集合相关函数来对象,它会迭代值。你也可以直接_.toLower使用任何字符串或数字或布尔值,它将处理所有的角落情况,所以如果你想要一个更简单的解决方案,结构良好的代码,那么你去:

data.filter(o=>_.some(o, v => _.toLower(v).indexOf('s')>-1))

以下是工作示例:



let data = [{
  "id": 1,
  "first_name": "Jean",
  "last_name": "Owens",
  "email": "jowens0@google.ru",
  "gender": "Female"
}, {
  "id": 2,
  "first_name": "Marie",
  "last_name": "Morris",
  "email": "mmorris1@engadget.com",
  "gender": "Female"
}, {
  "id": 3,
  "first_name": "Larry",
  "last_name": "Wallace",
  "email": "lwallace2@example.com",
  "gender": "Male"
}];

var sTxt = 's';

var res = data.filter(o=>_.some(o, v =>_.toLower(v).indexOf(sTxt)>-1))

console.log('Result: ', JSON.stringify(res,null,'    '));

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
&#13;
&#13;
&#13;