Convert arrays of arrays to object with key
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have arrays of arrays which contains something like this:
var values = [[1, 2, 3], [3, 2, 1]]
I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:
values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]
What should I use? This is what Im came up so far:
const object1 = [[1,2,3],[3,2,1]],
object =
object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);
console.log(object)
javascript arrays
add a comment |Â
up vote
6
down vote
favorite
I have arrays of arrays which contains something like this:
var values = [[1, 2, 3], [3, 2, 1]]
I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:
values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]
What should I use? This is what Im came up so far:
const object1 = [[1,2,3],[3,2,1]],
object =
object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);
console.log(object)
javascript arrays
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have arrays of arrays which contains something like this:
var values = [[1, 2, 3], [3, 2, 1]]
I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:
values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]
What should I use? This is what Im came up so far:
const object1 = [[1,2,3],[3,2,1]],
object =
object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);
console.log(object)
javascript arrays
I have arrays of arrays which contains something like this:
var values = [[1, 2, 3], [3, 2, 1]]
I've tried .map() and Object.assign but I dont know how to implement it. I want this as an output:
values = [
{'up': 1, 'middle': 2, 'down': 3],
...
]
What should I use? This is what Im came up so far:
const object1 = [[1,2,3],[3,2,1]],
object =
object1.forEach(function(array)
object.map(value => ('up': array[0], 'mid': array[1], 'down': array[2]))
);
console.log(object)
javascript arrays
javascript arrays
edited 2 hours ago


Farooq Khan
1,16411029
1,16411029
asked 3 hours ago
Cheryl Blossom
487
487
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago
add a comment |Â
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Not very different from what others already did, but a little more elegant:
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
add a comment |Â
up vote
3
down vote
you can simply use Array.map()
, there is no need of forEach()
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
add a comment |Â
up vote
0
down vote
You can try this simple:
var values = [[1, 2, 3], [3, 2, 1]];
if(values.length)
values.forEach(function(valueArray, index)
var temp = ;
valueArray.forEach(function(value)
if(value == 1)temp.up = value;
if(value == 2)temp.middle = value;
if(value == 3)temp.down = value;
);
values[index] = temp;
);
console.log(values);
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Not very different from what others already did, but a little more elegant:
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
add a comment |Â
up vote
4
down vote
accepted
Not very different from what others already did, but a little more elegant:
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Not very different from what others already did, but a little more elegant:
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
Not very different from what others already did, but a little more elegant:
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
let arr = [[1, 2, 3], [3, 2, 1]];
let result = arr.map(([up, middle, down]) => (up, middle, down));
console.log(result);
answered 2 hours ago
Amit
34.9k74478
34.9k74478
add a comment |Â
add a comment |Â
up vote
3
down vote
you can simply use Array.map()
, there is no need of forEach()
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
add a comment |Â
up vote
3
down vote
you can simply use Array.map()
, there is no need of forEach()
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
add a comment |Â
up vote
3
down vote
up vote
3
down vote
you can simply use Array.map()
, there is no need of forEach()
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
you can simply use Array.map()
, there is no need of forEach()
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
let arr =[[1, 2, 3], [3, 2, 1]];
let result = arr.map((e)=>("up" : e[0], "mid" : e[1], "down" : e[2]));
console.log(result);
answered 2 hours ago
amrender singh
4,7361518
4,7361518
add a comment |Â
add a comment |Â
up vote
0
down vote
You can try this simple:
var values = [[1, 2, 3], [3, 2, 1]];
if(values.length)
values.forEach(function(valueArray, index)
var temp = ;
valueArray.forEach(function(value)
if(value == 1)temp.up = value;
if(value == 2)temp.middle = value;
if(value == 3)temp.down = value;
);
values[index] = temp;
);
console.log(values);
add a comment |Â
up vote
0
down vote
You can try this simple:
var values = [[1, 2, 3], [3, 2, 1]];
if(values.length)
values.forEach(function(valueArray, index)
var temp = ;
valueArray.forEach(function(value)
if(value == 1)temp.up = value;
if(value == 2)temp.middle = value;
if(value == 3)temp.down = value;
);
values[index] = temp;
);
console.log(values);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can try this simple:
var values = [[1, 2, 3], [3, 2, 1]];
if(values.length)
values.forEach(function(valueArray, index)
var temp = ;
valueArray.forEach(function(value)
if(value == 1)temp.up = value;
if(value == 2)temp.middle = value;
if(value == 3)temp.down = value;
);
values[index] = temp;
);
console.log(values);
You can try this simple:
var values = [[1, 2, 3], [3, 2, 1]];
if(values.length)
values.forEach(function(valueArray, index)
var temp = ;
valueArray.forEach(function(value)
if(value == 1)temp.up = value;
if(value == 2)temp.middle = value;
if(value == 3)temp.down = value;
);
values[index] = temp;
);
console.log(values);
answered 2 hours ago
Nitin Kumar
405
405
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53002477%2fconvert-arrays-of-arrays-to-object-with-key%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
what is the expected output. always put 1st, 2nd or 3rd index or sort it first then arrange?
– Farooq Khan
2 hours ago