Javascript对象数组获得单个值 [英] Javascript Array of objects get single value

查看:85
本文介绍了Javascript对象数组获得单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一系列对象:

var employees=[]
employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}

是否有一个数组函数可以让我获得一个属性一个数组,
例如:

Is there an array function that will allow me to get one property as an array, for example:

namesArray = employees.reduceToProperty('name'); // none existent function i made up!

// should return ["George","Edward","Christine","Sarah"]

我知道如何通过循环获得所需的结果,我只是希望存在一个数组函数或函数组合,可以在一行中执行此操作。

I know how get the desired result with a loop, I am just hoping for an array function or combination of functions exist that can do this in one line.

推荐答案

var names = employees.map(function(i) {
  return i.name;
});

names 现在是一个包含<的数组code> name - 对象的属性。

names is now an array containing the name-properties of the objects.

这篇关于Javascript对象数组获得单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆