javascript - 在 javascript 中迭代整数,其中每个值等于一位数字

标签 javascript arrays loops integer iteration

我想使用 $.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/

相关文章:

javascript - 我想让vuejs中的图像可点击

c++ - 如何将两个负数数组合并为一个数组?

ruby-on-rails - 如何在 Ruby On Rails 中获取数组值

arrays - 功能方法 : Merge Two Arrays and Return a Tuple with common objects

javascript - 获取 contenteditable=true 数据

javascript - 使用 Angular 在 Enter 上调用查询过滤器

javascript - 函数循环的可能解决方案

Python - 高级参数嵌套循环

loops - 在Vim中自动添加/生成内容

javascript - 开发环境中与AJAX数据服务有关的PhoneGap环境变量?