Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarfarazansari/7e8ae05168b80b36016eb1c561a82f73 to your computer and use it in GitHub Desktop.
Save sarfarazansari/7e8ae05168b80b36016eb1c561a82f73 to your computer and use it in GitHub Desktop.
//assuming array look like this.
var arr = [
["Status", "Name", "Marks", "Position"],
["active", Sarfaraz, 10.0, "Front-end-dev"],
["active", John, 10.0, "Front-end-dev"],
["deactive", Raganar, 10.0, "UI designer"],
];
console.log (arrToObject(arr));
//create JSON object from 2 dimensional Array
function arrToObject (arr){
//assuming header
var keys = arr[0];
//vacate keys from main array
var newArr = arr.slice(1, arr.length);
var formatted = [],
data = newArr,
cols = keys,
l = cols.length;
for (var i=0; i<data.length; i++) {
var d = data[i],
o = {};
for (var j=0; j<l; j++)
o[cols[j]] = d[j];
formatted.push(o);
}
return formatted;
}
@yhdleung
Copy link

// assuming array look like this.
let arr = [
	["Status", "Name", "Marks", "Position"], 
	["active", "Sarfaraz", 10.0, "Front-end-dev"],
	["active", "John", 10.0, "Front-end-dev"],
	["deactive", "Raganar", 10.0, "UI designer"],
];

// vacate headers/keys
let keys = arr.shift()
// create JSON objects from Array
let result = arr.map(arr =>
    Object.assign({}, ...arr.map((x, i) => ({ [keys[i]]: x })))
);

console.log(result);

@km190164
Copy link

km190164 commented Apr 20, 2023

// assuming array look like this.
let arr = [
	["Status", "Name", "Marks", "Position"], 
	["active", "Sarfaraz", 10.0, "Front-end-dev"],
	["active", "John", 10.0, "Front-end-dev"],
	["deactive", "Raganar", 10.0, "UI designer"],
];

// vacate headers/keys
let keys = arr.shift()
// create JSON objects from Array
let result = arr.map(arr =>
    Object.assign({}, ...arr.map((x, i) => ({ [keys[i]]: x })))
);

console.log(result);

This is quite concise and quick. I have basic skill of arrays, and map, reduce, assign etc are new for me although I am in IT for over 20 years but in non-technical role for 12 years. Your solution did confuse me when I the results were not in order but after a few hours when I used JSON.stringfy on the results, I realized your solution was given up to a stage where developer can decide what to do from there. Thank you.

@yhdleung
Copy link

That was quite minified, which could be confusing.
We can re-write it to be more readable. And put it into a function similar to the OP.

function convertTableToJSON(table) {
    // extract header as keys
    let keys = table.shift();
    // create JSON objects from the remaining data 
    let json = table.map(row => {
        let obj = {};
        // add each element of the row to the object with the corresponding key
        row.forEach((value, index) => {
            obj[keys[index]] = value;
        });
        return obj;
    });
    return json;
}

And the concise version would be:

function convertTableToJSON(table) {
    let keys = table.shift();
    let json = table.map(row => Object.assign({}, ...row.map((v, i) => ({ [keys[i]]: v }))));
    return json
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment