0

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.

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?

Rooster
  • 9,954
  • 8
  • 44
  • 71
Damien
  • 4,093
  • 9
  • 39
  • 52
  • 4
    It's an array of arrays. – Jason P Jul 17 '13 at 19:58
  • 5
    it's an array of arrays. `locationsArray[0][0] == 'title1'` – Kevin B Jul 17 '13 at 19:58
  • Its a 3 dimensional array. I think thats what you call them. Or maybe that one was called 2 dimensional. Eh, idk. – Shawn31313 Jul 17 '13 at 19:59
  • 4
    @Shawn31313 i believe 2 dimensional, if you write it out with each nested array `locationsArray[x]` on its own line, it will make a 2d grid – dm03514 Jul 17 '13 at 20:00
  • 4
    There is only one kind of arrays, what you jave is just a bunch of them nested inside one another. – adeneo Jul 17 '13 at 20:01
  • 1
    Note that javascript does not have "multidimensional" arrays. It just happens to be an array storing another array, etc. – jbabey Jul 17 '13 at 20:02
  • 1
    Agree with adeneo -- it is an array. An array is an array. There are not different "kinds" of arrays. The contents of an array do not make it become something other than an array. Want proof? Type `console.log(typeof locationsArray);` – Chris Baker Jul 17 '13 at 20:03
  • Agree with @jbabey - also note that arrays in javascript aren't of any particular "type" ... you can create an array that looks like ["a string", ["another string", 17], { foo="bar", num=17 }, 86]` that is - an array of 4 items: a[0] is a string, a[1] is an array, a[2] is an object, and a[3] is an integer. – Stephen P Jul 17 '13 at 20:17

5 Answers5

3

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/

Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • so would the resulting newArray look like this: [['title1','des1',['title3','des3']] – Damien Jul 17 '13 at 20:04
  • @Damien except for the right square bracket you're missing after `des1`, yes – John Dvorak Jul 17 '13 at 20:05
  • what's the ,10 for in the parseInt statement? Is it necessary? – Damien Jul 17 '13 at 20:13
  • @Damien parse it converts the string to integer and 10 is the radix. – Selvakumar Arumugam Jul 17 '13 at 20:14
  • parseInt in this case is useless. "If one operand is a number, convert the other operand to a number and perform a numeric comparison." http://stackoverflow.com/questions/14687876/how-do-the-javascript-relational-comparison-operators-coerce-types or http://es5.github.io/#x11.8 – Givi Jul 17 '13 at 20:21
1

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']. If I needed to get the title from the first array, I can access it by locationsArray[0][0] to get '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.

DSurguy
  • 375
  • 1
  • 9
0

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.

Dany Caissy
  • 3,176
  • 15
  • 21
0

locationsArray is an array of arrays. The first [] operator indexes into the main array (e.g. locationsArray[0] = ['title1','description1','12']) while a second [] operation indexes into the array that the first index pointed to (e.g. locationsArray[0][1] = 'description1').

Your newArray looks like it needs to be the same thing.

Lochemage
  • 3,974
  • 11
  • 11
0

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 );
Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80