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);
Related
I want to unshift on a copy of the array, but the original array modifies too. Why is that?
var array1 = [1, 2, 3]
var array2 = array1
array2.unshift(4, 5)
console.log(array1)
console.log(array2)
Try using Array.from()
var array1 = [1, 2, 3]
var array2 = Array.from(array1)
array2.unshift(4, 5)
console.log(array1)
console.log(array2)
Use spread operator( ... ) to make second array. It will make a new array with new reference. And then perform your task.
var array1 = [1, 2, 3];
var array2 = [...array1];
array2.unshift(4, 5);
console.log(array1);
console.log(array2);
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;
}
How do I change a JS array in place (like a Ruby "dangerous" method, e.g. with trailing !)
Example:
If I have this:
var arr = [1, 2, 3]
How can I make this:
arr === [2, 4, 6]
(assuming I have an appropriate function for doubling numbers) in one step, without making any more variables?
Use Array.prototype.forEach() , third parameter is this : input array
var arr = [1, 2, 3];
arr.forEach(function(el, index, array) {
array[index] = el * 2
});
console.log(arr)
A smart Array.prototype.map() and an assignment will do.
The map() method creates a new array with the results of calling a provided function on every element in this array.
var arr = [1, 2, 3];
arr = arr.map(function (a) {
return 2 * a;
});
document.write('<pre>' + JSON.stringify(arr, 0, 4) + '</pre>');
map() returns a new array, but it can also modify the array in-place if the callback function works on the array's elements:
function double(el, i, array) {
array[i]= el * 2;
} //double
var arr= [1, 2, 3];
arr.map(double);
console.log(arr); // [2, 4, 6]
I know how to initliaize one but how do add I items to an Array? I heard it was push() maybe? I can't find it...
For JavaScript arrays, you use push().
var a = [];
a.push(12);
a.push(32);
For jQuery objects, there's add().
$('div.test').add('p.blue');
Note that while push() modifies the original array in-place, add() returns a new jQuery object, it does not modify the original one.
push is a native javascript method. You could use it like this:
var array = [1, 2, 3];
array.push(4); // array now is [1, 2, 3, 4]
array.push(5, 6, 7); // array now is [1, 2, 3, 4, 5, 6, 7]
You are right. This has nothing to do with jQuery though.
var myArray = [];
myArray.push("foo");
// myArray now contains "foo" at index 0.
For JavaScript arrays, you use Both push() and concat() function.
var array = [1, 2, 3];
array.push(4, 5); //use push for appending a single array.
var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var array3 = array1.concat(array2); //It is better use concat for appending more then one array.
just it jquery
var linkModel = {
Link: "",
Url: "",
Summary: "",
};
var model = [];
for (let i = 1; i < 2; i++) {
linkModel.Link = "Test.com" + i;
linkModel.Url= "www.Test.com" + i;
linkModel.Summary= "Test is add" + i;
model.Links.push(linkModel);
}
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);