js每隔两条(自定义条数)数据,则添加一个空的 item(这个也可以新增你指定的数据),但是最后一条则不需要新增条数
let temp = []
const newData = []
for (let i = 0; i < temp.length; i++) {
newData.push(temp[i])
if ((i + 1) % 2 === 0 && i < temp.length - 1) {
newData.push({
'shippingCompanyId': '',
'shippingCompanyName': '',
'deliverMonth': '',
'effectRate': '',
'deliverCount': '',
'transportType': '',
'serialNo': ''
})
}
}
原数据
全部代码
let temp = [
{
"shippingCompanyId": 176,
"shippingCompanyName": "德立迅",
"deliverMonth": "2023-07-15~2023-07-16",
"effectRate": 57.14,
"deliverCount": 28,
"transportType": "空派",
"serialNo": 1
},
{
"shippingCompanyId": 176,
"shippingCompanyName": "德立迅",
"deliverMonth": "2023-07-17~2023-07-22",
"effectRate": 0,
"deliverCount": 0,
"transportType": "空派",
"serialNo": 2
},
{
"shippingCompanyId": 113,
"shippingCompanyName": "深圳筋斗云供应链有限公司",
"deliverMonth": "2023-07-23~2023-07-28",
"effectRate": 0,
"deliverCount": 0,
"transportType": "空派",
"serialNo": 3
},
{
"shippingCompanyId": 113,
"shippingCompanyName": "深圳筋斗云供应链有限公司",
"deliverMonth": "2023-07-15~2023-07-16",
"effectRate": 0,
"deliverCount": 5,
"transportType": "空派",
"serialNo": 1
}
]
const newData = []
for (let i = 0; i < temp.length; i++) {
newData.push(temp[i])
if ((i + 1) % 2 === 0 && i < temp.length - 1) {
newData.push({
'shippingCompanyId': '',
'shippingCompanyName': '',
'deliverMonth': '',
'effectRate': '',
'deliverCount': '',
'transportType': '',
'serialNo': ''
})
}
}
console.log('newData1', JSON.parse(JSON.stringify(newData)))