Javascript Array Length

I use an array to store a number of other arrays and split each stored array by adding "fin" at the end.

What really throws me is; when I show what javascript considers to be the length of this array, it says that the array has 603 elements, whereas in reality the array contains approximately 90 elements .: - (

Code as requested: -

// Declare the array var arrForAllData = new Array(); // Concatenate all the arrays to build the 'arrForAllData' array arrForAllData = arrApplicationData + ",fin," + arrCmdbIds + ",fin," + arrBaAvail + ",fin," + arrAppStatus + ",fin," + arrMaxAchieve + ",fin," + arrBreaches + ",fin," + arrMTTR + ",fin," + arrMTBF + ",fin," + arrMTBSI + ",fin," + arrCleanDays + ",fin," + arrHistBaAvail + ",fin," + arrHistBreaches + ",fin," + arrHistDuration; 

I use fin as a separator for each array, since I need to rebuild the arrays later in order to save the need to make API calls to recreate most of the data.

 // Example array arrApplicationData contains Downstream,CIM,Eserve,FPS,Global,GSAP // Display the data in the arrForAllData alert("arrForAllData contains " + arrForAllData ); 

This warning displays all the elements in the array, as I expect it, all separated by commas.

 // Calculate the length of the array var adlen = arrForAllData.length; alert("Number of elements in arrForAllData is " + adlen ); 

This warning displays "adlen" as 603, which, as I said below, is a count of all individual characters.

For some reason, "array.length" counts every single character.

Has anyone come across this before, and if so, is there any way to fix it?

Thanks in advance for your time.

+4
source share
1 answer

We do not combine arrays with strings, as they are passed to a string. Here is what you need:

 var arrForAllData = new Array( arrApplicationData, arrCmdbIds, arrBaAvail, arrAppStatus, arrMaxAchieve, arrBreaches, arrMTTR, arrMTBF, arrMTBSI, arrCleanDays, arrHistBaAvail, arrHistBreaches ); // And now for existing array you can always add new item arrForAllData.push(arrHistDuration); // You access elements of array by their index var a = arrForAllData[5]; // 'a' is now holding the 'arrBreaches' as arrays are indexed from 0 // You can iterate over array, for example to count all the items inside nested arrays var all_items_amount = 0; for(var i=0; i<arrForAllData.length; i++){ all_items_amount += arrForAllData[i].length; } alert(arrForAllData.length); // This will alert the length of main array alert(all_items_amount); // This will alert the number of elements in all nested arrays 

As an alternative to the array definition used, arrays can be created by:

 var x = []; // Empty array var x = new Array(); // Empty array too var x = [a, b, c]; // Array with three items made of variables 'a', 'b' and 'c' var x = new Array(new object(), 'xxx', [], a); // Array with four items: // new instance of object, string 'xxx', new empty array and variable 'a' 
+3
source

Source: https://habr.com/ru/post/1490683/


All Articles