I need to add ann array to an existing array.
If i have an array
var array = ["foo", "baah"];
then, what i need to do is to add an array into the existing one. So it ends up looking like
var array = ["foo", "baah", ["newfoo", "newbaah"]];
i need to do it client site, so using javascript or JQuery. Anyone who can help me? If i can't add a array to a array, is i then possible to add an object
Something like
var array = ["foo", "baah", /*myObject containing 2 item*/];
just ask for more info if needed.
Just .push will do it
var array1 = ["foo", "baah"];
var array2 = ["newfoo", "newbaah"];
array1.push(array2)
console.log(array1)
const arrOne = [1,2,3];
const arrTwo = [4,5];
arrOne[arrOne.length] = arrTwo;
console.log(arrOne);
You don't need to use .push(), you can use index notation.
Just use push:
var array1 = ["foo", "baah"];
var array2 = ["newfoo", "newbaah"];
array1.push(array2);
console.log(array1);
You can do it via ... spread operator.
var array1 = ["foo", "baah"];
var array2 = ["newfoo", "newbaah"];
var array3 = [...array1, array2];
console.log(array3);
.push will mutate your first array but this one doesn't.
Push
You can use push to add one array to another
var a = [1,2]
var b =[3,4]
a.push(b) //[1,2,[3,4]]
unshift
This is similar to push but adds the array to the first
a.push(b) //[[3,4],1,2]
concat
a.concat(b)//[1,2,3,4]
concat returns a new array rather than push and unshift which adds in the existing array
Related
I have to following array:
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
And I would like to convert it to an array of arrays, as follows:
var arr1 = [
["0.1","0"],
["1.1","1"],
["2.1","2"]
];
I would like to convert to the other array only a few properties, not all of them.
Any idea how I can do this?
PS: I was using flatMap from another post but it does not work as it does not exist in Edge.
Assuming that the second value in each subarray is coming from the number after the period from the m key, you could use the map function to accomplish this:
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
var newArray = arr1.map(x => [x[0].n, x[0].m.split('.')[1]]);
console.log(newArray);
For next time you have to put your attempts.
this is the solution for your problem
var arr1 = [
[{n:"0.1",m:"m.0",other:"eg1"}],
[{n:"1.1",m:"m.1",other:"eg2"}],
[{n:"2.1",m:"m.2",other:"eg3"}]
];
arr1 = arr1.map(currentArray=>{
const item = currentArray.shift();
return [item.n,item.m.replace( /^\D+/g, '')]
});
I have a multi dimensional array like this.
var myArray = [['aaa','1','2.33','44'],['bbb','1','2.33','44'],['ccc','1','2.33','44']]
I want to remove all the first element to get a result like this.
var myArray = [['1','2.33','44'],['1','2.33','44'],['1','2.33','44']]
Please Help me. Thanks
Use .forEach to loop over the nested arrays and then .splice them. Splice will remove the first item in the nested array and effect the current array.
var myArray = [['aaa','1','2.33','44'],['bbb','1','2.33','44'],['ccc','1','2.33','44']];
myArray.forEach(array => array.splice(0,1));
console.log(myArray);
Base on the comment you can also use .shift() function to remove the first item.
var myArray = [['aaa','1','2.33','44'],['bbb','1','2.33','44'],['ccc','1','2.33','44']];
myArray.forEach(array => array.shift());
console.log(myArray);
You can try this
var myArray = [['aaa','1','2.33','44'],['bbb','1','2.33','44'],['ccc','1','2.33','44']];
var done = function(){
console.log(myArray);
};
myArray.forEach(function(array){
array.splice(0,1);
done();
});
Output
[['1','2.33','44'],['1','2.33','44'],['1','2.33','44']];
I have two arrays now once i rendered the data from backend i want to push just object from array2 to array1 not the array itself.
How can i just push object from array2 to array1 I dont want to push as array.
ctrl.js
var array1 = [{name:'john', address:'cliffwood ave'}]
var array2 = [{name:'Mike', address:'florence ave'}]
array1.push(array2);
If you want to mutate array1:
array1.push.apply(array1, array2);
Otherwise:
var array3 = array1.concat(array2);
If you wanted to push a single object in the array, you could just reference it by it's specific index :
array1.push(array2[0]);
Otherwise, if you wanted to push all of the items, you might consider just concatenating them via the concat() function :
array1.concat(array2);
If you want to use ES6 you can use the spread operator:
array1.push(...array2);
which is functionally equivalent to this ES5 method...
array1.push.apply(array1, array2);
...mentioned in one of the other answers.
DEMO
To push {name:'Mike', address:'florence ave'} into array1 :
array1.push(array2[0]);
As the object you want is simply the first element in your array2 variable.
(var I = 0; I <= array2.length-1 ; I++){
array1.push(array2[I])
}
I am curious to know how I can quickly and most efficiently remove a number of items from an array in JavaScript without creating a loop.
EXAMPLE:
var array = [1,2,3,4,5,6,7,8,9];
array.remove[0..4]; //pseudo code
console.log(array);//result would then be [6,7,8,9]
Is there a function for this, or is a custom loop required? Rudimentary question I suppose, but just wondering out of curiosity.
Use Array#splice:
var array = [1,2,3,4,5,6,7,8,9];
array.splice(0, 4); // returns [1,2,3,4]
console.log(array); // logs [5,6,7,8,9]
You could just use .slice() on the array.
var array = [1,2,3,4,5,6,7,8,9];
array = array.slice(5,array.length);
Using filter method
var a = [1,2,3,4,5,6,7,8,9], b = [];
b = a.filter(function(element, index){ return index > 4 });
Output of b[]
[6,7,8,9]
I have 3 arrays like:
var arr1 = [];
var arr2 = [];
var arr3 = [];
//When I want to add something into array then I use
arr1.push("text");
arr2.push("text");
but is it possible to make something like the following example?
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(item);
is it even possible to do something like that? I have tried but does not work.
There's a fundamental misunderstanding though, arr is an array but you're using it as an associative array, which in JavaScript is better represented with an object {}. for...in is for objects, NOT arrays, the MDN has a warning note about it:
for..in should not be used to iterate over an Array where index order
is important...
I would advice even if index is trivial to use a regular for loop or a forEach.
Consider using the following, more appropiate approach.
var obj = {
group1: ['text1'],
group2: ['text2'],
group3: ['text3']
};
// pushing more strings
obj.group1.push('foo');
obj['group2'].push('baz');
You're treating arr['group1'] as an array (by using .push()), but you haven't declared it as an array.
var arr = [];
arr['group1'] = [];
arr['group2'] = [];
arr['group3'] = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
It seems you're actually looking for Javascript Objects instead of arrays.
Also, you need to create these objects first.
var obj = {group1:[],group2:[],group3:[]};
/* or
var obj = {};
obj.group1 = [];
*/
obj['group1'].push("text1");
// or obj.group1.push("text1");
The for...in structure sets your for variable to the key, not the value. Assuming arr['group1'] is an array, this will work fine:
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(arr['group1'][item]);