Checking the lengths of all my arrays in Javascript

时间:2016-10-20 19:13:49

标签: javascript arrays string for-loop

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

4 个答案:

答案 0 :(得分: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);
  });
}

答案 1 :(得分: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);  
});

答案 2 :(得分: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);
}

答案 3 :(得分:-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