js 把数组中每个元素的某个字段取出

方法一:map()

let cities =  [
  {city:"北京",bOn:false},
  {city:"上海",bOn:false},
  {city:"重庆",bOn:false},
  {city:"广州",bOn:false},
  {city:"西安",bOn:false}
];
 let city = cities.map((item) =>{
   return item.city
 });
 console.log('city == ',city);

 

方法二:foreach()(你用for也行,哈哈哈)
let cities =  [
  {city:"北京",bOn:false},
  {city:"上海",bOn:false},
  {city:"重庆",bOn:false},
  {city:"广州",bOn:false},
  {city:"西安",bOn:false}
];
 let city = [];
 cities.forEach((item)=>{
    city.push(item.city);
 });
 console.log('city == ',city);

 

posted @ 2019-02-22 15:56  mei1234!  阅读(5385)  评论(0编辑  收藏  举报