Best way to concat an array to to another array - javascript

Totally I have 7 arrays . my first array contains only one item which has to be inserted to the 0th index of all other 6 arrays . while searching through questions related to this I found a.concat(b) will be better than a.unshift(b). Is there any other best way available to perform this function .

Simplest way to merge multiple arrays:
var array1 = [1.79 , 2.33 , 3.1];
var array2 = [2];
var multipleArrays = [array1, array2];
var flatArray = [].concat.apply([], multipleArrays);
console.log(flatArray);

var array1=[1]
var array2=[2]
var array3=[3]
var array4=[4]
var array5=[5]
var array6=[6]
var array7=[7]
var concatedArray = array1.concat(array2).concat(array3).concat(array4).concat(array5).concat(array6).concat(array7);
console.log(concatedArray)

Related

Is there away to push the same element into 2 different arrays?

I want to push the same element into 2 different arrays without repeating myself and keeping the code dry. Is there any way of doing this with JS?
var arrayA = [];
var arrayB = [];
var a = "a";
arrayA.push(a)
arrayB.push(a)
Can I combine those 2 last lines into one? I was thinking something like:
[arrayA, arrayB].push(a) ? But this doesn't seem to work
Thanks
A bit of an overkill, but you can wrap them both in an array, and use Array.forEach() to push the item to the original arrays:
var arrayA = [];
var arrayB = [];
var a = "a";
[arrayA, arrayB].forEach(arr => arr.push(a));
console.log({
arrayA,
arrayB
});

Convert Array of arrays of objects to Array of Arrays

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, '')]
});

Giving an array of indexes, how to push items from one array to another depending on the indexes that are specified?

I am trying to push items from one Array to another depending on the order that is supplied. Essentially i have a 2d array with a name and a price :
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
Another array with the order it should be in :
var myOrder = [0,2,1];
My resulting array would look like this :
var finalArray = [['Apples',22],['Berry',23],['Orange',55]]
My initial thought process was to loop through myArray and loop through myOrder , store the object temporary at a specified index in myOrder then push to final array. I think i am over thinking it a bit, i made several attempts but with no luck whatsoever. Any help would be greatly appreciated!
This is a simple map() that doesn't require anything else
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
let final = myOrder.map(i => myArray[i])
console.log(final)
The optimal way appears to me to be:
Initialize empty finalArray
Loop over your myOrder array
2.1. Push myArray[index] to finalArray
Like so:
let finalArray = [];
for(let index of myOrder) {
finalArray.push(myArray[index]);
}
Review the for...of syntax if you're not familiar with it.
You can use splice to insert so long as the same number of elements are present in both the arrays.
You iterate over the myOrder array and then use splice, to which the index of the new array is the current value of the iteration and then use array present in the index position of myArray
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
var finalArray = [];
myOrder.forEach(function(val, index) {
finalArray.splice(val, 0, myArray[index]);
});
console.log(finalArray);
Easy enough using .reduce:
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
function reorder(array, order) {
return order.reduce((newArray, orderIndex) => {
newArray.push(array[orderIndex]);
return newArray;
}, []);
}
console.log(reorder(myArray, myOrder))
function reorder(arr, order) {
return order.map(function(i) {
return arr[i];
});
}
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
reorder(myArray, myOrder); // => [["Apples",22],["Berry",23],["Orange",55]]
One of way solving this will be
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
var finalArray;
for (x in myOrder) {
finalArray[x] = myArray[myOrder[x]];
}
This is a beginning level solution. Also you use libraries available for java script such as underscore.js(http://underscorejs.org/) for such operations on Array and Object.
Also you can use ECMA 6, for doing this which will reduce your line of coding.
Example-
var myArray = [['Apples',22],['Orange',55],['Berry',23]];
var myOrder = [0,2,1];
let finalArray = myOrder.map(i => myArray[i])
This is the new way of coding in javascript.
In my point of view, it will be easy if you learn latest version of Java script(ECMAscript 6)

Merge two arrays , So that both 1st index is in new array and so on

I have two arrays like this
1st array is
array1[
0:"7#a.com"
1:"6#live.com"
2:"5#live.com"
3:"55#a.com"
4:"4#live.com"
5:"3#live.com"
6:"62#a.com"
7:"61#a.com"
8:"61#a.com"
9:"59#a.com"
10:"58#a.com"
And array two in same way like this
0"5223b14d-1682-4777-8ada-c5b0c972756f"
1:"290c79e8-62da-46f1-bff5-157031a079fa"
2:"eb4ff5f9-82c7-4095-8116-1c34f1755d06"
3:"987c51f4-23f1-4783-8061-a47dfff16fe3"
4:"afcf5ef1-a87d-465f-bb1b-f8db81ecc178"
5:"2d299d60-9481-4c3f-9b9b-e6659ee74d07"
6:"1f40b5ef-990f-4edd-925c-b511dd64899f"
7:"88de17fd-286f-4960-9e4f-a2b01425da82"
8:"1bb181cb-ab7c-47af-95a6-99357459e6a3"
9:"9554f7c2-a20d-4ff7-add6-8840b2d06a89"
10:"15e51955-9eb9-4c4b-990f-97e29820e04d"
now I want to make 10 new arrays with combinations of indexs of both like
newarray=[array1[0],array2[0]];
I got the algo but dont know how can I loop through on both. as I tried but got just words of both. And I tried 3,4 soloution on stak overflow but didnt work for me .
If the arrays length is equal, you can just use map() method to loop over one of them and return a custom array with both elements:
var results = arr1.map(function(el, index){
return [el, arr2[index]];
});
Demo:
var arr1 = ["7#a.com","6#live.com","5#live.com","55#a.com","4#live.com","3#live.com","62#a.com","61#a.com","61#a.com","59#a.com","58#a.com"];
var arr2 = ["5223b14d-1682-4777-8ada-c5b0c972756f","290c79e8-62da-46f1-bff5-157031a079fa","eb4ff5f9-82c7-4095-8116-1c34f1755d06","987c51f4-23f1-4783-8061-a47dfff16fe3","afcf5ef1-a87d-465f-bb1b-f8db81ecc178","2d299d60-9481-4c3f-9b9b-e6659ee74d07","1f40b5ef-990f-4edd-925c-b511dd64899f","88de17fd-286f-4960-9e4f-a2b01425da82","1bb181cb-ab7c-47af-95a6-99357459e6a3","9554f7c2-a20d-4ff7-add6-8840b2d06a89","15e51955-9eb9-4c4b-990f-97e29820e04d"];
var results = arr1.map(function(el, index){
return [el, arr2[index]];
});
console.log(results)
You can map them into pairs of values:
var array1 = ["7#a.com","6#live.com","5#live.com","55#a.com","4#live.com","3#live.com","62#a.com","61#a.com","61#a.com","59#a.com","58#a.com"];
var array2 = ["5223b14d-1682-4777-8ada-c5b0c972756f","290c79e8-62da-46f1-bff5-157031a079fa","eb4ff5f9-82c7-4095-8116-1c34f1755d06","987c51f4-23f1-4783-8061-a47dfff16fe3","afcf5ef1-a87d-465f-bb1b-f8db81ecc178","2d299d60-9481-4c3f-9b9b-e6659ee74d07","1f40b5ef-990f-4edd-925c-b511dd64899f","88de17fd-286f-4960-9e4f-a2b01425da82","1bb181cb-ab7c-47af-95a6-99357459e6a3","9554f7c2-a20d-4ff7-add6-8840b2d06a89","15e51955-9eb9-4c4b-990f-97e29820e04d"];
var result = array1.map(function(v1, i) {
return [v1, array2[i]];
});
console.log(result);
array1 = [ "7#a.com", "6#live.com", "5#live.com", "55#a.com", "4#live.com", "3#live.com", "62#a.com", "61#a.com", "61#a.com", "59#a.com", "58#a.com"]; array2 = [
"5223b14d-1682-4777-8ada-c5b0c972756f", "290c79e8-62da-46f1-bff5-157031a079fa", "eb4ff5f9-82c7-4095-8116-1c34f1755d06", "987c51f4-23f1-4783-8061-a47dfff16fe3", "afcf5ef1-a87d-465f-bb1b-f8db81ecc178", "2d299d60-9481-4c3f-9b9b-e6659ee74d07", "1f40b5ef-990f-4edd-925c-b511dd64899f", "88de17fd-286f-4960-9e4f-a2b01425da82", "1bb181cb-ab7c-47af-95a6-99357459e6a3", "9554f7c2-a20d-4ff7-add6-8840b2d06a89", "15e51955-9eb9-4c4b-990f-97e29820e04d"];
var newArray = [];
for (var i=0; i<array1.length; i++)
newArray.push([ array1[i], array2[i]]);
console.log( newArray );

How to remove multiple sequential items of an array - JavaScript

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]

Categories