-1

I have below array

[['a','b','c'],['1','2','3'],['5','4','3'],['1','2','3']]

How do I covert to get this in JSON format in Javascript/Node.

{
"a": ['1','5','1'],
"b": ['2','4','2'],
"c" :['3','3','3']
}

I tired with iterating with for loop but that is for fixed no of values what if values are keep on varying for e.g. 

 [['a','b','c','d'],['1','2','3','6'],['5','4','3','7'],['1','2','3',2']]

or

[['a','b'],['1','2'],['5','4'],['1','2']]

Not able to get any clue ? Any help or clue ?

getashu1
  • 77
  • 9
  • Possible duplicate of [Convert array to JSON](https://stackoverflow.com/questions/2295496/convert-array-to-json) – sheplu Aug 13 '18 at 18:14

1 Answers1

1

You can do something like this:

function convertArrayToJSON(arr) {
    var result = {};
    for (var j = 0; j < arr[0].length; j++) {
        result[arr[0][j]] = [];
        for (var i = 1; i < arr.length; i++) {
            result[arr[0][j]].push(arr[i][j]);
        }
    }
    console.log(result);
    return result;
}

convertArrayToJSON([['a','b','c'],['1','2','3'],['5','4','3'],['1','2','3']]);
Sookie Singh
  • 1,543
  • 11
  • 17