简体   繁体   English

Javascript当为null时跳到下一个记录代码修改

[英]Javascript When null skip to the next record code modification

I have some code that's working great but I have a problem with. 我有一些运行良好的代码,但是有问题。

Basically when it get to a record that's NULL it's adding a 0 to the array... 基本上,当到达记录为NULL时,它会在数组中添加0 ...

In this case the second record is NULL, so I'm getting: 在这种情况下,第二条记录为NULL,因此我得到:

[10, 0, 20] [10,0,20]

What I need it to do is that if thsub is NULL then add nothing to the array and continue to the next record. 我需要做的是,如果thsub为NULL,则不向数组添加任何内容,并继续到下一条记录。

So the desired result in this case would be: 因此,这种情况下的预期结果将是:

[10, 20] [10,20]

Here's the full code: 这是完整的代码:

 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++) { thCount[l] = 0; if (data.cars[l].thsub) { for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) { if (data.cars[l].thsub[i].stats) { thCount[l]+=data.cars[l].thsub[i].stats.items; } } } } console.log(thCount); 

How can I do this? 我怎样才能做到这一点?

You could push only a value, if thsub is set. 如果设置了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 }], image: null }, { id: "2", name: "name 2", thsub: null, 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 }] }, thCount = []; for (var l = 0, m = data.cars.length; l < m; l++) { if (data.cars[l].thsub) { thCount.push(0); for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) { if (data.cars[l].thsub[i].stats) { thCount[thCount.length - 1] += data.cars[l].thsub[i].stats.items; } } } } console.log(thCount); 

solution

you need to add variable and then push to array only if there is something. 您需要添加变量,然后仅在存在某些情况时才推送到数组。

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);

You can create new variable inside for loop and use push() instead if its not 0. 您可以在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}],"image":null},{"id":"2","name":"name 2","thsub":null,"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++) { var count = 0 if (data.cars[l].thsub) { for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) { if (data.cars[l].thsub[i].stats) { count += data.cars[l].thsub[i].stats.items; } } } if (count != 0) thCount.push(count) } console.log(thCount); 

The line thCount[l] = 0; thCount[l] = 0; will cause to add an element to thCount regardless of your condition if (data.cars[l].thsub) if (data.cars[l].thsub)无论您的条件如何,都会导致向thCount添加元素

You can use another variable and increase it only when we want to add something to our array: 您可以使用另一个变量并仅在我们想向数组中添加某些内容时才增加它:

  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, k = -1, m = data.cars.length; l < m; l++) { if (data.cars[l].thsub) { thCount[++k] = 0; for (var i = 0, j = data.cars[l].thsub.length; i < j; i++) { if (data.cars[l].thsub[i].stats) { thCount[k] += data.cars[l].thsub[i].stats.items; } } } } console.log(thCount); //alert(thCount); 

There has been some answers about how to fix the loop, so the basic problem is with the thCount[l] = 0 line. 关于如何修复循环有一些答案,因此基本问题是thCount[l] = 0行。 You can also use higher order functions to loop through the object which results in a better readable code in my opinion: 您还可以使用更高阶的函数来遍历对象,以使我认为代码可读性更好:

 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 = data.cars.filter(function(car){ return car.thsub; }).map(function(car){ return car.thsub.reduce(function(a,b){ return a.stats.items + b.stats.items; }); }); console.log(thCount) 

First, you filter out all objects that have no thsub . 首先, 过滤掉所有没有thsub对象。 Then you map those entries to new values. 然后,您这些条目映射到新值。 You get those values with the reduce -function, which sums up all the items -values in the stats . 您可以通过reduce function获得这些值,该函数将stats中的所有items values求和。

One also might consider a nested reduce approach eg like the following ... 人们还可以考虑使用嵌套的reduce方法,例如以下内容...

 var data = { "cars": [{ "id": "1", "name": "name 1", "thsub": [{ "id": "11", "name": "sub 1", "stats": { "items": 5 }, "translations": null }, { "id": "22", "name": "sub 2", "stats": { "items": 5 }, "translations": null }], "image": null }, { "id": "2", "name": "name 2", "thsub": null, "image": null }, { "id": "54", "name": "name something", "thsub": [{ "id": "65", "name": "sub 1", "stats": { "items": 10 }, "translations": null }, { "id": "22", "name": "sub 2", "stats": { "items": 10 }, "translations": null }], "image": null }] }; var thCount = data.cars.reduce(function (collector, carItem) { var thSubs = carItem.thsub; //if (Array.isArray(thSubs)) { ... } if ((thSubs != null) && (thSubs.length >= 1) && Number.isFinite(thSubs.length)) { collector.push( thSubs.reduce(function (count, subItem) { return (count + subItem.stats.items); }, 0) ); } return collector; }, []); console.log(thCount); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM