【问题标题】:Javascript When null skip to the next record code modificationJavascript null 时跳转到下一条记录代码修改
【发布时间】:2026-01-31 03:55:02
【问题描述】:

我有一些运行良好的代码,但我遇到了问题。

基本上,当它到达一个为 NULL 的记录时,它会在数组中添加一个 0...

在这种情况下,第二条记录为 NULL,所以我得到:

[10, 0, 20]

我需要它做的是,如果 thsub 为 NULL,则不向数组中添加任何内容并继续下一条记录。

所以在这种情况下,期望的结果是:

[10, 20]

这是完整的代码:

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

我该怎么做?

【问题讨论】:

    标签: javascript arrays data-structures iteration reduce


    【解决方案1】:

    如果设置了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);

    【讨论】:

      【解决方案2】:

      solution

      你需要添加变量,然后只有在有东西的时候才会推送到数组。

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

      【讨论】:

        【解决方案3】:

        您可以在 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);

        【讨论】:

          【解决方案4】:

          thCount[l] = 0; 行将导致向 thCount 添加一个元素,无论您的条件如何if (data.cars[l].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, //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);

          【讨论】:

            【解决方案5】:

            已经有一些关于如何修复循环的答案,所以基本问题在于thCount[l] = 0 行。您还可以使用高阶函数来遍历对象,这在我看来会产生更好的可读性代码:

            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)

            首先,您将filter 剔除所有没有thsub 的对象。然后你 map 将这些条目设置为新值。您可以使用reduce-函数获取这些值,该函数汇总了stats 中的所有items-值。

            【讨论】:

              【解决方案6】:

              也可以考虑嵌套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);

              【讨论】: