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










share|improve this question
























  • It turns out you don't even want to use ES5, like .map!?
    – trincot
    Nov 10 at 9:16















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










share|improve this question
























  • It turns out you don't even want to use ES5, like .map!?
    – trincot
    Nov 10 at 9:16













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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








share|improve this answer























  • 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 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


















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; }








share|improve this answer























  • 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


















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/






share|improve this answer



















  • 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


















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





share|improve this answer




























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








    share|improve this answer




























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








      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        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

























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








        share|improve this answer























        • 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 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















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








        share|improve this answer























        • 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 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













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








        share|improve this answer














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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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 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


















        • 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 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
















        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












        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; }








        share|improve this answer























        • 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















        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; }








        share|improve this answer























        • 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













        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; }








        share|improve this answer














        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; }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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


















        • 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










        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/






        share|improve this answer



















        • 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















        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/






        share|improve this answer



















        • 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













        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/






        share|improve this answer














        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/







        share|improve this answer














        share|improve this answer



        share|improve this answer








        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














        • 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










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





        share|improve this answer

























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





          share|improve this answer























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





            share|improve this answer












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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 9:13









            Rohith K N

            46648




            46648






















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








                share|improve this answer

























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








                  share|improve this answer























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








                    share|improve this answer












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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 9:17









                    Nick Parsons

                    2,7082619




                    2,7082619






















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








                        share|improve this answer

























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








                          share|improve this answer























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








                            share|improve this answer












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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 10 at 10:04









                            Nina Scholz

                            170k1383147




                            170k1383147






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                鏡平學校

                                ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                Why https connections are so slow when debugging (stepping over) in Java?