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"]
}

其它方法

分割图

查看更多 数组 的方法

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐