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

时间:2017-01-24 09:23:35

标签: javascript arrays data-structures iteration reduce

我有一些代码工作得很好,但我遇到了问题。

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

在这种情况下,第二条记录为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);

我该怎么做?

6 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

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

答案 2 :(得分:0)

您可以在for循环内创建新变量,如果不是0,则使用push()

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

thCount[l] = 0;将导致向thCount添加元素,无论您的条件是if (data.cars[l].thsub)

您可以使用另一个变量,只有在我们想要向数组添加内容时才增加它:

&#13;
&#13;
	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);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

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

&#13;
&#13;
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)
&#13;
&#13;
&#13;

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

答案 5 :(得分:0)

还可以考虑使用嵌套reduce方法,例如如下......

&#13;
&#13;
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);
&#13;
&#13;
&#13;

相关问题