Filter array of objects on all properties value

后端 未结 3 1285
心在旅途
心在旅途 2021-01-19 02:39

I am really surprised I haven\'t been able to find anything related to my question. I am looking for a fast way to filter my array of objects based on a user text input.

3条回答
  •  粉色の甜心
    2021-01-19 03:33

    You could use Object.keys() and some() instead.

    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)

提交回复
热议问题