简体   繁体   English

从 Java 脚本/类型脚本中的对象数组中获取属性数组

[英]Get array of properties from Array of Objects in Java script / Type script

I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"]我有以下数组如何获取年龄大于 18 的姓名列表。所以输出应该是: ["Anna", "Bob"]

  friends = [{
  name: 'Anna',
  books: ['Bible', 'Harry Potter'],
  age: 21
}, {
  name: 'Bob',
  books: ['War and peace', 'Romeo and Juliet'],
  age: 26
}, {
  name: 'Alice',
  books: ['The Lord of the Rings', 'The Shining'],
  age: 18
}]

I have tried below我在下面试过

let names = friends.map((item) => {if (item.age > 18){ return item.name}});

but i got below output但我低于输出

["Anna", "Bob", undefined]

Use Array.filter() before Array.map() since map always returns a value and you'll get undefined if you don't specify return statement.Array.filter()之前使用Array.map()因为map总是返回一个值,如果你不指定return语句你会得到undefined For 3-elements array there's always going to be 3-elements result.对于 3 元素数组,总是会有 3 元素结果。

 let friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, { name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 }, { name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 }] let result = friends.filter(f => f.age > 18).map(f => f.name); console.log(result);

You can use Array.prototype.reduce您可以使用Array.prototype.reduce

 let friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, { name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 }, { name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 }]; let ans = friends.reduce((acc, val) => (val.age > 18 && acc.push(val.name),acc), []); console.log(ans);

You are getting undefined as the last item in names array because map function is used to transform each item in the array it is called on and then return each transformed value.您将undefined作为names数组中的最后一项,因为map函数用于转换调用它的数组中的每个项,然后返回每个转换后的值。

If you call it on an array of length 3, map function will return an array of same length.如果在长度为 3 的数组上调用它, map函数将返回一个相同长度的数组。 Since you are only returning those names from map function where age is greater than 18, last object where age is not greater than 18, you are not transforming it and returning its name property, so you get undefined .由于您仅从年龄大于 18 的map函数返回那些年龄大于 18 的名称,最后一个年龄不大于 18 的对象,因此您undefined对其进行转换并返回其 name 属性,因此您会得到undefined

One way to achieve the desired result is to use filter function to filter out the object where age is not greater than 18 and then call map function on that filtered array.实现预期结果的一种方法是使用filter函数过滤掉年龄不大于 18 的对象,然后在该过滤后的数组上调用map函数。

In above approach, you code will first iterate over the friends array and then iterate over the filtered array.在上述方法中,您的代码将首先迭代friends数组,然后迭代过滤后的数组。

You can achieve desired result and iterate over friends array only once using reduce function您可以使用reduce函数获得所需的结果并仅迭代一次friends数组

 const friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, { name: 'Bob', books: ['War and peace', 'Romeo and Juliet'], age: 26 }, { name: 'Alice', books: ['The Lord of the Rings', 'The Shining'], age: 18 }]; const res = friends.reduce((acc, curr) => (curr.age > 18 && acc.push(curr.name), acc), []); console.log(res);

let friends = [{   name: 'Anna',   books: ['Bible', 'Harry Potter'],   age: 21 }, {   name: 'Bob',   books: ['War and peace', 'Romeo and Juliet'],   age: 26 }, {   name: 'Alice',   books: ['The Lord of the Rings', 'The Shining'],   age: 18 }]

let ages = friends.filter((friends) => friends.age>18)

let finalResult = ages.map(fun => ages.name)

console.log(finalResult)

Use Array.filter()使用 Array.filter()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM