我想使用 $.each()
循环来迭代整数中的数字。
该整数是作为另一个数组的索引生成的,尽管我不确定这是否会产生影响。
Javascript
var words = [ "This", "is", "an", "array", "of", "words","with", "a", "length", "greater", "than", "ten." ];
var numbers = [];
$.each(words, function(i,word) {
$.each(i, function(index,value) {
$(numbers).append(value);
});
});
我希望数组numbers
等于以下数组:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1 ]
其中最后四个条目 [ ... 1, 0, 1, 1 ]
是通过条目 [ ... "than", "的索引迭代生成的十。” ]
在 words
数组中。
最佳答案
let words = [ "This", "is", "an", "array", "of", "words","with", "a", "length", "greater", "than", "ten." ];
let numbers = words.map((w, i) => i.toString().split('').map(Number)) //map into array of digits
numbers = Array.prototype.concat.call(...numbers); //concat them all
console.log(numbers);
关于javascript - 在 javascript 中迭代整数,其中每个值等于一位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592376/