简体   繁体   English

JavaScript中的数组是什么类型?

[英]What kind of array is this in JavaScript?

I have an array that looks like this: 我有一个看起来像这样的数组:

var locationsArray = [['title1','description1','12'],['title2','description2','7'],['title3','description3','57']];

I can't figure out what type of array this is. 我不知道这是什么类型的数组。 More importantly, I'm gonna have to create one based on the info there. 更重要的是,我将不得不根据那里的信息创建一个。 So, if the number on the end is greater than 10 then create a brand new array in the same exact style, but only with the title and description. 因此,如果末尾的数字大于10,则以相同的精确样式创建一个全新的数组,但仅带有标题和描述。

var newArray = [];
// just a guess
if(locationsArray[0,2]>10){
   //add to my newArray like this : ['title1','description1'],['title3','description3']
   ? 
}

How can I do it? 我该怎么做?

Try like below, 尝试如下

var newArray = [];

for (var i = 0; i < locationsArray.length; i++) {
   if (parseInt(locationsArray[i][2], 10) > 10) {
      newArray.push([locationsArray[i][0], locationsArray[i][1]]);
   }
}

DEMO: http://jsfiddle.net/cT6NV/ 演示: http : //jsfiddle.net/cT6NV/

It's an array of arrays, also known as a 2-dimensional array. 它是一个数组数组,也称为二维数组。 Each index contains its own array that has its own set of indexes. 每个索引包含其自己的数组,该数组具有自己的一组索引。

For instance, if I retrieve locationsArray[0] I get ['title1','description1','12'] . 例如,如果我检索locationsArray[0] ,则会得到['title1','description1','12'] If I needed to get the title from the first array, I can access it by locationsArray[0][0] to get 'title1' . 如果需要从第一个数组中获取标题,则可以通过locationsArray[0][0]访问它以获取'title1'

Completing your example: 完成示例:

var newArray = [];
// just a guess
if(locationsArray[0][2]>10){
   newArray.push( [ locationsArray[0][0], locationsArray[0][1] ] );
}

throw that in a loop and you're good to go. 将其循环放置,您就可以开始了。

It's an array of arrays of strings. 它是一个字符串数组的数组。

Each time there is this : [], it defines an array, and the content can be anything (such as another array, in your case). 每次有这个[]时,它都定义一个数组,内容可以是任何内容(例如您的情况下为另一个数组)。

So, if we take the following example : 因此,如果我们采用以下示例:

var myArray = ["string", "string2", ["string3-1", "string3-2"]];

The values would be as such : 这些值将是这样的:

myArray[0] == "string"
myArray[1] == "string2"
myArray[2][0] == "string3-1"
myArray[2][1] == "string3-2"

There can be as many levels of depth as your RAM can handle. RAM可以处理的深度级别可以多达许多。

locationsArray is an array of arrays. LocationsArray是一个数组数组。 The first [] operator indexes into the main array (eg locationsArray[0] = ['title1','description1','12']) while a second [] operation indexes into the array that the first index pointed to (eg locationsArray[0][1] = 'description1'). 第一个[]运算符索引到主数组中(例如,locationsArray [0] = ['title1','description1','12']),而第二个[]运算符索引到第一个索引指向的数组中(例如,locationsArray [0] [1] ='description1')。

Your newArray looks like it needs to be the same thing. 您的newArray看起来好像是同一件事。

It's an array of array. 这是一个数组数组。

var newArray = [];
var locationsArray = [
    ['title1','description1','12'],
    ['title2','description2','7'],
    ['title3','description3','57']
];

for(i = 0; i < locationsArray.length; i++) {
    if (locationsArray[i][2] > 10) {
        newArray .push([locationsArray[i][0], locationsArray[i][1]]);
    }
}

console.log(newArray );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM