Putting array elements in another by spread method (but a little differently) - javascript

I used another (my) way to store the elements of some array in another as a spread method.
I used join method for this way, but the array contains only one.
Here's my code:
const arr = [1, 2, 3];
const newArray = [eval(arr.join(', ')), 4, 5]
console.log(newArray) // [3, 4, 5]

Try this:
const arr = [1, 2, 3];
const newArray = [...arr, 4, 5];
console.log(newArray);

You can use concat for that.
The concat() method will join two (or more) arrays and will not change the existing arrays, but instead will return a new array, containing the values of the joined arrays.
const arr = [1, 2, 3];
const newArray = arr.concat([4, 5]);
console.log(newArray)
Another option is to use spread syntax (...) (introduced in ES6)
const arr = [1, 2, 3];
const newArray = [...arr, ...[4, 5]];
console.log(newArray)

Related

How to duplicate indexes in array with numbers of second array, resulting third array in JS

I have an array of products amount, and another array of products ID's.
I'm trying to do something like this:
arrayOfAmount = [2,3,4]
arrayOfId = [1,2,3]
resulting the third array of ID's:
arrayOfProducts = [1,1,2,2,2,3,3,3,3]
how can I do that?
thank you in advance!
You can try this:
let arrayOfAmount = [2,3,4]
let arrayOfId = [1,2,3]
console.log(arrayOfId.flatMap((x,i)=>Array(arrayOfAmount[i]).fill(x)))
Here is a version how you could do this using the map and flat and fill methods of arrays. I added some comments to help understanding what the code does:
const arrayOfAmount = [2, 3, 4];
const arrayOfId = [1, 2, 3];
const arrayOfProducts = arrayOfId.map((id, index) => {
// get how often an id should be in the array
const length = arrayOfAmount[index];
// create an array with the length
const array = Array(length);
// insert the ID to every index of the array
return array.fill(id);
})
// and finally flat, so that the array doesn't look like: [[1, 1], [2, 2, 2], [3, 3, 3, 3]]
.flat();
console.log(arrayOfProducts);
A short version would be:
const arrayOfAmount = [2, 3, 4];
const arrayOfId = [1, 2, 3];
const arrayOfProducts = arrayOfId.map((id, index) => Array(arrayOfAmount[index]).fill(id))
.flat();

How to get which do not intersect value between 2 Array with Lodash?

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>

How can we make Javascript arrays immutable like for Objects we have Object.freeze() method?

For ex:
const arr = [1,2,3];
arr.push(4); //4
console.log(arr) //[1,2,3,4]
I want this array to be immutable. It should not allow push or add/delete arr values.
Object.freeze works just fine on arrays, since arrays are objects as well:
const arr = [1,2,3];
Object.freeze(arr);
arr.push(4); // attempting to push throws
Arrays are objects - so it'll work fine:
const arr = [1, 2, 3];
Object.freeze(arr);
arr.push(4);
arr[arr.length - 1] = 4;
The index assignment method also fails:
const arr = [1, 2, 3];
Object.freeze(arr);
arr[arr.length - 1] = 4;
Yes you can Freeze array
const arr = [1,2,3];
Object.freeze(arr)
arr.push(5);
Note:- it will do shallow freeze,
const arr = [1, 2, {key: 123}];
Object.freeze(arr)
arr[2].key = 'changed value'
console.log(arr)

How to remove first array element using the spread syntax

So I have an array, ex. const arr = [1, 2, 3, 4];. I'd like to use the spread syntax ... to remove the first element.
ie. [1, 2, 3, 4] ==> [2, 3, 4]
Can this be done with the spread syntax?
Edit: Simplified the question for a more general use case.
Sure you can.
const xs = [1,2,3,4];
const tail = ([x, ...xs]) => xs;
console.log(tail(xs));
Is that what you're looking for?
You originally wanted to remove the second element which is simple enough:
const xs = [1,0,2,3,4];
const remove2nd = ([x, y, ...xs]) => [x, ...xs];
console.log(remove2nd(xs));
Hope that helps.
Destructuring assignment
var a = [1, 2, 3, 4];
[, ...a] = a
console.log( a )
Is this what you're looking for?
const input = [1, 0, 2, 3, 4];
const output = [input[0], ...input.slice(2)];
After the question was updated:
const input = [1, 2, 3, 4];
const output = [...input.slice(1)];
But this is silly, because you can just do:
const input = [1, 2, 3, 4];
const output = input.slice(1);
You could use the rest operator (...arrOutput) with the spread operator(...arr).
const arr = [1, 2, 3, 4];
const [itemRemoved, ...arrOutput] = [...arr];
console.log(arrOutput);

Flatten an array with forEach

After checking out this post on flattening arrays, I noticed that no one had used the array method forEach. I gave it a try and failed, only receiving back an empty array:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach( (element) => {
result.concat(element)
})
console.log(result) //[]
Where did I go wrong?
You have to result = result.concat(element)
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach((element) => {
result = result.concat(element)
})
console.log(result) //[]
Doc: concat
.concat() always returns a new array. It doesn't modify arrays on which it is operating.
You are supposing that .concat() will save result in its operand after completing its opeartion. However it is not the case. You need to save result explicitly after .concat():
result = result.concat(element);
Demo:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach((element) => {
result = result.concat(element)
});
console.log(result);
You can also use spread syntax instead of .forEach() to flatten array.
let result = [].concat(...arrays);
Demo:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [].concat(...arrays);
console.log(result);
concat returns a new array and hence you need to assign it to result like result = result.concat(element)
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach( (element) => {
result = result.concat(element)
})
console.log(result)

Categories