0

My goal is to have a bunch of arrays, para1, para2.... para20, and find all of the arrays with multiple elements in it, and then to be able to access them. For example I want an if statement in a for loop that would basically check all of my arrays to see which ones have multiple elements. Then, for the ones that do have multiple elements I would use an if statement to extract a particular item from each array.

var para1 = ["NB4-CAXL-14U-12-"];
var para2 = ["K"];
var para3 = ["-270°C to 1372°C, –454°F to 2501°F"];
var para4 = ['1/8"', '3/16"', '1/4"'];
var para5 = ['6"', '12"', '18"'];

for (var j = 1; j < 10; j++) {
    var lenId = "para" + j;
    console.log(lenId.length);
}

document.getElementById("demo").innerHTML = typeof lenId;

I defined a few arrays and made a for loop that generated a variable that was the name of each of the arrays, but when i went to check the length of the arrays i realized they are all 5, because lenId = "para1" is just a string with 5 letters in it. How would i be able to check para1 to see how many elements are in the array? Or is there a better method for checking the length of all my arrays by possibly putting them all into one larger array or something? Any help would be greatly appreciated

akosel
  • 1,183
  • 9
  • 14
Matthew Sirkin
  • 93
  • 2
  • 14
  • use multi dimension arrays... basically an array of arrays – Liquidchrome Oct 20 '16 at 19:16
  • @Liquidchrome - An array of arrays, is not a multidimensional array. Two different things. See: http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – gravity Oct 20 '16 at 19:28

4 Answers4

1

Put the arrays in an object:

//Initializing the object:
var O = {
  para1: ["NB4-CAXL-14U-12-"],
  para2: ["K"],
  para3: ["-270°C to 1372°C, –454°F to 2501°F"],
  para4: ['1/8"', '3/16"', '1/4"']
};

//Adding a new key to the object:
O.para5 = ['6"', '12"', '18"'];

//Getting the object's keys and their lengths:
for(var j in O) {
  console.log(j + ': ' + O[j].length);
}

//Accessing an individual array element:
console.log(O.para4[1]);   //3/16"

//Iterating through the object's array values using a for loop
for(var j in O) {
  for(var i = 0 ; i < O[j].length ; i++) {
    console.log(j + ': Element ' + i + ': Value ' + O[j][i]);
  }
}

//Iterating through the object's array values using forEach
for(var j in O) {
  O[j].forEach(function(val, i) {
    console.log(j + ': Element ' + i + ': Value ' + val);
  });
}
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
  • Thanks, that worked perfectly. But what does it mean if in the console it says: VM39 extensions::schemaUtils:112Uncaught Error: Invocation of form runtime.connect(null, ) doesn't match definition runtime.connect(optional string extensionId, optional object connectInfo)? The only other thing in my code is a for loop that i've had for a long time and haven't changed so I don't know what could be causing it.. – Matthew Sirkin Oct 20 '16 at 19:30
  • Sorry, that's new to me. Are you running this in an extension? – Rick Hitchcock Oct 20 '16 at 19:33
  • I'm using Jsfiddle – Matthew Sirkin Oct 20 '16 at 19:59
  • That's strange. I don't see the same effect in JSFiddle: https://jsfiddle.net/s8m84c0u/ What browser are you using? – Rick Hitchcock Oct 20 '16 at 20:01
  • @ Rick Hitchcock - I'm using Chrome, and it's weird cause I have this code open on 2 different computers right now and 1 of them has the error and the other one doesn't so it must not be code related... I suppose it's not important since everything seems to be working correctly with the code. Thanks for the help – Matthew Sirkin Oct 20 '16 at 21:50
  • @ Rick Hitchcock - I just realized, your code works for accessing each of the sub arrays themselves, but how would i access an element inside a sub array? for example when i use O[3] i get ["1/8"", "3/16"", "1/4""] as the output, but how do i get 3/16"? Its like I want to take O[3][1] – Matthew Sirkin Oct 21 '16 at 13:47
  • Instead of `O[3][1]`, you would use `O.para4[1]` or `O['para4'][1]`. Objects provide a lot of power and flexibility, and they can *somewhat* emulate an associative array. I've provided more examples in my code for working with the object. – Rick Hitchcock Oct 21 '16 at 14:11
  • 1
    @ Rick Hitchcock, thanks again you're a life saver! My problem was that in my loop i was going from j = 0 to the length of O so my j was a number which is why i couldn't do O[j][i], whereas your j is the actual array itself – Matthew Sirkin Oct 21 '16 at 14:20
0

I would go ahead and throw your arrays into a single array. Then you can more easily do operations over that array without having to worry about numbering. The number is implicit based on the index!

var paras = [];
paras.push(["NB4-CAXL-14U-12-"]);
paras.push(["K"]);
paras.push(["-270°C to 1372°C, –454°F to 2501°F"]);
paras.push(['1/8"', '3/16"', '1/4"']);
paras.push(['6"', '12"', '18"']);

paras.forEach(function(para) {
    console.log(para.length);  
});
akosel
  • 1,183
  • 9
  • 14
0

It's not pretty, but it looks like you are storing these arrays in the global scope (client-side), so technically you could refer to each variable as a property of window:

for (var j = 1; j < 10; j++) {
 var lenId = "para" + j;
 console.log(window[lenId].length);
}
Rob M.
  • 35,491
  • 6
  • 51
  • 50
-1

If arrays are not dynamically generated and you define them like in your example, the better way to do that is by using standard array method filter. It accepts the function as a parameter that returns a boolean and tells whether the array item should be filtered or not. You want to use it like so:

var para1 = ['1', '2', '3'];
var para2 = ['1'];
var para3 = ['1', '2'];

var arraysToFilter = [para1, para2, para3];

var filteredArray = arraysToFilter.filter(function (paraArray) {

    // returns true if array has more than one element;
    return paraArray.length > 1;
});

Now filteredArray would be an array with two elements - para1 and para3

Konstantin Vitkovsky
  • 1,198
  • 11
  • 15