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 );
Related
This question already has answers here:
Split array into chunks
(73 answers)
Closed 2 years ago.
Suppose that you have an array of doubles in Javascript:
double_arr = [1, 2, 3, 4, 5, 6, 7, 8]
What is the most efficient way to convert it into an array of arrays with 2 doubles like above:
double_arr = [[1,2], [3,4], [5,6], [7,8]]
You can iterate skipping one index in each iteration, like this:
const double_arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
for (let i = 0; i < double_arr.length; i += 2)
result.push([ double_arr[i], double_arr[i+1] ]);
console.log(result);
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Closed 5 years ago.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
1.)If elements are equals,show that common elemenst in output
2.)The output(common elements) should be array form
Use Array#filter method and inside filter function use Array#indexOf or Array#includes methods to check second array includes the element.
var array1 = [1, 2, 3, 4, 5, 6];
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var res = array1.filter(function(v) { // iterate over the array
// check element present in the second array
return array2.indexOf(v) > -1;
// or array2.includes(v)
})
console.log(res);
This question already has answers here:
Get the last item in an array
(59 answers)
Closed 5 years ago.
This is the example of the array
arr = [1,2,3,4,5];
I expect to have this kind of result
arr = [5];
Use Splice/Slice function as below:
var arr = [1, 2, 3, 4, 5];
arr = arr.slice(-1);
console.log(arr);
OR
var arr = [1, 2, 3, 4, 5];
arr = arr.splice(arr.length - 1,1);
console.log(arr);
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 6 years ago.
Say I have 2 arrays:
var array1 = [1, 2, 3];
var array2 = [1, 2, 3, 4];
How do I compare them to single out the element ( in this case the number 4 )?
Something like:
if ( array1 == array2 ) {
//then do this
}else{
// find out the one that isn't the same in each and show it here
}
You can use Array.prototype.find to do the same
check this snippet
var array1 = [1, 2, 3];
var array2 = [1, 2, 3, 4];
function findNumber(number) {
return number=== 4?true:false;
}
var number=array2.find(findNumber);
if(number)
console.log("found");
else
console.log("not found");
Hope it helps
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);