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, '')]
});
Related
I know that if there is an array of values it must be used this approach:
console.log(['joe', 'jane', 'mary'].includes('jane')); // true
But in case of an array of arrays, is there a short way to do it? Without other computations between.
For this input:
[['jane'],['joe'],['mary']]
You can use flat method to flatten the array. For more neted array, you can also mention depth like flat(depth)
let arr = [["jane"],["joe"],["mary"]];
arr.flat().includes('jane'); //true
You can easily achieve this result using some
arr.some((a) => a.includes("jane"))
const arr = [
["jane"],
["joe"],
["mary"]
];
const arr2 = [
["joe"],
["mary"]
];
console.log(arr.some((a) => a.includes("jane")));
console.log(arr2.some((a) => a.includes("jane")));
it can also be done by first flattening the 2d arrays in 1 d aaray and then using includes to find whether the array contains the element or not
var arr = [['jane'],['joe'],['marry']]
var newarr=[].concat(...arr)
var v=newarr.includes('jane')
console.log(v)
I have two array of objects: - better solution
array1= [{id:1,name:"samsung"},{id:2,name:"nokia"},{id:3,name:"Lg"}];
array2 = [{id:5,name:"samsung"},{id:2,name:"panasonics"},{id:7,name:"Lg"}];
Expected output be:
if first array and second array id matches means take the second array name
in above example id 2 matches and we need id:2,name: panasonics.
o/p:
[{id:1,name:"samsung"},{id:2,name:"panasonics"},{id:3,name:"Lg"},{id:5,name:"samsung"},{id:7,name:"Apple"}]
Combine the arrays using Array.concat(), reduce them into a Map by id, and then convert the Map's values to an array with Array.from():
const unionBy = (field, ...arrays) => Array.from(
[].concat(...arrays)
.reduce((r, o) => r.set(o.id, o), new Map)
.values()
);
const array1 = [{id:1,name:"samsung"},{id:2,name:"nokia"},{id:3,name:"Lg"}];
const array2 = [{id:5,name:"samsung"},{id:2,name:"panasonics"},{id:7,name:"Lg"}];
const result = unionBy('id', array1, array2);
console.log(result);
You can use a simple .forEach() loop like below (you can also use a for loop if you want, but .forEach() is easier).
This code loops through array1, and loops through array2 in that loop. It then checks if the ids are the same. If there are, the name is appended to result.
const array1= [{id:1,name:"samsung"},{id:2,name:"nokia"},{id:3,name:"Lg"}];
const array2 = [{id:5,name:"samsung"},{id:2,name:"panasonics"},{id:7,name:"Lg"}];
let result = [];
array1.forEach(e1 => {
array2.forEach(e2 => {
if (e1.id == e2.id) {
result.push(e2.name);
}
});
});
console.log(result);
Use map() and concat() like the following code
array1= [{id:1,name:"samsung"},{id:2,name:"nokia"},{id:3,name:"Lg"}];
array2 = [{id:5,name:"samsung"}, {id:2,name:"panasonics"},{id:7,name:"Lg"}];
var array3=array1.map(function(i,v){
if(array2[v].id==i.id){
return array2[v]
}
else return i
})
array4=array3.concat(array2);
console.log(array4);
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)
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 have an Object array that looks like the following:
var array = [
{'id':1,'description':test},
{'id':2,'description':test},
{'id':3,'description':test}
]
And I want to convert it to look like this:
var newArray = [
1, 2, 3
]
So it's basically taking the object array, extracting the id's out of the objects, and creating a new array that contains the id's that were extracted. Is there a way to do this in one line? Even better if a new array doesn't have to get created and array is just updated.
var test ="sai";
var array = [
{'id':1,'description':test},
{'id':2,'description':test},
{'id':3,'description':test}
]
console.log(array.map(function(obj){return obj.id}))
iterate the array using foreach and populate newArray
var newArray = [];
array.forEach(funtion(element){
newArray.push(element.id);
});
console.log( newArray );
array.map(function (item) {
return item["id"];
}
If you'd like to have anew instance of the mapped array:
var newarray = [];
array.forEach(function (item) {
newarray.push(item["id"]);
}
array.map(function(element) {return element.id})
OP Requirement: better if array is just updated
array = array.map(function(element) {return element.id})