ES总结
利用map方法方便获得对象数组中的特定属性值(属性值组成的数组)
利用map方法方便获得对象数组中的特定属性值们。例如下面这个例子:
var users = [
{name: "张三", "age":18},
{name: "李四", "age":12},
{name: "王五", "age":14},
];
var ages= users.map(function (user) { return user.age; });
console.log(ages.join(", ")); // 18,12,14
利用find查找数组中第一个符合条件的元素(属性值)
const arr = [{ "a": "a"}, { "b": "b" }, { "c": "c" }]
const item = arr.find(value => {
return value.a === "a"
})
console.log(item)//{a: "a"}
利用values,获取对象中的属性值们(属性值组成的数组)
m = {
a1 : 'hello',
a2 : 'world'
}
Object.values(m);//['hello','world']
利用arr.map(返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值)
var numbers = [4, 9, 16, 25];
console.log(numbers.map(Math.sqrt));//[2,3,4,5]