[英]Iterate over integer in javascript where each value equals a single digit
I would like to use the $.each()
loop to iterate over digits in an integer. 我想使用
$.each()
循环遍历整数中的数字。 The integer is produced as the index of another array, though I'm not sure that this makes a difference. 整数是作为另一个数组的索引产生的,尽管我不确定这会有所作为。
Javascript Java脚本
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);
});
});
I would like the array numbers
to equal the following array: 我希望数组
numbers
等于以下数组:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0, 1, 1 ]
Where the last four entries, [ ... 1, 0, 1, 1 ]
are generated through iteration over the indexes for the entries [ ... "than", "ten." ]
最后四个条目
[ ... 1, 0, 1, 1 ]
1,0,1,1 [ ... 1, 0, 1, 1 ]
是通过对条目[ ... "than", "ten." ]
的索引进行迭代生成的[ ... "than", "ten." ]
[ ... "than", "ten." ]
in the words
array. [ ... "than", "ten." ]
在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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.