how to convert multidimention array to arr in object
up vote
1
down vote
favorite
can you explain the logic how to convert this 2d array to array and in array has objects, here is the input :
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
how to convert them to be:
obj = [
{
name:"Tony",
first:"a",
second:"b"
},
{
name:"Sara",
first:"c",
second:"z"
}
]
should we create 2 objects temporary and are ? to put them in the array,
and how about looping? can we just use one-time looping?
and how if that 2d array length is not same with ther 2d on the first or the second,
i do love to know the method, with explaning if you dont mind,
and i do like you all dont use ES6 for this :), so i know the logic
javascript arrays object for-loop multidimensional-array
add a comment |
up vote
1
down vote
favorite
can you explain the logic how to convert this 2d array to array and in array has objects, here is the input :
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
how to convert them to be:
obj = [
{
name:"Tony",
first:"a",
second:"b"
},
{
name:"Sara",
first:"c",
second:"z"
}
]
should we create 2 objects temporary and are ? to put them in the array,
and how about looping? can we just use one-time looping?
and how if that 2d array length is not same with ther 2d on the first or the second,
i do love to know the method, with explaning if you dont mind,
and i do like you all dont use ES6 for this :), so i know the logic
javascript arrays object for-loop multidimensional-array
It turns out you don't even want to use ES5, like.map
!?
– trincot
Nov 10 at 9:16
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
can you explain the logic how to convert this 2d array to array and in array has objects, here is the input :
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
how to convert them to be:
obj = [
{
name:"Tony",
first:"a",
second:"b"
},
{
name:"Sara",
first:"c",
second:"z"
}
]
should we create 2 objects temporary and are ? to put them in the array,
and how about looping? can we just use one-time looping?
and how if that 2d array length is not same with ther 2d on the first or the second,
i do love to know the method, with explaning if you dont mind,
and i do like you all dont use ES6 for this :), so i know the logic
javascript arrays object for-loop multidimensional-array
can you explain the logic how to convert this 2d array to array and in array has objects, here is the input :
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
how to convert them to be:
obj = [
{
name:"Tony",
first:"a",
second:"b"
},
{
name:"Sara",
first:"c",
second:"z"
}
]
should we create 2 objects temporary and are ? to put them in the array,
and how about looping? can we just use one-time looping?
and how if that 2d array length is not same with ther 2d on the first or the second,
i do love to know the method, with explaning if you dont mind,
and i do like you all dont use ES6 for this :), so i know the logic
javascript arrays object for-loop multidimensional-array
javascript arrays object for-loop multidimensional-array
edited Nov 10 at 9:11
asked Nov 10 at 9:08
Zr Classic
676
676
It turns out you don't even want to use ES5, like.map
!?
– trincot
Nov 10 at 9:16
add a comment |
It turns out you don't even want to use ES5, like.map
!?
– trincot
Nov 10 at 9:16
It turns out you don't even want to use ES5, like
.map
!?– trincot
Nov 10 at 9:16
It turns out you don't even want to use ES5, like
.map
!?– trincot
Nov 10 at 9:16
add a comment |
6 Answers
6
active
oldest
votes
up vote
4
down vote
accepted
Use Array.map()
. In the map's callback, extract the values to variables, using array destructuring, than create the object with shorthand property names:
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
And if you don't want to use Array.map()
, you can build one using a for...of
loop:
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
Map is not magic at all. You can build a map easily using afor...loop
- awanderingreader.com/…
– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
add a comment |
up vote
3
down vote
You can use .map()
with some array destructuring:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alternatively, you can use a simple for loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
add a comment |
up vote
2
down vote
var arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
function parseData(input){
var output = ;
for(var i = 0; i< input.length ; i++){
output.push({name:input[i][0],first:input[i][1],second:input[i][2]})
}
return output;
}
console.log(parseData(arr));
EDIT - Explanation
As the input is structured as 2D array with inner array is of fixed length of 3 and outer is of non-negative ( >= 0 ). Iterate over the outer array using for
loop and inner array using the index number and store the result in output
array.
JsFiddle demo - https://jsfiddle.net/53umf8rv/
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
add a comment |
up vote
1
down vote
Without Map:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
obj=;
for(innerArr of arr)
{
obj.push({"name":innerArr[0],"first":innerArr[1],"second":innerArr[2]});
}
console.log(obj);
add a comment |
up vote
1
down vote
Without using higher order functions you can achieve this using a for of
loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
add a comment |
up vote
1
down vote
You could use an outer for ... of
statement, which iterates the items of the array and iterate the inner array by using a classic for
statement with an index variable, which is used to get the corresponding key value as well.
For each inner iteration take a new property for the temporary object and assign a value. At the end of the inner iteration push the temporary object to the result set.
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Use Array.map()
. In the map's callback, extract the values to variables, using array destructuring, than create the object with shorthand property names:
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
And if you don't want to use Array.map()
, you can build one using a for...of
loop:
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
Map is not magic at all. You can build a map easily using afor...loop
- awanderingreader.com/…
– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
add a comment |
up vote
4
down vote
accepted
Use Array.map()
. In the map's callback, extract the values to variables, using array destructuring, than create the object with shorthand property names:
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
And if you don't want to use Array.map()
, you can build one using a for...of
loop:
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
Map is not magic at all. You can build a map easily using afor...loop
- awanderingreader.com/…
– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Use Array.map()
. In the map's callback, extract the values to variables, using array destructuring, than create the object with shorthand property names:
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
And if you don't want to use Array.map()
, you can build one using a for...of
loop:
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
Use Array.map()
. In the map's callback, extract the values to variables, using array destructuring, than create the object with shorthand property names:
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
And if you don't want to use Array.map()
, you can build one using a for...of
loop:
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = arr.map(([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
const map = (arr, cb) => {
const r = ;
for(const item of arr) {
r.push(cb(item));
}
return r;
};
const arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
const result = map(arr, ([name, first, second]) => ({
name,
first,
second
}));
console.log(result);
edited Nov 10 at 9:28
answered Nov 10 at 9:10
Ori Drori
71.3k127388
71.3k127388
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
Map is not magic at all. You can build a map easily using afor...loop
- awanderingreader.com/…
– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
add a comment |
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
Map is not magic at all. You can build a map easily using afor...loop
- awanderingreader.com/…
– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
how about if we dont use map?? @ori
– Zr Classic
Nov 10 at 9:10
2
2
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
What's wrong with map?
– Ori Drori
Nov 10 at 9:12
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
map is like a magic, i want to learn by logic and by old style method, :D,
– Zr Classic
Nov 10 at 9:14
2
2
Map is not magic at all. You can build a map easily using a
for...loop
- awanderingreader.com/…– Ori Drori
Nov 10 at 9:16
Map is not magic at all. You can build a map easily using a
for...loop
- awanderingreader.com/…– Ori Drori
Nov 10 at 9:16
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
@ZrClassic - I've added an example of a magicless map using for...of.
– Ori Drori
Nov 10 at 9:21
add a comment |
up vote
3
down vote
You can use .map()
with some array destructuring:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alternatively, you can use a simple for loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
add a comment |
up vote
3
down vote
You can use .map()
with some array destructuring:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alternatively, you can use a simple for loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
add a comment |
up vote
3
down vote
up vote
3
down vote
You can use .map()
with some array destructuring:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alternatively, you can use a simple for loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use .map()
with some array destructuring:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Alternatively, you can use a simple for loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = arr.map(([name, first, second]) => ({name, first, second}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
const result = ;
for(var i = 0; i < arr.length; i++) {
result.push({
name: arr[i][0],
first: arr[i][1],
second: arr[i][2]
});
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
edited Nov 10 at 9:19
answered Nov 10 at 9:12
Mohammad Usman
19k103653
19k103653
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
add a comment |
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
how about if we dont use map?? @MohammadUsman
– Zr Classic
Nov 10 at 9:13
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
@ZrClassic I've updated my answer.
– Mohammad Usman
Nov 10 at 9:18
add a comment |
up vote
2
down vote
var arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
function parseData(input){
var output = ;
for(var i = 0; i< input.length ; i++){
output.push({name:input[i][0],first:input[i][1],second:input[i][2]})
}
return output;
}
console.log(parseData(arr));
EDIT - Explanation
As the input is structured as 2D array with inner array is of fixed length of 3 and outer is of non-negative ( >= 0 ). Iterate over the outer array using for
loop and inner array using the index number and store the result in output
array.
JsFiddle demo - https://jsfiddle.net/53umf8rv/
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
add a comment |
up vote
2
down vote
var arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
function parseData(input){
var output = ;
for(var i = 0; i< input.length ; i++){
output.push({name:input[i][0],first:input[i][1],second:input[i][2]})
}
return output;
}
console.log(parseData(arr));
EDIT - Explanation
As the input is structured as 2D array with inner array is of fixed length of 3 and outer is of non-negative ( >= 0 ). Iterate over the outer array using for
loop and inner array using the index number and store the result in output
array.
JsFiddle demo - https://jsfiddle.net/53umf8rv/
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
add a comment |
up vote
2
down vote
up vote
2
down vote
var arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
function parseData(input){
var output = ;
for(var i = 0; i< input.length ; i++){
output.push({name:input[i][0],first:input[i][1],second:input[i][2]})
}
return output;
}
console.log(parseData(arr));
EDIT - Explanation
As the input is structured as 2D array with inner array is of fixed length of 3 and outer is of non-negative ( >= 0 ). Iterate over the outer array using for
loop and inner array using the index number and store the result in output
array.
JsFiddle demo - https://jsfiddle.net/53umf8rv/
var arr = [["Tony", "a", "b"], ["Sara", "c", "z"]];
function parseData(input){
var output = ;
for(var i = 0; i< input.length ; i++){
output.push({name:input[i][0],first:input[i][1],second:input[i][2]})
}
return output;
}
console.log(parseData(arr));
EDIT - Explanation
As the input is structured as 2D array with inner array is of fixed length of 3 and outer is of non-negative ( >= 0 ). Iterate over the outer array using for
loop and inner array using the index number and store the result in output
array.
JsFiddle demo - https://jsfiddle.net/53umf8rv/
edited Nov 10 at 13:29
answered Nov 10 at 9:17
front_end_dev
1,3151511
1,3151511
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
add a comment |
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
1
1
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
Since your answer may be a good answer, it still needs more explainations about the changes and how it works.
– Tân Nguyễn
Nov 10 at 10:53
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
@TânNguyễn Explanation added as requested.
– front_end_dev
Nov 10 at 13:30
add a comment |
up vote
1
down vote
Without Map:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
obj=;
for(innerArr of arr)
{
obj.push({"name":innerArr[0],"first":innerArr[1],"second":innerArr[2]});
}
console.log(obj);
add a comment |
up vote
1
down vote
Without Map:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
obj=;
for(innerArr of arr)
{
obj.push({"name":innerArr[0],"first":innerArr[1],"second":innerArr[2]});
}
console.log(obj);
add a comment |
up vote
1
down vote
up vote
1
down vote
Without Map:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
obj=;
for(innerArr of arr)
{
obj.push({"name":innerArr[0],"first":innerArr[1],"second":innerArr[2]});
}
console.log(obj);
Without Map:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
obj=;
for(innerArr of arr)
{
obj.push({"name":innerArr[0],"first":innerArr[1],"second":innerArr[2]});
}
console.log(obj);
answered Nov 10 at 9:13
Rohith K N
46648
46648
add a comment |
add a comment |
up vote
1
down vote
Without using higher order functions you can achieve this using a for of
loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
add a comment |
up vote
1
down vote
Without using higher order functions you can achieve this using a for of
loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
add a comment |
up vote
1
down vote
up vote
1
down vote
Without using higher order functions you can achieve this using a for of
loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
Without using higher order functions you can achieve this using a for of
loop:
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
const arr = [
["Tony", "a", "b"],
["Sara", "c", "z"]
];
let obj_arr = ;
for(inner_arr of arr) {
obj_arr = [...obj_arr, {name: inner_arr[0], first: inner_arr[1], second: inner_arr[2]}];
}
console.log(obj_arr);
answered Nov 10 at 9:17
Nick Parsons
2,7082619
2,7082619
add a comment |
add a comment |
up vote
1
down vote
You could use an outer for ... of
statement, which iterates the items of the array and iterate the inner array by using a classic for
statement with an index variable, which is used to get the corresponding key value as well.
For each inner iteration take a new property for the temporary object and assign a value. At the end of the inner iteration push the temporary object to the result set.
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
add a comment |
up vote
1
down vote
You could use an outer for ... of
statement, which iterates the items of the array and iterate the inner array by using a classic for
statement with an index variable, which is used to get the corresponding key value as well.
For each inner iteration take a new property for the temporary object and assign a value. At the end of the inner iteration push the temporary object to the result set.
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
add a comment |
up vote
1
down vote
up vote
1
down vote
You could use an outer for ... of
statement, which iterates the items of the array and iterate the inner array by using a classic for
statement with an index variable, which is used to get the corresponding key value as well.
For each inner iteration take a new property for the temporary object and assign a value. At the end of the inner iteration push the temporary object to the result set.
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
You could use an outer for ... of
statement, which iterates the items of the array and iterate the inner array by using a classic for
statement with an index variable, which is used to get the corresponding key value as well.
For each inner iteration take a new property for the temporary object and assign a value. At the end of the inner iteration push the temporary object to the result set.
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
function convert(array, keys) {
var result = ,
items,
i,
temp;
for (items of array) {
temp = {};
for (i = 0; i < items.length; i++) {
temp[keys[i]] = items[i];
}
result.push(temp);
}
return result;
}
console.log(convert([["Tony", "a", "b"], ["Sara", "c", "z"]], ["name", "first", "second"]));
answered Nov 10 at 10:04
Nina Scholz
170k1383147
170k1383147
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237495%2fhow-to-convert-multidimention-array-to-arr-in-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It turns out you don't even want to use ES5, like
.map
!?– trincot
Nov 10 at 9:16