总要记录一些数组操作,在实际工作中用到的,以后会慢慢都整理到这里,会了方便自己,也是方便到别人
更新数组其中一个对象 set方法
this.$set(this.dataListPie1, 0, {
name: "优良",
value: res.result[0].rate.toFixed(0),
});
数组对象赋值
把对象里面的值,赋到数姐里面的某个值里面去
原数组
schoolList: [
{
sum: 0,
attrName: "school_cnt"
},
{
sum: 0,
attrName: "teacher_cnt"
},
{
sum: 0,
attrName: "student_cnt"
}
],
数据对象
result:{
school_cnt: 54,
student_cnt: 65621,
teacher_cnt: 4327
}
方法
this.schoolList = this.schoolList.map(item => {
item.sum = result[item.attrName]
return item
})
数组 数组赋值
把数组里面某个值,赋到另一个数姐里面的某个值里面去
原数组
recoveryList: [
{
sum: 0,
attrName: "幼儿园"
},
{
sum: 0,
attrName: "小学"
}
],
数据数组
result:[
{
capital: "120"
etl_time: null
executor_name: ""
gmt_create: "2022-08-10 20:07:22"
gmt_modified: null
id: 3
is_deleted: 0
receive_time: "2021"
school_num: 4
school_type: "幼儿园"
}
]
方法
this.recoveryList = this.recoveryList.map(item => {
let result = this.result.find(item2 => {
return item2.school_type === item.attrName
})
item.sum = result ? result.school_num : 0
return item;
})