简体   繁体   English

遍历javascript中的整数,其中每个值都等于一个数字

[英]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.

相关问题 返回一个字符串,其中每个数字重复的次数等于它的值 - return a string where each digit is repeated a number of times equals to its value 如何将一位数整数转换为两位数 [Javascript] - How to convert single digit integer into double digit [Javascript] 遍历JavaScript中的每个消息以进行循环? - Iterate over each message in JavaScript for loop? 遍历JavaScript中每个列表项的数组 - Iterate over an array for each list item in JavaScript 创建一个包含每个数字的数组,这些数组作为JavaScript中与整数分开的单独元素 - To create an array that contains each digit as separate element from integer in JavaScript Javascript中的空格是否等于整数0? - Is whitespace equals to integer 0 in Javascript? 在 JavaScript/p5 中检查 integer 值的最后一位 - Checking the last digit of an integer value in JavaScript/p5 Javascript forEach,其中value等于不同数组中的value - Javascript forEach where value equals value in different array 如何迭代javascript行分隔的字符串,检查分号分隔的第一个值,并加入值相等的行? - How to iterate over a javascript line delimited string, check the semi-colon delimited first value, and join lines where value is equal? 如何遍历对象数组然后删除JavaScript中的每个元素 - How to iterate over an array of object then remove each element in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM