Javascript-多维数组对象到数组 [英] Javascript - Multidimensional array object to array
问题描述
我是Node.js的新手,我想从看起来像这样的对象中访问一组特定的信息:
I'm new to Node.js and I want to access a particular set of information from an object that looks like this:
userData: [
{
value: false,
id: 1,
title: 'Hello World',
date: '17 February 2018 - 06:27:51 PM',
status: 'Processing'
},
{
value: false,
id: 2,
title: 'Hello People',
date: '17 February 2018 - 06:27:48 PM',
status: 'Active'
},
{
value: false,
id: 3,
title: 'Hello Canary',
date: '17 February 2018 - 06:27:44 PM',
status: 'Expired'
}
]
现在我想返回一个仅包含"title"的数组
Now I want to return an array that consist of only "title"
示例: ['Hello World','Hello people','Hello Canary']
我尝试了 Object.values(obj)
和 Object.keys(yourObject).map(key => yourObject [key])
没有任何运气.谁能帮我吗?
I have tried Object.values(obj)
and Object.keys(yourObject).map(key => yourObject[key])
without any luck. Can anyone please help me?
更新:我正在使用ES6,并且想优化代码以提高性能
Update: I'm using ES6 and would like to optimize my code for performance
推荐答案
让标题= userData.map(x => x.title);
使用 Array.prototype.map时与数组.它返回一个数组,该数组由您传递的函数的返回值组成,并在该数组的每个元素上调用.因此,上面的示例在用于包含 .title
属性的对象数组时,将返回一个包含原始数组中每个对象标题的新数组.
When using Array.prototype.map with an array. It returns an array made up of the return value of the function you pass it, called on each element of the array. So the above example, when used on your array of objects containing the .title
property, will return a new array containing the titles of each object in the original array.
这篇关于Javascript-多维数组对象到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!