Javascript空时跳到下一个记录代码修改
我有一些代码工作得很好,但我有一个问题 基本上,当它到达一个空的记录时,它会向数组中添加一个0 在本例中,第二条记录为空,因此我得到: [10,0,20] 我需要它做的是,如果thsub为NULL,则不向数组中添加任何内容,并继续下一条记录 因此,在这种情况下,理想的结果是: [10,20] 以下是完整的代码:Javascript空时跳到下一个记录代码修改,javascript,arrays,data-structures,iteration,reduce,Javascript,Arrays,Data Structures,Iteration,Reduce,我有一些代码工作得很好,但我有一个问题 基本上,当它到达一个空的记录时,它会向数组中添加一个0 在本例中,第二条记录为空,因此我得到: [10,0,20] 我需要它做的是,如果thsub为NULL,则不向数组中添加任何内容,并继续下一条记录 因此,在这种情况下,理想的结果是: [10,20] 以下是完整的代码: var数据={ “汽车”:[{ “id”:“1”, “名称”:“名称1”, “thsub”:[{ “id”:“11”, “名称”:“子1”, “统计数据”:{ “项目”:5, }, “
var数据={
“汽车”:[{
“id”:“1”,
“名称”:“名称1”,
“thsub”:[{
“id”:“11”,
“名称”:“子1”,
“统计数据”:{
“项目”:5,
},
“离子”:空
}, {
“id”:“22”,
“名称”:“子2”,
“统计数据”:{
“项目”:5,
},
“翻译”:空
}],
“图像”:空
},
{
“id”:“2”,
“名称”:“名称2”,
“thsub”:null,//这将中断代码
“图像”:空
},
{
“id”:“54”,
“命名”:“命名某物”,
“thsub”:[{
“id”:“65”,
“名称”:“子1”,
“统计数据”:{
“项目”:10,
},
“离子”:空
}, {
“id”:“22”,
“名称”:“子2”,
“统计数据”:{
“项目”:10,
},
“翻译”:空
}],
“图像”:空
}
]
}
var-thCount=[];
对于(var l=0,m=data.cars.length;l console.log(thCount)
您可以在for循环内部创建新变量,如果不是0,则使用push()
var data={“cars”:[{“id”:“1”,“name”:“name 1”,“thsub”:[{“id”:“11”,“name”:“sub 1”,“stats”:{“items”:5},“ions”:null},{“id”:“22”,“name”:“sub 2”,“stats”:{“items”:5},“translations”:null},{“id”:“2”,“name”:“name”:“name”:“name 2”,“thsub,“离子”:null},{“id”:“22”,“名称”:“sub2”,“统计”:{“items”:10},“翻译”:null}],“图像”:null}]}
var-thCount=[];
对于(var l=0,m=data.cars.length;l console.log(thCount);
如果设置了thsub
,则只能推送一个值
var data={cars:[{id:“1”,name:“name 1”,thsub:[{id:“11”,name:“sub 1”,stats:{items:5,},ions:null},{id:“22”,name:“sub 2”,stats:{items:5,},translations:null},{id:“2”,name:“name:null},{id:“54”,name:“name:“name”,thsub id:[{65”,name:“sub 1”,stats:{items:10,},ions:null},{id:“22”,name:“sub 2”,stats:{items:10,},translations:null}],image:null}]},
thCount=[];
对于(var l=0,m=data.cars.length;l
您需要添加变量,然后仅当存在某些内容时才推送到数组
var data = {
"cars": [{
"id": "1",
"name": "name 1",
"thsub": [{
"id": "11",
"name": "sub 1",
"stats": {
"items": 5,
},
"ions": null
}, {
"id": "22",
"name": "sub 2",
"stats": {
"items": 5,
},
"translations": null
}],
"image": null
},
{
"id": "2",
"name": "name 2",
"thsub": null, //this will break the code
"image": null
},
{
"id": "54",
"name": "name something",
"thsub": [{
"id": "65",
"name": "sub 1",
"stats": {
"items": 10,
},
"ions": null
}, {
"id": "22",
"name": "sub 2",
"stats": {
"items": 10,
},
"translations": null
}],
"image": null
}
]
}
var thCount = [];
for (var l = 0, m = data.cars.length; l < m; l++) {
if (data.cars[l].thsub) {
var tmp = 0;
for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) {
if (data.cars[l].thsub[i].stats) {
tmp+=data.cars[l].thsub[i].stats.items;
}
thCount.push(tmp);
}
}
}
console.log(thCount);
var数据={
“汽车”:[{
“id”:“1”,
“名称”:“名称1”,
“thsub”:[{
“id”:“11”,
“名称”:“子1”,
“统计数据”:{
“项目”:5,
},
“离子”:空
}, {
“id”:“22”,
“名称”:“子2”,
“统计数据”:{
“项目”:5,
},
“翻译”:空
}],
“图像”:空
},
{
“id”:“2”,
“名称”:“名称2”,
“thsub”:null,//这将中断代码
“图像”:空
},
{
“id”:“54”,
“命名”:“命名某物”,
“thsub”:[{
“id”:“65”,
“名称”:“子1”,
“统计数据”:{
“项目”:10,
},
“离子”:空
}, {
“id”:“22”,
“名称”:“子2”,
“统计数据”:{
“项目”:10,
},
“翻译”:空
}],
“图像”:空
}
]
}
var-thCount=[];
对于(var l=0,m=data.cars.length;l
行thCount[l]=0;
将导致在thCount中添加一个元素,无论您的条件如何if(data.cars[l].thsub)
您可以使用另一个变量,仅当我们要向数组中添加某些内容时才增加该变量:
var数据={
“汽车”:[{
“id”:“1”,
“名称”:“名称1”,
“thsub”:[{
“id”:“11”,
“名称”:“子1”,
“统计数据”:{
“项目”:5,
},
“离子”:空
}, {
“id”:“22”,
“名称”:“子2”,
“统计数据”:{
“项目”:5,
},
“翻译”:空
}],
“图像”:空
},
{
“id”:“2”,
“名称”:“名称2”,
“thsub”:null,//这将中断代码
“图像”:空
},
{
“id”:“54”,
“命名”:“命名某物”,
“thsub”:[{