vue 中遍历数组对象 存到一个新数组里
vue 中遍历数组对象 存到一个新数组里(亲测可行!!!)数组对象 :data() {return {arr: [{ name: "a", age: 1 },{ name: "b", age: 2 },{ name: "c", age: 3 }],newArr: []};},使用 foreach 方法 :var str = "";this.arr.forEach(functi
·
vue 中遍历数组对象 存到一个新数组里(亲测可行!!!)
参考文档 : MDN-解构赋值
方法一 :
For of 迭代和解构
var people = [
{
name: 'Mike Smith',
family: {
mother: 'Jane Smith',
father: 'Harry Smith',
sister: 'Samantha Smith'
},
age: 35
},
{
name: 'Tom Jones',
family: {
mother: 'Norah Jones',
father: 'Richard Jones',
brother: 'Howard Jones'
},
age: 25
}
];
for (var {name: n, family: {father: f}} of people) {
console.log('Name: ' + n + ', Father: ' + f);
}
// "Name: Mike Smith, Father: Harry Smith"
// "Name: Tom Jones, Father: Richard Jones"
方法二 :
forEach :
data() {
return {
arr: [
{ name: "a", age: 1 },
{ name: "b", age: 2 },
{ name: "c", age: 3 }
],
newArr: []
};
},
mounted(){
var str = "";
this.arr.forEach(function(e) {
str += e.name + ",";
});
str = str.substring(0, str.length-1);
this.newArr = str.split(",");
window.console.log(str);
// 输出: a,b,c
window.console.log(this.newArr);
// 输出: ["a","b","c"]
}
更多推荐
已为社区贡献2条内容
所有评论(0)