Thursday 19 December 2019

JS array && objects

Objects copy
var a = {test : 1}
var b = {}
// Copy a to b
Object.assign(b,a)


Array are like objects
when var a = [{test:1}]
var b = a; b refers to the array.

How to copy simple array(array with values ) to another array?
var newArray = oldArray.slice();
Basically, the slice() operation clones the array and returns a reference to a new array.
https://stackoverflow.com/questions/7486085/copy-array-by-value


How to copy complex array(array with objects) to another array?

const newArray = myArray.map(a => Object.assign({}, a));
https://stackoverflow.com/questions/597588/how-do-you-clone-an-array-of-objects-in-javascript


How to remove an object from array(must use at least one object attribute, for example id)?

https://stackoverflow.com/questions/21659888/javascript-find-and-remove-object-in-array-based-on-key-value/21659937
var id = 88;

for(var i = 0; i < data.length; i++) {
    if(data[i].id == id) {
        data.splice(i, 1);
        break;
    }
}


What is array splice?
 Original array will be altered using splice. If want original array not be altered us slice instead
The splice method can be used to add or remove elements from an array. The first argument specifies the location at which to begin adding or removing elements. The second argument specifies the number of elements to remove. The third and subsequent arguments are optional; they specify elements to be added to the array.

Here we use the splice method to remove two elements starting from position three (zero based index):


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

/*
removed === [3, 4]
arr === [1, 2, 5, 6, 7, 8, 9, 0]

*/

https://love2dev.com/blog/javascript-remove-from-array/


How to find difference between two simple array(with values only) ?
function arr_diff (a1, a2) {

    var a = [], diff = [];

    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }

    for (var k in a) {
        diff.push(k);
    }

    return diff;
}

console.log(arr_diff(['a', 'b'])

https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript


How to find differences between complex array(array with objects)?


a = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"},  { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}]
b = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}]

function comparer(otherArray){
  return function(current){
    return otherArray.filter(function(other){
      return other.value == current.value && other.display == current.display
    }).length == 0;
  }
}

var onlyInA = a.filter(comparer(b));
var onlyInB = b.filter(comparer(a));

result = onlyInA.concat(onlyInB);

console.log(result);

No comments:

Post a Comment