Is it possible to insert an array inside another keeping its original array form? I looked on google and didn't find what I wanted, so I'll try here. Should look like this:
arr1 = [1, 2 , 3]
arr2 = [4, 5, 6]
console.log(arr3) // [[1, 2, 3], [4, 5, 6]]
Create a new array that references your other arrays...
const arr3 = [arr1, arr2];
console.log(arr3);
// [[1, 2, 3], [4, 5, 6]]
Or this one use array.push()
let arr1 = [1, 2 , 3]
let arr2 = [4, 5, 6]
let arr3= []
arr3.push(arr1,arr2)
console.log(arr3)
Yes its possible like the below way
var arr1 = new Array (1, 2, 3);
var arr2 = new Array (4, 5, 6);
var arr3 = new Array();
arr3[0] = arr1;
arr3[1] = arr2;
console.log(JSON.stringify(arr3))
Yes, absolutely, it's possible:
const arr1 = [1, 2 , 3]
const arr2 = [4, 5, 6]
const arr3 = []
arr3.push(arr1)
arr3.push(arr2)
console.log(arr3)
The new array holds the references to arr1 and arr2. Even if the references to arr1 and arr2 become unaccessible, the content of these array will not be garbage collected if arr3 is accessible.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [7, 8, 9];
const concat = (...arrs) => arrs;
console.log(JSON.stringify(concat(arr1, arr2)));
console.log(JSON.stringify(concat(arr1, arr2, arr3)));
Related
I have an array of arrays:
var arr = [
[0, 3, 3],
[0, 4, 3],
[1, 3, 4],
[1, 4, 4]
];
I want to create a new array that combines the contents of each index of the inner arrays. For example, the first elements at index 0 of each of the inner arrays are 0, 0, 1, and 1. I want to create a new array that combines those elements so that I end up with [0, 1] (I don't want duplicates in the new array). The next "column" is 3, 4, 3, and 4. So I'd want an array of [3, 4].
The end result I want is
[
[0, 1],
[3, 4],
[3, 4]
]
I can't quite figure out how to do this.
May not be the most efficient, but does meet you cryptic requirements.
var arr = [
[0, 3, 3],
[0, 4, 3],
[1, 3, 4],
[1, 4, 4]
];
const unique = (myArray) => [...new Set(myArray)];
let x, y,
tarr = [], // temp array
farr = []; // final array
for (x=0; x<arr.length; x++) {
tarr.length = 0;
for (y=0; y<arr[x].length; y++) {
tarr.push(arr[y][x])
}
farr.push( unique(tarr) );
}
console.log(farr.join('\n'));
Here another solution.
const result = (arr) => {
let index = 0;
const min = Math.min(...arr.map(r => r.length)) // get min length
const grid = [];
while (index < min) { // loop with min
let s = new Set(); // using set to keep unique values
arr.forEach(r => s.add(r[index])); // pick value
grid.push([...s]); // get array from set
index++;
}
return grid
}
const array = [ [0, 3, 3], [0, 4, 3], [1, 3, 4], [1, 4, 4] ];
console.log(result(array))
Let's say I have two arrays, where array1 is always changing:
First case:
array1 = [1, 2, 3, 4, 5]
array2 = [1, 2, 3]
How can I compare them and add 4 and 5 into array2?
I am getting the difference between them doing:
let difference = array1.filter(x => !array2.includes(x));
and then doing array2.push(difference), so array2 is now equal to array1, right?
Second case:
array1 = [1, 2, 8, 9]
array2 = [1, 2, 3]
So now I need to remove 3 from array2, and add 8 and 9, how can I do this?
EDIT: I need this because I'm getting array1 from a server(they are chats) and it's dynamically changing every 5 sec, and this is problem. I need to keep the elements I already have so they won't "update" and only change the one getting deleted or added. Hope this makes sense.
First case will not work as aspectedlooking at the code,
to achive what you want you have to write:
difference.forEach((x) => array2.push(x));
instead of:
array2.push(difference)
for the second one if you want to remove a record in array2 because is missing in array1 you need to control each value of array2 in array1 and remove if not exists by ID
var array1 = [1, 2, 8, 9];
var array2 = [1, 2, 3];
//here i build difference2 collecting the value of array2 that miss on array1
let difference2 = array2.filter((x) => !array1.includes(x));
//here with splice and indexOf i remove every value collected before
difference2.forEach((x) => array2.splice(array2.indexOf(x), 1));
//following code is to add the 8 and 9
let difference = array1.filter((x) => !array2.includes(x));
difference.forEach((x) => array2.push(x));
console.log(array2);
//the result [1,2,8,9]
let array1 = [1, 2, 3, 4, 5];
let array2 = [1, 2, 3];
let filteredArray = array2.filter((a) => array1.includes(a));
let secFilteredArray = array1.filter((a) => !filteredArray.includes(a));
console.log(filteredArray.concat(secFilteredArray));
You could take a Set and delete seen items and add the rest to the array.
const
array1 = [1, 2, 8, 9],
array2 = [1, 2, 3],
set1 = new Set(array1);
let i = array2.length;
while (i--) if (!set1.delete(array2[i])) array2.splice(i, 1);
array2.push(...set1);
console.log(array2);
Just use another filter and combine the two arrays.
const array1 = [1, 2, 8, 9];
let array2 = [1, 2, 3];
const inArrOne = array1.filter(x => !array2.includes(x));
const inBothArr = array2.filter(x => array1.includes(x));
array2 = [...inBothArr, ...inArrOne];
console.log(array2);
I would avoid much built-in or third party compare functions since I am not sure what I am dealing with. This could be refactored and optimized more if the array1 is guaranteed to have an ordered list.
let localArray = [1, 2, 3, 4, 5],
lastServerArray = [];
/**
* Compares "fromArr" to "targetArr"
* #param fromArr Array of elements
* #param targetArr Array of elements
* #returns List of elements from "fromArr" that do not happen in "targetArr"
*/
const compArr = (fromArr, targetArr) => {
const result = [];
for (let i = 0, len = fromArr.length; i < len; i++) {
const elem = fromArr[i],
targetIdx = targetArr.indexOf(elem);
if (!~targetIdx && !~result.indexOf(elem)) {
// Element do not exist in "targetArr" and in current "result"
result.push(elem);
}
}
return result;
}
const updateLocalArray = (serverArray = []) => {
if (JSON.stringify(lastServerArray) === JSON.stringify(serverArray)) {
console.log('Nothing changed from server, skip updating local array');
return localArray;
}
lastServerArray = serverArray;
const notExistentLocalElems = compArr(serverArray, localArray), // Elements that do not exists in local array
notExistentServerElems = compArr(localArray, serverArray); // Elements that do not exists in server array
// Do something to those "notExistentLocalElems" or "notExistentServerElems"
// ---
// Sync server array to local array
// Remove elements that is not on server.
localArray = localArray.filter(elem => !~notExistentServerElems.indexOf(elem));
console.log('These elements removed from localArray', notExistentServerElems);
// Append elements that is on server.
localArray.push(...notExistentLocalElems);
console.log( 'These elements added into localArray', notExistentLocalElems);
return localArray;
}
updateLocalArray([1, 2, 3]);
console.log(`1. server sends [1, 2, 3] -- local becomes`, localArray);
updateLocalArray([3, 4, 5, 6]);
console.log(`2. server sends [3, 4, 5, 6] -- local becomes`, localArray);
updateLocalArray([5, 5, 4, 2, 7]);
console.log(`3. server sends [5, 5, 4, 2, 7] -- local becomes`, localArray);
updateLocalArray([0, 0, 1, 2]);
console.log(`4. server sends [0, 0, 1, 2] -- local becomes`, localArray);
You could do like this if you want to mutate array2:
let array1 = [1, 2, 8, 9];
let array2 = [1, 2, 3];
let valuesToAdd = array1.filter(x => !array2.includes(x));
let indexesToDelete = Object.entries(array2).filter(([, x]) => !array1.includes(x)).map(([i]) => i);
// Reverse iteration to preserve indexes while removing items
indexesToDelete.reverse().forEach(i => array2.splice(indexesToDelete[i], 1));
array2.push(...valuesToAdd);
console.log(array2);
How to get which do not intersect value between 2 Array with Lodash?
Expected:
const arr1 = [1, 2, 3]
const arr2 = [2, 3, 4]
console.log(unintersection(arr1, arr2))
Output
[1, 4]
Use _.xor() to find the symmetric difference - the numbers that are in one of the arrays, but not both:
const arr1 = [1, 2, 3]
const arr2 = [2, 3, 4]
const result = _.xor(arr1, arr2)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
I have to compare two arrays and return true if only the array 1 contains all values of array 2. What is the suitable loadash function for this?
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2]
let arr3 = [1, 5]
comparing arr1 with arr2 should return true
comparing arr1 with arr3 should return false
Just for completeness, you could take a Set and compare with Set#has.
let arr1 = [1, 2, 3, 4],
arr2 = [1, 2],
arr3 = [1, 5],
base = new Set(arr1);
console.log(arr2.every(Set.prototype.has, base));
console.log(arr3.every(Set.prototype.has, base));
You can just use every:
let arr1 = [1, 2, 3, 4];
let arr2 = [1, 2];
let arr3 = [1, 5];
const allElements = (a1, a2) => a2.every(e => a1.includes(e));
console.log(allElements(arr1, arr2));
console.log(allElements(arr1, arr3));
There is no need for loadash here just use native JavaScript Array#every method with Array#includes method
function compareArray(arr1, arr2) {
return arr2.every(v => arr1.includes(v))
}
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2]
let arr3 = [1, 5]
console.log(compareArray(arr1, arr2))
console.log(compareArray(arr1, arr3))
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2]
for(let x of arr2) {
_.includes(arr1, x) || true;
}
You can use _.difference in such way:
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2]
const b = _.difference(arr2, arr1).length === 0
You can use _.difference
let arr1 = [1, 2, 3, 4]
let arr2 = [1, 2]
let arr3 = [1, 5]
console.log(_.difference(arr2,arr1).length === 0)
console.log(_.difference(arr3,arr1).length === 0)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
I have an array and would like to get its head and the rest. How do I do this using a destructuring assignment? Is this even possible?
If the array has only two elements, it's pretty easy:
const [head, rest] = myArray;
But what if it contains more than two entries?
You can use spread syntax for that.
const [head, ...rest] = myArray;
var myArray = [1, 2, 3, 4, 5, 6];
const [head, ...rest] = myArray;
console.log(head);
console.log(rest);
Thay way:
const [head, ...rest] = myArray;
With the spread syntax ..., all other items goes into the rest array.
const [head, ...rest] = [1, 2, 3, 4, 5];
console.log(head);
console.log(rest);
You can do like this
const [car, ...cdr] = [1, 2, 3, 4, 5];
console.log(car); // 1
console.log(cdr); // [2, 3, 4, 5]
For more details, refer this link
Hope this helps :)
Accoding to Destructing assignment (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) :
var x = [1, 2, 3, 4, 5];
var [y, z] = x;
console.log(y); // 1
console.log(z); // 2