Related
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)
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)
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 );
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]);