Javascript arrays are one of the most interesting data structures. Javascript is a part of every developer and I will show you some things that we can do more often using the javascript.
What is an Array?
An array is a variable, which can store multiple values in same time
How to create an array in Javascript?
var array_name = [item1, item2, ...];
var cars = ["HARIKRISHNAN", "ROSHU", "SURESH"];
1. How to delete elements from an Array?
We all will reach a situation in which we want to delete an element from an array there are multiple techniques such as using the delete operator with the index of the array or we can use by creating a new array remove the item we want to delete or using the splice or we can filter an array.
const str = ['KBFC', 'ATK', 'NEUFC', 'BFC'];
str.splice(3, 1);
console.log(str);
// Output: > Array ["KBFC", "ATK", "NEUFC"]
2. How to Empty an array?
Removing elements from an array is something we need to do, but in some cases when we need want to remove entire elements from the array
const str = new Array(5).fill('h');
str.length = 0;
console.log(str);
// Output: > Array []
The length property in an array is used for both write and read so the above code is valid even in hard mode.
3. How to Remove false values from an Array?
I some cases arrays will have some bad values like undefined,null or just false in them. So we need to remove an elements which are bad in nature and we can do by this words.
const str = ['k', null, false, 'b', undefined];
const str1 = str.filter(Boolean)
console.log(str1);
// Output: > Array ["k", "b"]
4. How to check if all elements in an array accept a condition?
When we are working with an array of objects we need to check if all elements of the objects have same key or not can be done very easily with this method.
const number = [2,4,5];
const odd = number.every(item => item % 2 === 0);
console.log(odd);
5. Check if a given variable is an Array
In Javascript, all arrays are considered as objects which will have some extra methods added to the objects, so when we try to use the type of an array it will return an object. Here we can use the length method we will have a better way to check if the given variable is an array or not.
const names = ["Kerala", "Blasters"];
console.log(Array.isArray(names)); // true
const age = 7;
console.log(Array.isArray(age)); // false
What is an Array?
An array is a variable, which can store multiple values in same time
How to create an array in Javascript?
var array_name = [item1, item2, ...];
var cars = ["HARIKRISHNAN", "ROSHU", "SURESH"];
1. How to delete elements from an Array?
We all will reach a situation in which we want to delete an element from an array there are multiple techniques such as using the delete operator with the index of the array or we can use by creating a new array remove the item we want to delete or using the splice or we can filter an array.
const str = ['KBFC', 'ATK', 'NEUFC', 'BFC'];
str.splice(3, 1);
console.log(str);
// Output: > Array ["KBFC", "ATK", "NEUFC"]
2. How to Empty an array?
Removing elements from an array is something we need to do, but in some cases when we need want to remove entire elements from the array
const str = new Array(5).fill('h');
str.length = 0;
console.log(str);
// Output: > Array []
The length property in an array is used for both write and read so the above code is valid even in hard mode.
3. How to Remove false values from an Array?
I some cases arrays will have some bad values like undefined,null or just false in them. So we need to remove an elements which are bad in nature and we can do by this words.
const str = ['k', null, false, 'b', undefined];
const str1 = str.filter(Boolean)
console.log(str1);
// Output: > Array ["k", "b"]
4. How to check if all elements in an array accept a condition?
When we are working with an array of objects we need to check if all elements of the objects have same key or not can be done very easily with this method.
const number = [2,4,5];
const odd = number.every(item => item % 2 === 0);
console.log(odd);
5. Check if a given variable is an Array
In Javascript, all arrays are considered as objects which will have some extra methods added to the objects, so when we try to use the type of an array it will return an object. Here we can use the length method we will have a better way to check if the given variable is an array or not.
const names = ["Kerala", "Blasters"];
console.log(Array.isArray(names)); // true
const age = 7;
console.log(Array.isArray(age)); // false
Comments
Post a Comment