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 :: Recursion In A Class Function 
Javascript :: regex specific number of characters 
Javascript :: useDapp connect metamask example 
Javascript :: How to Solve the Parking Lot Challenge in JavaScript 
Javascript :: var oddOrEven = function(num) {}; 
Javascript :: prisma usersWithZeroPosts 
Javascript :: JavaScript HTMLCollection Object 
Javascript :: todo app html css javascript 
Javascript :: Control a progress bar for custom video player 
Javascript :: get images from mysql with php jquery ajax and display them in html page inside DIVs 
Javascript :: How to Loop Through an Array with a forEach Loop in JavaScript 
Javascript :: how to create existing nodes in godot 
Javascript :: angular service await for data 
Javascript :: Simple Backbone Example 
Javascript :: Top Tips for Vue 3 Development 
Javascript :: Solution-4-C--solution options for reverse bits algorithm js 
Javascript :: javascript odd or even 
Javascript :: react tutorial app 
Javascript :: javascript filter array of object by id 
Javascript :: js new function 
Javascript :: 2d arrays js 
Javascript :: singleton class in js 
Javascript :: electron Uncaught ReferenceError: require is not defined 
Javascript :: how to convert string to reverse title case in javascript 
Javascript :: remove equal objects in list of json js 
Javascript :: how to make navbar links scroll to component react 
Javascript :: javascript map size 
Javascript :: javascript statements 
Javascript :: jQuery - Dimensions 
Javascript :: Could not resolve "i18n-iso-countries" 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =