基于javascript中的另一个数组过滤对象数组

IT技术 javascript arrays filter
2021-01-20 17:57:04

给定一个对象数组:

people = [
    {id: "1", name: "abc", gender: "m", age:"15" },
    {id: "2", name: "a", gender: "m", age:"25" },
    {id: "3", name: "efg", gender: "f", age:"5" },
    {id: "4", name: "hjk", gender: "m", age:"35" },
    {id: "5", name: "ikly", gender: "m", age:"41" },
    {id: "6", name: "ert", gender: "f", age:" 30" },
    {id: "7", name: "qwe", gender: "f", age:" 31" },
    {id: "8", name: "bdd", gender: "m", age:" 78" },
]

以及一系列想要的 ID:

id_filter = [1,4,5,8]

如何过滤数组people以返回在id_filterand of 中定义的目标 ID gender=m

6个回答

最简单的过滤方式是使用数组的filter()函数,类似这样:

people.filter(person => id_filter.includes(person.id))
如果我想添加另一个性别应该是 == m 的条件怎么办?
2021-03-26 17:57:04
people.filter((person) => person.gender == 'm')
2021-03-27 17:57:04
它真的是people.filter((person) => id_filter.includes(Number(person.id)) && person.gender === "m")people.filter(({ id, gender }) => id_filter.includes(Number(id)) && gender === "m")
2021-03-28 17:57:04

您可以使用array.filter()很少的条件来获得所需的输出。我也更正了你的 JSON。

var filtered = people.filter(function(item) {
        return id_filter.indexOf(item.id) !== -1 && item.gender==='m';
});

演示

var  people =[
  { "id": 1, "name": "abc", "gender": "m","age": "15" },
  { "id": 2, "name": "a", "gender": "m", "age": "25"  },
  { "id": 3,"name": "efg", "gender": "f","age": "5" },
  { "id": 4,"name": "hjk","gender": "m","age": "35" },
  {  "id": 5, "name": "ikly","gender": "m","age": "41" },
  { "id": 6, "name": "ert", "gender": "f", "age": "30" },
  { "id": 7, "name": "qwe", "gender": "f", "age": "31" },
  { "id":8, "name": "bdd",  "gender": "m", "age": " 8" }
];
var id_filter = [1,4,5,8];
var filtered = people.filter(function(item) {
    return id_filter.indexOf(item.id) !== -1 && item.gender==='m';
});
console.log(filtered);

Array.includes()功能:

var people = [
    {id : "1", name : "abc", gender : "m", age :"15" }, {id : "2", name : "a", gender : "m", age :"25" },
    {id : "3", name : "efg", gender : "f", age :"5" },  {id : "4", name : "hjk", gender : "m", age :"35" },
    {id : "5", name : "ikly", gender : "m", age :"41" }, {id : "6", name : "ert", gender : "f", age :" 30" },
    {id : "7", name : "qwe", gender : "f", age :" 31" }, {id : "8", name : "bdd", gender : "m", age :" 78" }
], 
    id_filter = [1,4,5,8],
    result = people.filter((o) => id_filter.includes(+o.id) && o.gender == "m");       
	
console.log(result);


  • +o.id-+此处用于将数字字符串转换为数字

您可以通过以下代码实现:

const filtered_people = people.filter(function(person){
    return id_filter.includes(person.id) && person.gender === 'm';
});

只需确保每个人的 id 是整数而不是字符串,如您的示例所示。否则,includes() 函数将不匹配。此外,您的people数组有内部语法问题。所以,最终的代码看起来像这样:

const people = [
    {id: 1, name: "abc", gender: "m", age:15},
    {id: 2, name: "a", gender: "m", age: 25},
    {id: 3, name: "efg", gender: "f", age: 5},
    {id: 4, name: "hjk", gender: "f", age: 35},
    {id: 5, name: "ikly", gender: "m", age: 41},
    {id: 6, name: "ert", gender: "f", age: 30},
    {id: 7, name: "qwe", gender: "f", age: 31},
    {id: 8, name: "bdd", gender: "m", age: 78}
]
const id_filter = [1,4,5,8]
const filtered_people = people.filter((person) => id_filter.includes(person.id) && person.gender === 'm')
console.log(filtered_people)

我希望这可以帮助你。祝你好运。

对于这种情况,您可以使用 filter 和 include 函数,因为您的 id 是字符串,因此需要在使用前进行解析。

var result = people.filter((person) => (id_filter.includes(parseInt(person.id)) && person.gender ==='m'))