从数组中删除额外的“”

时间:2016-04-02 23:27:51

标签: javascript

我将在命令行重新编写一个简单的神经网络,所以我不会从这里开始遇到NPM废话!

问题是在阵列中添加了额外的“”。我出于个人原因拒绝写正则表达式。我们只是说它不重要。让我们继续......

var gulp = require('gulp');

gulp.task('default', function() {

    var trainingSet = "001111101011",
    neuralNetworkStrength = 3;

    var neuralNetwork = []; 

    // e)ssential for loop
    for (var e = 0; e <= (trainingSet.length / neuralNetworkStrength); e++){

        neuralNetwork[e]= trainingSet.substr(e*neuralNetworkStrength,neuralNetworkStrength);

    }


    console.log(neuralNetwork); // [ '001', '111', '101', '011', '' ]



}); // I will eventually remove gulp so I own rights to the code.

2 个答案:

答案 0 :(得分:4)

你的算法错了。您必须在循环中使用<运算符,而不是<=运算符。

完整代码:

var trainingSet = "001111101011",
neuralNetworkStrength = 3;

var neuralNetwork = []; 

// essential for loop
for (var e = 0; e < (trainingSet.length / neuralNetworkStrength); e++){

    neuralNetwork[e]= trainingSet.substr(e * neuralNetworkStrength,neuralNetworkStrength);

}


console.log(neuralNetwork);

答案 1 :(得分:2)

将{for循环中的<=替换为<。这应该可以解决你的问题。