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

  • Post author:
  • Post category:其他


方法一: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);

转载于:https://www.cnblogs.com/mei1234/p/10418897.html