js提取对象数组中的某一个属性组成新数组

例:

arrold = [

  {name: "name1", age: "1"},
  {name: "name2", age: "2"},
  {name: "name3", age: "3"},
  {name: "name4", age: "4"},
  {name: "name5", age: "5"},
  {name: "name6", age: "6"}
]

arrnew = ["name1","name2","name3","name4","name5","name6"]

方法一:

1 let arr = []
2 arrold.forEach(e => {
3    arrnew.push(e.name)
4 })

方法二:

1 let arrnew = arrold.map(obj => {
2    return obj.name;
3 })

补充:

1 let arrnew = arrold.map(item => {
2      return Object.assign({},{'name':item.name})
3 })

关于Object.assign(target,sources)方法:拷贝源对象且可枚举的属性到目标对象,如果目标对象中的属性具有相同的键,则属性将被源中的属性覆盖。例:

1 const object1 = {
2   a: 1,
3   b: 2,
4   c: 3
5 };
6 
7 // object2: a=1, b=2, c=3, d=5
8 const object2 = Object.assign({c: 4, d: 5}, object1);

 

posted @ 2021-06-02 09:12  lijjj  阅读(6719)  评论(0编辑  收藏  举报