let res =
[
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'cet4'
},
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'cet6',
},
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'kaoyan',
}
]
for (let i = 0; i < res.length; i++) {
if (res[i].type == 'cet4') {
let count = '1'
res.total = count // 要添加的属性
} else if (res[i].type == 'cet6') {
let count = '2'
res.total = count
} else if (res[i].type == 'kaoyan') {
let count = '3'
res[i].total = count
}
}
console.log(res)
res =
[
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'cet4',
total:'1'
},
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'cet6',
total:'2'
},
{
_id: 'd4107ab1624d09a60520d3a7711d2db6',
type: 'kaoyan',
total:'3'
}
]
同上; res[i].total = count 这段代码就行 你来社区一年了
let res = [ { _id: 'd4107ab1624d09a60520d3a7711d2db6', type: 'cet4' }, { _id: 'd4107ab1624d09a60520d3a7711d2db6', type: 'cet6', }, { _id: 'd4107ab1624d09a60520d3a7711d2db6', type: 'kaoyan', } ] for (let i = 0; i < res.length; i++) { if (res[i].type == 'cet4') { let count = '1' res[i].total = count // 要添加的属性 } else if (res[i].type == 'cet6') { let count = '2' res[i].total = count } else if (res[i].type == 'kaoyan') { let count = '3' res[i].total = count } } console.log(res)
你目前代码里面打印比返回结果快,但是展开打印结果应该是正常的。