Javascript 输出map()的索引

Javascript 输出map()的索引,javascript,Javascript,我想使用map()输出每个元素的索引 const arr=[1,2,3,4,5,6,7,8,9]; const div=document.querySelector(“.container”) div.innerHTML=arr.map((索引,val)=>“”+val+“”)。连接(“”) *{ 保证金:0; 填充:0; } .它{ 边框:2倍实心; 背景颜色:白肋木; 颜色:白色; 宽度:60px; 高度:50px; } 怎么样: // You can just fill an array

我想使用map()输出每个元素的索引

const arr=[1,2,3,4,5,6,7,8,9];
const div=document.querySelector(“.container”)
div.innerHTML=arr.map((索引,val)=>“”+val+“”)。连接(“”)
*{
保证金:0;
填充:0;
}
.它{
边框:2倍实心;
背景颜色:白肋木;
颜色:白色;
宽度:60px;
高度:50px;
}
怎么样:

// You can just fill an array with the required number of null elements
const arr = new Array(9).fill(null);
const div = document.querySelector('.container');
// Note that index is the second parameter of the callback passed to map
div.innerHTML = arr.map((val, index) => {
    const count = index + 1;
    // If you prepend the count with "it-", the class name is valid
    return `<div class="it it-${count}">${count}</div>`;
}).join('');
//只需使用所需数量的空元素填充数组即可
const arr=新数组(9).fill(null);
const div=document.querySelector('.container');
//请注意,index是传递给map的回调的第二个参数
div.innerHTML=arr.map((val,index)=>{
常数计数=索引+1;
//如果在计数前加上“it-”,则类名有效
返回`${count}`;
}).加入(“”);

那么:

// You can just fill an array with the required number of null elements
const arr = new Array(9).fill(null);
const div = document.querySelector('.container');
// Note that index is the second parameter of the callback passed to map
div.innerHTML = arr.map((val, index) => {
    const count = index + 1;
    // If you prepend the count with "it-", the class name is valid
    return `<div class="it it-${count}">${count}</div>`;
}).join('');
//只需使用所需数量的空元素填充数组即可
const arr=新数组(9).fill(null);
const div=document.querySelector('.container');
//请注意,index是传递给map的回调的第二个参数
div.innerHTML=arr.map((val,index)=>{
常数计数=索引+1;
//如果在计数前加上“it-”,则类名有效
返回`${count}`;
}).加入(“”);

在映射值时,必须应用innerHTML,并使用+=赋值运算符连接每个值

const arr = [1,2,3,4,5,6,7,8,9];
const div = document.querySelector('.container')
arr.map(val => div.innerHTML += `<div class='it'>${val}</div>`)
const arr=[1,2,3,4,5,6,7,8,9];
const div=document.querySelector(“.container”)
arr.map(val=>div.innerHTML+=`${val}`)

在映射值时,必须应用innerHTML,并使用+=赋值运算符连接每个值

const arr = [1,2,3,4,5,6,7,8,9];
const div = document.querySelector('.container')
arr.map(val => div.innerHTML += `<div class='it'>${val}</div>`)
const arr=[1,2,3,4,5,6,7,8,9];
const div=document.querySelector(“.container”)
arr.map(val=>div.innerHTML+=`${val}`)
试着理解。。。 这个

const arr=['a','b','c'];
const div=document.querySelector(“#container”)
div.innerHTML=arr.map((val,i)=>`${val}-${i}`)。连接(“”)
#容器分区{
显示:内联块;
边框:2倍纯色灰色;
背景颜色:白肋木;
颜色:白色;
宽度:4em;
高度:2.5em;
文本对齐:居中;
线高:2.6em;
边缘:1米;
}
试着理解。。。 这个

const arr=['a','b','c'];
const div=document.querySelector(“#container”)
div.innerHTML=arr.map((val,i)=>`${val}-${i}`)。连接(“”)
#容器分区{
显示:内联块;
边框:2倍纯色灰色;
背景颜色:白肋木;
颜色:白色;
宽度:4em;
高度:2.5em;
文本对齐:居中;
线高:2.6em;
边缘:1米;
}

考虑到您有以下几点:

const arr = [1,2,3,4,5,6,7,8,9];
...
div.innerHTML = arr.map((index,val) => "<div class='it'>"+val+"</div>" ).join('');
const arr=[1,2,3,4,5,6,7,8,9];
...
div.innerHTML=arr.map((索引,val)=>“”+val+“”)。连接(“”);
将元素的索引作为第二个参数。因此,要获取此索引,您的代码应该如下所示:

div.innerHTML = arr.map((val,index)=>`<div>${val} - ${index}</div>`).join('');
div.innerHTML=arr.map((val,index)=>`${val}-${index}`)。连接(“”);

这将解决您的问题。

考虑到您有以下几点:

const arr = [1,2,3,4,5,6,7,8,9];
...
div.innerHTML = arr.map((index,val) => "<div class='it'>"+val+"</div>" ).join('');
const arr=[1,2,3,4,5,6,7,8,9];
...
div.innerHTML=arr.map((索引,val)=>“”+val+“”)。连接(“”);
将元素的索引作为第二个参数。因此,要获取此索引,您的代码应该如下所示:

div.innerHTML = arr.map((val,index)=>`<div>${val} - ${index}</div>`).join('');
div.innerHTML=arr.map((val,index)=>`${val}-${index}`)。连接(“”);


这应该可以解决您的问题。

将索引添加到您输出的内容中。您将映射回调的参数放在最前面。值是第一个参数,索引是第二个参数。@Dave Newton,如何将索引输出为
?示例:class=“1”,…class=“9”将索引添加到输出内容中。您将映射回调的参数放在最前面。值是第一个参数,索引是第二个参数。@Dave Newton,如何将索引输出为
?示例:class=“1”,…class=“9”如何将索引输出为类?示例:class=“1”、…class=“9”1和9不是有效的类名。您需要使用对类名的第一个字符有效的字符作为前缀。请看更新。你能看一下吗,谢谢:,提前谢谢如何将索引输出为类?示例:class=“1”、…class=“9”1和9不是有效的类名。您需要使用对类名的第一个字符有效的字符作为前缀。请看更新。你能看一看吗,谢谢:,提前谢谢这是不对的-示例中的映射构建了一个html字符串数组,然后连接并应用到
innerHtml
,实际上是为了修正这一点。你所得到的并没有错,但这不是OP问题的解决方案。没有必要在循环中应用
innerHTML
。这是不对的-示例中的映射构建了一个html字符串数组,然后将这些字符串连接并应用到
innerHTML
,实际上是为了修正这一点。你所得到的并没有错,但这不是OP问题的解决方案。没有必要在循环中应用
innerHTML
。请看一看,谢谢:请看一看,谢谢: