如何使用一个数组过滤对象数组并创建另一个数组? -Javascript

时间:2019-02-15 14:47:01

标签: javascript arrays

这是我的代码

 var people = [
          {name:'Sam', age: 22},
          {name:'Pam', age: 24},
          {name:'Adam', age: 25},
          {name:'Sam', age: 26},
          {name:'Pam', age: 28},
          {name:'Pam', age: 26}
  ];

  var nameFilter = ['Sam', 'Pam'];

使用姓名,我想获得相应的年龄

我应该以年龄数组的形式获取所有这些值

  

[22,26,24,28,26]

我不想要唯一的值。

使用Vanilla JS实现此目的的最简单方法是什么。我不想使用Lodash或Jquery

5 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.filter()

  

filter()方法将创建一个新数组,其中包含所有通过提供的功能实现的测试的元素。

Array.prototype.includes()

  

includes()方法确定数组的条目中是否包含某个值,并根据需要返回true或false。

Array.prototype.map()

  

map()方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

var people = [{name:'Sam', age: 22},
  {name:'Pam', age: 24},
  {name:'Adam', age: 25},
  {name:'Sam', age: 26},
  {name:'Pam', age: 28},
  {name:'Pam', age: 26}
];

var nameFilter = ['Sam', 'Pam'];
var res = people.filter(p => nameFilter.includes(p.name)).map(p => p.age)
console.log(res);

答案 1 :(得分:1)

您可以使用filter() includes()map()

var people = [{name:'Sam', age: 22},
          {name:'Pam', age: 24},
          {name:'Adam', age: 25},
          {name:'Sam', age: 26},
          {name:'Pam', age: 28},
          {name:'Pam', age: 26}

  ];

  var nameFilter = ['Sam', 'Pam'];
let ages = people.filter(person => nameFilter.includes(person.name)).map(x => x.age);
console.log(ages);

答案 2 :(得分:0)

var people = [{name:'Sam', age: 22},
          {name:'Pam', age: 24},
          {name:'Adam', age: 25},
          {name:'Sam', age: 26},
          {name:'Pam', age: 28},
          {name:'Pam', age: 26}

  ];

  var nameFilter = ['Sam', 'Pam'];

function getValues(array, aname) {
  var tmp = [];
  for (var i = 0; i < array.length; i++) {
     var obj = array[i];
     for (var j = 0; j < aname.length; j++) {
      var name = aname[j];
      if (obj.name === name) //console.log(obj.age);
        tmp.push(obj.age);
    }
  }
  return tmp;
}

console.log(getValues(people, nameFilter));

我这样做了,告诉我是否可行。

答案 3 :(得分:0)

您可以使用filter类型的Array方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

然后使用map提取年龄。

在您的示例中,您将执行以下操作:

people
  .filter(e => nameFilter.indexOf(e.name) >= 0)
  .map(e => e.age)

答案 4 :(得分:0)

使用filtermap可能是更易读的方法。但是,如果要一次迭代而不是两次迭代,则也可以使用reduce,如下所示:

let ages = people.reduce(
  (acc, {name, age}) => (nameFilter.includes(name) && acc.push(age), acc)
  , []);

console.log(ages); // [ 22, 24, 26, 28, 26 ]

请注意,由于您拥有nameFilter作为数组,因此includes的Big-O可能处于最坏的情况O(n);如果将其替换为普通对象,则将具有O(1)。像这样:

  let nameFilter = {"Sam": true, "Pam": true};

  let ages = people.reduce(
    (acc, {name, age}) => (nameFilter[name] && acc.push(age), acc)
    , []);

并且,如果您对唯一值及其在原始数组中出现的次数感兴趣,则还可以将返回值从数组更改为对象:

  let nameFilter = {"Sam": true, "Pam": true};

  let ages = people.reduce(
    (acc, {name, age}) => (nameFilter[name] && (acc[age] = -~acc[age]), acc)
    , {});

  console.log(ages) // { 22: 1, 24: 1, 26: 2, 28: 1 }

在这种情况下,您还可以快速检查是否有某个年龄的人,或有多少人:

  var ageInput = "21";
  if (!ages[ageInput]) {
     console.log(`No one ${ageInput} years old!`)
  } else if ages[ageInput] === 1 {
     console.log(`Only one person is ${ageInput} years old`);
  } else {
     console.log(`There are ${age[ageInput]} people that are ${ageInput} years old`)
  }

当然,这实际上取决于您需要如何处理获取的数据。