- JavaScript variables can be objects. Arrays are special kinds of objects. Because of this, you can have variables of different types in the same Array. You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array
- The new keyword only complicates the code. It can also produce some unexpected results:
var points = new Array(); // Bad
var points = []; // Good
var points = new Array(40, 100, 1, 5, 25, 10); // Bad
var points = [40, 100, 1, 5, 25, 10]; // Good
var points = new Array(40, 100); // Creates an array with two elements (40 and 100)
var points = new Array(40); // Creates an array with 40 undefined elements !!!!!
var fruits, text;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.forEach(myFunction);
function myFunction(value, index, array) {
text += "<li>" + value + "</li>";
}
// using Arrow Function
fruits.forEach((value, index, array) => {
console.log(value, index, array);
});
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon"); // adds a new element (Lemon) to fruits
fruits[fruits.length] = "Lemon"; // adds a new element (Lemon) to fruits
- Adding elements with high indexes can create
undefined
"holes" in an array.
- If you use named indexes, JavaScript will redefine the array to a standard object. After that, some array methods and properties will produce incorrect results.
- Arrays are a special kind of objects, with numbered indexes.
- You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.
- How to Recognize an Array if
typeof
returns object
- To solve this problem ECMAScript 5 defines a new method
Array.isArray()
: Array.isArray(fruits); // returns true
- For older browser, you can create your own
isArray()
function as follow:
function isArray(x) {
return x.constructor.toString().indexOf("Array") > -1;
}
- The
instanceof
operator returns true if an object is created by a given constructor: fruits instanceof Array; // returns true
.
- Methods:
toString()
, join()
, pop()
, push()
, shift()
, unshift()
, delete fruits[0];
, splice()
, concat()
, slice()
, sort()
, reverse()
, Math.max.apply()
, Math.min.apply
,
- Using
delete
may leave undefined holes in the array. Use pop() or shift() instead.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi"); //in-place change.
fruits.splice(0, 1); // Removes the first element of fruits
- The concat() method can take any number of array arguments:
var newarray = arr1.concat(arr2, arr3);
- The concat() method can also take strings as arguments:
var newarray = arr1.concat("Mariam");
- Sorting Array of objects:
var cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
cars.sort(function(a, b){return a.year - b.year}); // sort cars based on year
cars.sort(function(a, b){
var x = a.type.toLowerCase();
var y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});