Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert json to 2d array

function jsonArrayTo2D(arrayOfObjects){
  let header = [],
      AoA = [];
  arrayOfObjects.forEach(obj => {
    Object.keys(obj).forEach(key => header.includes(key) || header.push(key))
    let thisRow = new Array(header.length);
    header.forEach((col, i) => thisRow[i] = obj[col] || '')
    AoA.push(thisRow);
  })
  AoA.unshift(header);
  return AoA;
}
Comment

Turn A 2D Array Into A JSON

	var arr = [
    ["Status", "Name", "Marks", "Position"], 
    ["active", "Akash", 10.0, "Web Developer"],
    ["active", "Vikash", 10.0, "Front-end-dev"],
    ["deactive", "Manish", 10.0, "designer"],
    ["active", "Kapil", 10.0, "JavaScript developer"],
    ["active", "Manoj", 10.0, "Angular developer"],
];
//javascript create JSON object from two dimensional Array
function arrayToJSONObject (arr){
    //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;
}
let thing = arrayToJSONObject(arr);
console.log(thing);
console.log(thing[0]['Name']);
/*Akash*/
Comment

Create A JSON From 2D Array Example

	const url = new URL('https://example.com/?firstkey=firstvalue&secondkey=secondvalue');

console.log(url.href);
// https://example.com/?a=hello&b=world

console.log(url.origin);

var obj = {};
for(const [key, value] of Array.from(url.searchParams.entries()))
{
	obj[key] = value;

}
console.log(obj);
 /*Object { firstkey: "firstvalue", secondkey: "secondvalue" }*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: Using an object of functions as a parameter into a function 
Javascript :: 1--Reverse Bits Algo 
Javascript :: Hardhat config file multiple network 
Javascript :: check if can go back react native 
Javascript :: Update A Value In ExpressJS/MongoDB 
Javascript :: prisma is and isNot 
Javascript :: function titleCase 2 
Javascript :: Get year from user entered date in javascript 
Javascript :: NavBar with divs 
Javascript :: express dynamic api template 
Javascript :: How to Loop Through an Array with a for…of Loop in JavaScript 
Javascript :: connect nextjs to google sheets 
Javascript :: how to get on hnage input before clicking off 
Javascript :: send form data to endpoint js 
Javascript :: React Gotchas 
Javascript :: Solution-4-B--solution options for reverse bits algorithm js 
Javascript :: update excel file in react js using sheetjs 
Javascript :: anonymous function js 
Javascript :: popper js 
Javascript :: hello world js 
Javascript :: dictionnary js 
Javascript :: regex javscript 
Javascript :: js array append 
Javascript :: how to convert roman to decimal in javascript 
Javascript :: How to print even and odd position characters of an array of strings in JavaScript 
Javascript :: ring add an attribute to the object 
Javascript :: zigale assefa 
Javascript :: what is package.josn file 
Javascript :: jQuery - Set 
Javascript :: fingerprint2 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =