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 :: how to get 6 months after date object 
Javascript :: function if else javascript 
Javascript :: box shadow generador react native 
Javascript :: dayofweek mongodb 
Javascript :: rewrite expressjs url 
Javascript :: js example 
Javascript :: how to get the children of an element in cypress 
Javascript :: how to add eventlister to multiple variable 
Javascript :: put image in canvas with cover mode 
Javascript :: node js install aws-sdk 
Javascript :: getting cannot call a class as a function 
Javascript :: gradle error react native 
Javascript :: javascript get last 2 item in array 
Javascript :: regex check for anchor tag with specific text 
Javascript :: [Object: null prototype] appolo 
Javascript :: display toastr info 
Javascript :: json traversal in js 
Javascript :: bundle 
Javascript :: vscode regex replace 
Javascript :: find second largest number in array javascript 
Javascript :: qr code generator with js 
Javascript :: get all database react native 
Javascript :: jquery document ready deprecated 
Javascript :: is loop backward 
Javascript :: How to get random no. without math.random() function 
Javascript :: js get location params 
Javascript :: inline style to change background color 
Javascript :: math.floor + roandom 
Javascript :: js push 
Javascript :: js-cookie 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =