How to change content return with file json

please help me, i have tried by methode lambda dan closure, source with note //how to change content return, with file.json ,the retur like pivottable, i make with library orb react

window.demo = {};
window.demo.data = [];

var data = getData();
for (var t = 0; t < 1; t++) {
  for (var j = 0; j < data.length; j++) {
    window.demo.data.push(data[j]);
  }
}

function getData() {
  // how to change content return[]
  return [
    [
      "Contoso Florida",
      "Proseware LCD17W E202 Black",
      "Proseware, Inc.",
      "Economy",
      "Monitors",
      4,
      509.55
    ],
    [
      "Contoso New Jersey",
      "Adventure Works CRT15 E101 Black",
      "Adventure Works",
      "Economy",
      "Monitors",
      4,
      351
    ]
  ];
}
// how to change content return[]

function getData1() {
  return [new model(1012, "REPUBLICAN", 2408, "71 +", "08/2006", 51, "PERM")];
}

Here’s how to load a JSON file locally

function loadJSON(callback, filename) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open("GET", filename, true); // Replace 'my_data' with the path to your file
    xobj.onreadystatechange = function () {
        if (xobj.readyState === 4 && xobj.status === 200) {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}

Here’s an example of how to use loadJSON:

HTML

<pre id="response"></pre>

JavaScript

function init(callback) {
    function parseJSON(response) {
        // Parse JSON string into object
        var actual_JSON = JSON.parse(response);
        callback(actual_JSON);
    }
    loadJSON(parseJSON, "my_data.json");
}

function useJSON(data) {
    var response = document.getElementById("response");
    response.innerHTML = JSON.stringify(data, undefined, 4);
}
init(useJSON);

I recommend that you rename the useJSON() function to a more meaningful name that accurately conveys what your code does.

A note for if you run the script directly on your computer. Any XMLHttpRequest code isn’t likely to work, due to security restrictions that prevents bad people from accessing files on your computer.

You can set up a local server on you computer from which to run and test the code. The following article on How do you set up a local testing server? provides a good tutorial on how to do so.

source java script like this

(function() {

	var model = function(voter, party, precinct, ageGroup, lastVoted, yearsReg, ballotStatus) {
		this.voter = voter; 
		this.party = party;
		this.precinct = precinct;
		this.ageGroup = ageGroup;
		this.lastVoted = lastVoted;
		this.yearsReg = yearsReg;
		this.ballotStatus = ballotStatus;
	};

	window.demo = {};
	window.demo.data = [];

	var data = getData();
	for(var t=0;t<1;t++) {
		for(var j = 0;j < data.length; j++) {
			window.demo.data.push(data[j]);
		}
	}

//add bellow source	
function loadJSON(callback, filename) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open("GET", filename, true); // Replace 'my_data' with the path to your file
    xobj.onreadystatechange = function () {
        if (xobj.readyState === 4 && xobj.status === 200) {
        // Required use of an anonymous callback as .open 
        // will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}
	
function init(callback) {
    function parseJSON(response) {
        // Parse JSON string into object
        var actual_JSON = JSON.parse(response);
        callback(actual_JSON);
    }
    loadJSON(parseJSON, "demo.json");
}

function useJSON(data) {
    var response = document.getElementById("response");
    response.innerHTML = JSON.stringify(data, undefined, 4);
}
init(useJSON);
//........

	
}());

i’m created ur solution, but can not run ? trims

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.