Ad Code

Responsive Advertisement

All About JavaScript Arrays

How to merge two arrays in JavaScript and de-duplicate items

var array1 = ["Shohel","Rana"];
var array2 = ["Shipon", "Shohel"];
let noDuplicate = array1.filter ( i => array2.findIndex(a => i==a)==-1 );
let result = [...noDuplicate, ...array2];
console.log(result);

JavaScript Array splice vs slice


var array=[1,2,3,4,5];
console.log(array.splice(2));
This will return [3,4,5]. The original array is affected resulting in array being [1,2].

var array=[1,2,3,4,5]
console.log(array.slice(2));
This will return 3,4,5. The original array is NOT affected with resulting in array being [1,2,3,4,5].

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var removed = array.splice(2,2);

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < array.length-1; i++){ 
   if ( array[i] === 5) {
     arr.splice(i, 1); 
   }
}
//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]

var elements = [1, 5, 5, 3, 5, 2, 4];
for(var i = elements.length -1; i >= 0 ; i--){
    if(elements[i] == 5){
        elements.splice(i, 1);
    }
}

JavaScript pushing element at the beginning of an array

TheArray.unshift(TheNewObject);

Remove Elements from End of a Array in JavaScript

var ar = [1, 2, 3, 4, 5, 6];
ar.length = 4; // set length to remove elements
console.log( ar ); //  [1, 2, 3, 4]

Remove Elements from End of a Array using pop in JavaScript

var ar = [1, 2, 3, 4, 5, 6];
ar.pop(); // returns 6
console.log( ar ); // [1, 2, 3, 4, 5]

Remove Elements from Beginning of a Array in JavaScript

var ar = ['zero', 'one', 'two', 'three'];
ar.shift(); // returns "zero"
console.log( ar ); // ["one", "two", "three"]

Remove Element by Array filter Method

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var filtered = array.filter(function(value, index, arr){
    return value > 5;
});

//filtered => [6, 7, 8, 9]
//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Remove Elements by Delete Operator in JavaScript Array

var ar = [1, 2, 3, 4, 5, 6];
delete ar[4]; // delete element with index 4
console.log( ar ); // [1, 2, 3, 4, undefined, 6]
alert( ar ); // 1,2,3,4,,6

Clear or Reset a JavaScript Array

var ar = [1, 2, 3, 4, 5, 6];
//do stuff
ar = [];

or
ar.length = 0;

//a new, empty array!
var ar = [1, 2, 3, 4, 5, 6];
console.log(ar); // Output [1, 2, 3, 4, 5, 6]
ar.splice(0, ar.length);
console.log(ar); // Output []

Insert an item into an array at a specific index (JavaScript)

var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());

Array sort and reverse in Java Script Array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();        // First sort the elements of fruits 
fruits.reverse();     // Then reverse the order of the elements

Array Extension

//delete object
//split javascript object
Array.prototype.spliceObject = function (sourceToRemove) {
    //Remove the deleted entry from list
    var index = this.indexOf(sourceToRemove);
    if (index != -1) { // Make sure the value exists
        this.splice(index, 1);
    }
    return index;
};
// calling arrayList.spliceObject(entity);

Array.prototype.spliceObjectByID = function (id) {
    //Remove the deleted entry from list
    var index = this.map(function (x) { return x.id; }).indexOf(id);
    if (index != -1) { // Make sure the value exists
        this.splice(index, 1);
    }
    return index;
};
// calling arrayList.spliceObject(entity.id);

//array move
//move up move(array, element, -1);
//move down move(array, element, 1);
Array.prototype.arrayMove = function (element, delta) {
    var index = this.indexOf(element);
    var newIndex = index + delta;
    if (newIndex < 0 || newIndex == this.length) return; //Already at the top or bottom.
    var indexes = [index, newIndex].sort(function (a, b) { return a - b; }); //Sort the indixes
    this.splice(indexes[0], 2, this[indexes[1]], this[indexes[0]]); //Replace from lowest index, two elements, reverting the order
    return;
};

Find the Highest (or Lowest) Array Value

//Sorting ascending:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
// now points[0] contains the lowest value
// and points[points.length-1] contains the highest value

//Sorting descending:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
// now points[0] contains the highest value
// and points[points.length-1] contains the lowest value


Java Script Array Clone

https://e2ecode.blogspot.com/2013/12/array-clone-in-java-script.html

Post a Comment

0 Comments

Close Menu