简体   繁体   English

我想给我每个数组元素一个数字

[英]I want to give each of my array element a number

I have this code, a problem with is that it lists numbers for individual array elements instead of a whole array. 我有这段代码,一个问题是它列出了单个数组元素而不是整个数组的数字。

I did try other methods including .map but I failed. 我尝试了其他方法,包括.map,但失败了。

let names = 'Bilbo, Gandalf, Nazgul, Bob, Grim, Laniakea, Dzevid, Levan, George, What'; 让名字=“ Bilbo,Gandalf,Nazgul,Bob,Grim,Laniakea,Dzevid,Levan,George,What”;

let arr = names.split(', ');

for (let name of arr) {
for(let i =1; i < arr.length; i++)
  document.write( i + ` A message to ${name}.` + `<br>` ); // A message to Bilbo  (and other names)

}
  1. My results with this code is this: 使用此代码的结果是:

     1 A message to Bilbo. 2 A message to Bilbo. 3 A message to Bilbo. 4 A message to Bilbo. 5 A message to Bilbo. 6 A message to Bilbo. 7 A message to Bilbo. 8 A message to Bilbo. 9 A message to Bilbo. 1 A message to Gandalf. 2 A message to Gandalf. 3 A message to Gandalf. 4 A message to Gandalf. 5 A message to Gandalf. 6 A message to Gandalf. 7 A message to Gandalf. 8 A message to Gandalf. 9 A message to Gandalf. 1 A message to Nazgul. 2 A message to Nazgul. 3 A message to Nazgul. 4 A message to Nazgul. 5 A message to Nazgul. 6 A message to Nazgul. 7 A message to Nazgul. 8 A message to Nazgul. 9 A message to Nazgul. 1 A message to Bob. 2 A message to Bob. 3 A message to Bob. 4 A message to Bob. 5 A message to Bob. 6 A message to Bob. 7 A message to Bob. 8 A message to Bob. 9 A message to Bob. 1 A message to Grim. 2 A message to Grim. 3 A message to Grim. 4 A message to Grim. 5 A message to Grim. 6 A message to Grim. 7 A message to Grim. 8 A message to Grim. 9 A message to Grim. 1 A message to Laniakea. 2 A message to Laniakea. 3 A message to Laniakea. 4 A message to Laniakea. 5 A message to Laniakea. 6 A message to Laniakea. 7 A message to Laniakea. 8 A message to Laniakea. 9 A message to Laniakea. 1 A message to Dzevid. 2 A message to Dzevid. 3 A message to Dzevid. 4 A message to Dzevid. 5 A message to Dzevid. 6 A message to Dzevid. 7 A message to Dzevid. 8 A message to Dzevid. 9 A message to Dzevid. 1 A message to Levan. 2 A message to Levan. 3 A message to Levan. 4 A message to Levan. 5 A message to Levan. 6 A message to Levan. 7 A message to Levan. 8 A message to Levan. 9 A message to Levan. 1 A message to George. 2 A message to George. 3 A message to George. 4 A message to George. 5 A message to George. 6 A message to George. 7 A message to George. 8 A message to George. 9 A message to George. 1 A message to What. 2 A message to What. 3 A message to What. 4 A message to What. 5 A message to What. 6 A message to What. 7 A message to What. 8 A message to What. 9 A message to What. 

I want it to work in the following way: 我希望它以以下方式工作:

    1 A message to Bilbo
    2 A message to Gandalf
    3 A message to Nazgul
    4 A message to Bob
    5 A message to Grim
    6 A message to Laniakea
    7 A message to Dzevid
    8 A message to Levan
    9 A message to George
    10 A message to what

Not sure what you are trying to achieve. 不确定您要达到的目标。 But here is the code which can help. 但是这是可以提供帮助的代码。

let names = 'Bilbo, Gandalf, Nazgul, Bob, Grim, Laniakea, Dzevid, Levan, George, What';\
names = names.split(',');

names.forEach((name, index) => {
    document.write(`${index} A message to ${name.trim()}`);
})

In this example we firstly transform that long string into array of names. 在此示例中,我们首先将长字符串转换为名称数组。 Then using simple forEach handle each name. 然后使用简单的forEach处理每个名称。 Note that I use name.trim() for removing unnecessary spaces left after splitting. 请注意,我使用name.trim()删除拆分后剩余的不必要空间。

Actually there was no need to add for (let name of arr) { .... } . 实际上,不需要添加for (let name of arr) { .... } You can just remove that line. 您可以删除该行。 Other than that, you need to start for loop from i = 0 , as an array starts from index 0 , 除此之外,您需要从i = 0开始循环,因为数组从index 0开始,

let names = 'Bilbo, Gandalf, Nazgul, Bob, Grim, Laniakea, Dzevid, Levan, George, What';
let arr = names.split(', '); 

for(let i = 0; i < arr.length; i++){
  document.write( `${i+1}`  + ` A message to ${arr[i]}.` + `<br>` );
}

Why do you use a second loop ? 为什么要使用第二个循环? just set a counter like 像这样设置一个计数器

let names = 'Bilbo, Gandalf, Nazgul, Bob, Grim, Laniakea, Dzevid, Levan, George, What';

let arr = names.split(', ');
let count = 1;
for (let name of arr) {
 document.write( count + ` A message to ${name}.` + `<br>` );
 count++;
}

Or 要么

for (i = 1; i < arr.length; i++) { 
    document.write( i + ` A message to ${name}.` + `<br>` );
}

You can use the map for looping and you do not need second loop because arr is one-dimensional array 您可以使用地图进行循环,并且不需要第二次循环,因为arr是一维数组

 let names = 'Bilbo, Gandalf, Nazgul, Bob, Grim, Laniakea, Dzevid, Levan, George, What'; let arr = names.split(', '); arr.map((d, i)=>{ document.write( i+1 + ` A message to ${d}.` + `<br>` ); // A message to Bilbo (and other names) }) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 想要给每个元素不同的ID - Want to give different ID to each element 我想从数组中读取每个元素以及该元素的深度位置 - I want to read each element and depth position of that element from an array 如何为数组中的每个对象分配自己的密钥? - How can i give each of the objects in my array their own key? JavaScript - 从数组 1 中选取随机元素,我想从数组 2 中获取相同的元素编号 - JavaScript - picking random element from array 1, I want to get the same element number from array 2 对于数组中的每个元素,我希望它有另一个元素,每个元素都有独特的动作 - For each element inside an array I want it to have another for each that will have unique action 我有一个贯穿数组的 API,我想在每个数组元素上向客户端发送数据 - I have an API that runs through an array and I want to send data to the client on each array element 我想从 jquery 的 jason 数组中获取每天每个团队的编号 - I want to get number of each team numbers in each day from jason array in jquery 如果数组包含重复项,如何为数组中的每个元素赋予唯一的ID? - How to give each element in array an unique id if array consists of duplicats? 我的无序列表是从 json 文件呈现的,但我希望每个列表元素都有一个边框 - My unordered list is rendered from a json file but I want a border around each list element 我想给每个渲染的 todo 元素一个 id - I want to give an id to every todo element that renders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM