js 把数组中每个元素的某个字段取出
作者:互联网
方法一:map()
let cities = [ {city:"北京",bOn:false},let city = cities.map((item) =>{ return item.city }); console.log('city == ',city);
{city:"上海",bOn:false},
{city:"重庆",bOn:false},
{city:"广州",bOn:false},
{city:"西安",bOn:false}
];
方法二: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);
标签:city,false,数组,js,item,let,bOn,某个,cities 来源: https://www.cnblogs.com/mei1234/p/10418897.html