获取 JavaScript 数组中所有元素的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15354100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:30:10  来源:igfitidea点击:

Get list of all elements in a JavaScript array

javascriptarrays

提问by Anderson Green

I'm trying to obtain a list of all elements that are in a JavaScript array, but I've noticed that using array.toStringdoes not always show all the contents of the array, even when some elements of the array have been initialized. Is there any way to print each element of an array in JavaScript, along with the corresponding coordinates for each element? I want to find a way to print a list of all coordinates that have been defined in the array, along with the corresponding values for each coordinate.

我试图获取 JavaScript 数组中所有元素的列表,但我注意到 usingarray.toString并不总是显示数组的所有内容,即使数组的某些元素已被初始化。有没有办法在 JavaScript 中打印数组的每个元素,以及每个元素的相应坐标?我想找到一种方法来打印已在数组中定义的所有坐标的列表,以及每个坐标的相应值。

http://jsfiddle.net/GwgDN/3/

http://jsfiddle.net/GwgDN/3/

var coordinates = [];
coordinates[[0, 0, 3, 5]] = "Hello World";

coordinates[[0, 0, 3]] = "Hello World1";

console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(coordinates.toString()); //this doesn't print anything at all, despite the fact that some elements in this array are defined

回答by user960567

Actually when you use coordinates[[0, 0, 3]] then this means coordinates object with [0, 0, 3] as key. It will not push an element to array but append a property to the object. So use this line which loop through objects. See this for other ways to loop through object properties,

实际上,当您使用坐标 [[0, 0, 3]] 时,这意味着以 [0, 0, 3] 为键的坐标对象。它不会将元素推送到数组,而是将属性附加到对象。因此,请使用循环遍历对象的这一行。有关循环遍历对象属性的其他方法,请参阅此内容

Object.keys(coordinates).forEach(function(key) {
    console.log(key, coordinates[key]);
});

http://jsfiddle.net/GwgDN/17/

http://jsfiddle.net/GwgDN/17/

回答by Latikov Dmitry

Use type 'object' instead 'array' for coordinates

使用类型“对象”而不是“数组”作为坐标

var coordinates = {};
coordinates[[0, 0, 3, 5]] = "Hello World";

coordinates[[0, 0, 3]] = "Hello World1";

console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(JSON.stringify(coordinates));

http://jsfiddle.net/5eeHy/

http://jsfiddle.net/5eeHy/

回答by Robert Mailloux

for (i=0;i<coordinates.length;i++)
{
document.write(coordinates[i] + "<br >");
}

回答by Robert Mailloux

use join function to get all elements of array.use following code

使用 join 函数获取数组的所有元素。使用以下代码

for (var i in coordinates)
{
    if( typeof coordinates[i] == 'string' ){
        console.log( coordinates[i] + "<br >");
    }
}

回答by Udo Klein

Look at coordinates in the debugger and you will see that you have set the properties [0,0,3,5] and [0,0,3] of the objectcoordinates. That is although coordinates is an array you are not using it as an array.

查看调试器中的坐标,您将看到您已经设置了对象坐标的属性 [0,0,3,5] 和 [0,0,3] 。也就是说,尽管坐标是一个数组,但您并没有将其用作数组。