how to do a "flat push" in javascript? [duplicate] - javascript

This question already has answers here:
How to extend an existing JavaScript array with another array, without creating a new array
(20 answers)
Closed 3 months ago.
I want to push all individual elements of a source array onto a target array,
target.push(source);
puts just source's reference on the target list.
In stead I want to do:
for (i = 0; i < source.length; i++) {
target.push(source[i]);
}
Is there a way in javascript to do this more elegant, without explicitly coding a repetition loop?
And while I'm at it, what is the correct term? I don't think that "flat push" is correct. Googling did not yield any results as source and target are both arrays.

apply does what you want:
var target = [1,2];
var source = [3,4,5];
target.push.apply(target, source);
alert(target); // 1, 2, 3, 4, 5
MDC - apply
Calls a function with a given this
value and arguments provided as an
array.

The easier way to do this.
var arr1 = [1,2,3]
var arr2 = [4,5,6]
arr1.push(...arr2) //arr1 now contains [1,2,3,4,5,6]

You could use the concat method:
var num1 = [1, 2, 3];
var num2 = [4, 5, 6];
var num3 = [7, 8, 9];
// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged
var nums = num1.concat(num2, num3);

You can simply use spread syntax:
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];
// [0, 1, 2, 3, 4, 5]
Alternatively:
var arr1 = ['a', 'b', 'c'];
var arr2 = [1, 2, 3, ...arr1];
// [1, 2, 3, 'a', 'b', 'c']
Demo:
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];
console.log(arr1);

Related

How to get numbers out of a 2D array to a 1D array [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed last year.
I have an argument that passes me a 2D array and I want to get the numbers from that array and put them on a normal array.
The argument passed to me = ([1, 3, 2], [5, 2, 1, 4], [2, 1])
What I want to do = [1, 3, 2, 5, 2, 1, 4, 2, 1]
Thanks in advance, I think I'm having a brain fart over here!
You can use the flat method of Array!
const arr = [[1, 2, 3], [4, 5, 6]]
console.log(arr.flat())
// [1, 2, 3, 4, 5, 6]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
Iterate the array and add all data in new array with concat() method.
var arr = [[1, 3, 2], [5, 2, 1, 4], [2, 1]];
var newArr = [];
for(var i = 0; i < arr.length; i++)
{
newArr = newArr.concat(arr[i]);
}
console.log(newArr);
You can use array flat method to convert 2d array to 1d array
const arr = [
[1,2,3],
[4,5,6]
].flat(1); //flat(depth:Number)
console.log(arr);
// [1,2,3,4,5,6]

check if variable name is the same a string inside an array [duplicate]

This question already has answers here:
Variable name as a string in Javascript
(20 answers)
Closed 1 year ago.
I have several arrays that I json_encode form php and two variables that I get from an inputs, lets say :
let a = [1,2,3,4,5];
let b = [1,2,3,4,5];
let c = [1,2,3,4,5];
let d = [1,2,3,4,5];
let e = [1,2,3,4,5];
let f = [1,2,3,4,5];
let name = document.getElementById("some_element").value
let number = document.getElementById("some_other_element").value
I wanna check if the name is the same as the array name and then use the array with the name for example a[number].
I am not quite sure of how to do this. Any help would be really welcome.
Use an object:
const arrays = {
a: [1, 2, 3, 4, 5],
b: [1, 2, 3, 4, 5],
c: [1, 2, 3, 4, 5],
d: [1, 2, 3, 4, 5],
e: [1, 2, 3, 4, 5],
f: [1, 2, 3, 4, 5],
};
const name = "a";
const number = 1;
console.log(arrays[name][number]);

How to push array into another array as one element

I'm new in JS, can't find solution to do something like that
var arr = [0];
var elem = [1, 2, 3];
???
console.log(arr); // shows [0, [1, 2, 3]];
I've tried with .push(elem), JS decides that I passed array of values (not a single one), and concatenate content of arr and elem arrays, so that the result is [0, 1, 2, 3]
Use concat!
var arr = [0];
var elem = [1, 2, 3];
var newArr = arr.concat([elem]);
console.log(newArr); // => [0,[1,2,3]]
You may try to use spread operator to concatenate values of an array.
For example:
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2);
//Output: [ 1, 2, 3, 4, 5 ]
Now, after you wrote, what you want,
[0, [1, 2, 3]]
you could use at lease three different approaches:
Simple assignment at the end of the array
var arr = [0],
elem = [1, 2, 3];
arr[arr.length] = elem;
console.log(arr);
Array#push for pushing a value/object at the end of an array, which is basically the same as above without indicating the place for inserting, but you can use more item for pusing to the array.
var arr = [0],
elem = [1, 2, 3];
arr.push(elem);
console.log(arr);
Array#concat, creating a new array with with the given array and the parameters. Here cou need to wrap the content in an array, because concat concatinates arrays.
var arr = [0],
elem = [1, 2, 3];
arr = arr.concat([elem]);
console.log(arr);

How can I insert array in the middle of another array in es6?

I have two arrays:
let a = [1, 3, 5];
let b = [2, 4];
I need to put second array into first one after second element, so this is result:
[1, 3, 2, 4, 5]
What is the best way to insert second array into first in es6?
It can be easily solved using concat operation, but I am looking for nice way of doing it in es6.
If you want to insert the second array at specific index, you can use splice function and spread operator.
var a = [1, 3, 5];
var b = [2, 4, 6];
a.splice(2, 0, ...b);
console.log(a); // [1, 3, 2, 4, 6, 5]
Use Array#splice method with spread syntax.
a.splice(2, 0, ...b)
let a = [1, 2, 5];
let b = [3, 4];
a.splice(2, 0, ...b);
console.log(a);
I think this is the answer you're looking for:
function frankenSplice(arr1, arr2, n) {
let arr3 = [...arr2];
arr3.splice(n, 0, ...arr1);
return arr3;
}

Merge two arrays in javascript [duplicate]

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 9 years ago.
I know in PHP I can use the array_merge function to do this, but let's say I have two arrays like this:
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
How do I merge these two arrays into another array arr3 so that it looks like this:
var arr3 = [1, 2, 3, 4, 5, 6];
Thank you
Try this
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
var array3 = arr1.concat(arr2 );

Categories

Resources