[英]I want to print index of all elements without using forEach or filter or map
Here is my code it is giving me the same index number of the element.这是我的代码,它为我提供了相同的元素索引号。
I want it like this 0,1,2,3,4,5,6
我想要这样
0,1,2,3,4,5,6
let as=[1, 2, 3, 4, 5, 5,5]
for(let i=0;i<as.length;i++){
console.log(as[i], as.indexOf(as[i]))
}
Here is my output :这是我的 output :
1 0
2 1
3 2
4 3
5 4
5 4
5 4
If I understand your question correctly, you want to fetch the index of the elements available in an array.如果我正确理解你的问题,你想获取数组中可用元素的索引。 If Yes, Then you can do like this :
如果是,那么你可以这样做:
let as = [1, 2, 3, 4, 5, 5,5]; const indexArray = as.map((item, index) => index); console.log(indexArray.join(','));
Try this:尝试这个:
let as=[1, 2, 3, 4, 5, 5,5]
for(let i=0;i<as.length;i++){
console.log(as[i], as.indexOf(as[i],i))
}
Explaination: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf解释: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.