You have to create an object. Assign the values to the object. Then push it into the array:
var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);
Answer from user3589620 on Stack OverflowVideos
You have to create an object. Assign the values to the object. Then push it into the array:
var nietos = [];
var obj = {};
obj["01"] = nieto.label;
obj["02"] = nieto.value;
nietos.push(obj);
Create an array of object like this:
var nietos = [];
nietos.push({"01": nieto.label, "02": nieto.value});
return nietos;
First you create the object inside of the push method and then return the newly created array.
Use the concat function, like so:
var arrayA = [1, 2];
var arrayB = [3, 4];
var newArray = arrayA.concat(arrayB);
The value of newArray will be [1, 2, 3, 4] (arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).
In ECMAScript 6, you can use the Spread syntax:
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1.push(...arr2);
console.log(arr1)
Spread syntax is available in all major browsers (that excludes IE11). For the current compatibility, see this (continuously updated) compatibility table.
However, see Jack Giffin's reply below for more comments on performance. It seems concat is still better and faster than the spread operator.
Your first snippet:
var row = new Array();
var newData = new Array();
for (i = 1; i <= 10 ; i++) {
row[0] = i;
newData.push(row);
}
pushes 10 times the reference to the array row, and at the same time changes the value contained in the position 0 of that array in turn to the values 1, 2, 3 .. 10, with the net result of setting the final contents of the row array to: [10].
The actual final value of newData is more correctly shown as:
[[10],[10],[10],[10],[10],[10],[10],[10],[10],[10]]
and not as:
[10,10,10,...,10]
In JavaScript, pushing an object into an array, (as in this case an instance of the Array class), actually pushes into the array a reference to the object. Pushing the same variable row doesn't create multiple instances of it.
This behavior is explained in detail here: Is JavaScript a pass-by-reference or pass-by-value language?.
The first code sample doesn't return an array with [10, 10, 10, etc..] but an array with a single item: [10]. The push method basically adds every object to the given array. It will not add the items of an array if the given object is an array. It simple adds its like an object to the array, and determines the index based on the existing items.
Another way to add items to your array is using an index. Here are two samples which both result in the same array.
var arr = [];
for(var i = 0; i < 5; i++) {
arr[i] = i;
}
[0, 1, 2, 3, 4]
var arr = [];
for(var i = 0; i < 5; i++) {
arr.push(i);
}
[0, 1, 2, 3, 4]
Hope this helps
Let's say I have the array of arrays:
groups = [ 0: ['FOLDER', 'file', 'last file'] 1: ['FOLDER', 'SUB folder', 'file', 'file', 'last file'] 2: ['SUB folder', 'file', 'last file'] 3: ['file', 'file', 'last file'] 4: ['FOLDER', 'last file', 'SUB folder', 'file', 'last file'] 5: ['last file'] ]
I want to push every sub-array in the "groups" array into the previous sub-array, if the sub-array's 1st item contains the 'last' or 'sub' strings. How do I do this?
This is what I'm trying but it doesn't work:
groups.forEach(function(group, i) {
if (group[0].includes('sub') || group[0].includes('last')) {
groups[i - 1].push(group);
}
}); So, for each sub-array in "groups" array, if the sub-array's first item contains string1 or string2, push it into the previous array in the loop.
Edit: thanks for the replies everyone, though I ended up going for a single one, I learned a bit about iteration from all of you.