How connect multiple numbers in a array of objects - javascript

I've been trying to find the solution for a few days but no success. It is only exemple I need of a function that execute this with any selectedNode. I will use it in a graph.
The array:
const edges = [
{
"id": "e1-2",
"from": "1",
"to": "2"
},
{
"id": "e2-3",
"from": "2",
"to": "3"
},
{
"id": "e2-8",
"from": "2",
"to": "8"
},
{
"id": "e2-13",
"from": "2",
"to": "13"
},
{
"id": "e3-4",
"from": "3",
"to": "4"
},
{
"id": "e4-5",
"from": "4",
"to": "5"
},
{
"id": "e4-6",
"from": "4",
"to": "6"
},
{
"id": "e4-7",
"from": "4",
"to": "7"
},
{
"id": "e8-9",
"from": "8",
"to": "9"
},
{
"id": "e9-10",
"from": "9",
"to": "10"
},
{
"id": "e9-11",
"from": "9",
"to": "11"
},
{
"id": "e9-12",
"from": "9",
"to": "12"
},
{
"id": "e13-14",
"from": "13",
"to": "14"
},
{
"id": "e14-15",
"from": "14",
"to": "15"
},
{
"id": "e14-16",
"from": "14",
"to": "16"
},
{
"id": "e14-17",
"from": "14",
"to": "17"
},
{
"id": "e14-18",
"from": "14",
"to": "18"
},
{
"id": "e14-19",
"from": "14",
"to": "19"
}
]
I trying use filter and map but only catch the first children. I need the children of children and so on.
const selectedNode = "2";
const connections = edges
.filter((edge) => edge.from === selectedNode).map((edge) => edge.to)
console.log(connections)
The output:
[LOG]: ["3", "8", "13"]
but i need all children as cascade.
Like this output:
["3", "8", "13", "4", "5", "6", "7", "9", "10", "11", "12", "14", "15", "16", "17", "18", "19"]
But if the selectedNode was "8", the array would be:
["9", "10", "11", "12"]

You need to make a recursive algorithm of some kind. One approach would be to restructure the edges into a mapping by froms first. Then, when iterating over an edge (starting with the selectedNode), if the edge's to contains any other tos, perform the recursive call for each one.
const edges=[{id:"e1-2",from:"1",to:"2"},{id:"e2-3",from:"2",to:"3"},{id:"e2-8",from:"2",to:"8"},{id:"e2-13",from:"2",to:"13"},{id:"e3-4",from:"3",to:"4"},{id:"e4-5",from:"4",to:"5"},{id:"e4-6",from:"4",to:"6"},{id:"e4-7",from:"4",to:"7"},{id:"e8-9",from:"8",to:"9"},{id:"e9-10",from:"9",to:"10"},{id:"e9-11",from:"9",to:"11"},{id:"e9-12",from:"9",to:"12"},{id:"e13-14",from:"13",to:"14"},{id:"e14-15",from:"14",to:"15"},{id:"e14-16",from:"14",to:"16"},{id:"e14-17",from:"14",to:"17"},{id:"e14-18",from:"14",to:"18"},{id:"e14-19",from:"14",to:"19"}];
const toByFrom = {};
for (const edge of edges) {
toByFrom[edge.from] ??= [];
toByFrom[edge.from].push(edge.to);
}
const allNodes = [];
const getNodes = (parent, allNodes = []) => {
const tos = toByFrom[parent];
if (tos) {
allNodes.push(...tos);
for (const to of tos) {
getNodes(to, allNodes);
}
}
return allNodes;
};
console.log(getNodes('2'));

This sounds like a recursion mission. This is the same answer as the other one. Only as a function.
const edges = [{ "id": "e1-2", "from": "1", "to": "2" }, { "id": "e2-3", "from": "2", "to": "3" }, { "id": "e2-8", "from": "2", "to": "8" }, { "id": "e2-13", "from": "2", "to": "13" }, { "id": "e3-4", "from": "3", "to": "4" }, { "id": "e4-5", "from": "4", "to": "5" }, { "id": "e4-6", "from": "4", "to": "6" }, { "id": "e4-7", "from": "4", "to": "7" }, { "id": "e8-9", "from": "8", "to": "9" }, { "id": "e9-10", "from": "9", "to": "10" }, { "id": "e9-11", "from": "9", "to": "11" }, { "id": "e9-12", "from": "9", "to": "12" }, { "id": "e13-14", "from": "13", "to": "14" }, { "id": "e14-15", "from": "14", "to": "15" }, { "id": "e14-16", "from": "14", "to": "16" }, { "id": "e14-17", "from": "14", "to": "17" }, { "id": "e14-18", "from": "14", "to": "18" }, { "id": "e14-19", "from": "14", "to": "19" }];
function find_immediate_edges(selected) {
return edges.filter(item => item.from == selected).map(item => item.to);
}
function find_edges(selected) {
var result = []
function loop(selected) {
var immediate = find_immediate_edges(selected);
result = result.concat(immediate)
immediate.forEach(loop)
}
loop(selected);
return result;
}
console.log("" + find_edges(2));
console.log("" + find_edges(8));

Related

How can I accurately return nested values from JSON for comparison and then complete the comparison?

I have asked a similar question before but I've not been able to expand on this and I haven't found the exact answer that would teach me how to do this. My JSON file will have the same structure in terms of the element names inside each nested object but the values will vary. In plain English, I want to start at the bottom of the file and return true if the following conditions occur and stop searching if true: AppStatus is one of these ["Approved", "Auto Approved", "Return"] and the nested keys of "Name": "Payments", "Value": "X" and "Name": "Term", "Value": "Y" have values that don't match. I'm providing a snippet of the JSON so you can see more easily. The AppStatus is 2nd level and the Name, Value are 3rd level. In this example, it should return true based on the 2nd set of data in DataElements.
{
"DecisionHistory": [
{
"Id": "273601",
"Number": "1",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "2",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "3",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "75",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
}
]
}
I've tried using 2 ugly nested for loops without success. I would prefer not to use for loops if I don't have to. See below:
function main(jsonFile) {
const history = JSON.parse(jsonFile).DecisionHistory;
const appStatus = ["Approved", "Auto Approved", "Return"];
const termMonths = "TermMonths";
const numberOfPayments = "Number of Payments";
let termMonthsVal = 0;
let numberofPaymentsVal = 0;
for (let a = history.length - 1; a >= 0; a--) {
if (appStatus.includes(history[a].AppStatus)) {
var dataElementsA = history[a].DataElements;
for (let b = (dataElementsA.length) - 1; b >= 0; b--) {
if (dataElementsA[b].Name == termMonths) {
termMonthsVal = new Number((dataElementsA[b].Value));
break;
}
}
}
}
for (let a = history.length - 1; a >= 0; a--) {
if (appStatus.includes(history[a].AppStatus)) {
var dataElementsB = history[a].DataElements;
for (let b = (dataElementsB.length) - 1; b >= 0; b--) {
if (dataElementsB[b].Name == numberOfPayments) {
numberofPaymentsVal = new Number((dataElementsB[b].Value));
break;
}
}
}
}
return (termMonthsVal != numberofPaymentsVal);
}
let jsonFile = `{
"DecisionHistory": [{
"Id": "273601",
"Number": "1",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "2",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "3",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "75",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
}
]
}`;
console.log(main(jsonFile));
//var result = main("_inJson");
//module.exports = main;
As a side note, the jsonFile in the main function is read in from a resource folder in my VS Code and I'm exporting main to an app.js so I can run assertions against it. So the JSON posted here is actually in a separate file but this is the exact structure of the data.
What am I missing? .filter? .map? .reduce? Differnt breaks?
Here's a "one-liner" depending on how long you want your lines to be ;)
const x = {
"DecisionHistory": [
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "72",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
},
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "72",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
},
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "75",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
}
]
};
const bar = (f) =>
f.DecisionHistory.reverse().some((decision) => ["Approved", "Auto Approved", "Return"].includes(decision.AppStatus) && decision.DataElements.find((el) => el.Name === 'Payments')?.Value !== decision.DataElements.find((el) => el.Name === 'Term')?.Value);
console.log(bar(x));

Sorting API JSON response using Javascript

I am getting below response on hitting DoctorData.api and want to sort them all with their 'ID'. Can someone show me how to sort the Output JSON data and display it same format.
Kindly Excuse my coding skills, this is my second test case. I am new to JS.
var doctorIDgeturl = geturl.geturls.getapiUrl; //'getapiUrl' is Doctor Get API
var res = await api.getRequest(doctorIDgeturl);
logger.logger().info('GET_data = ', JSON.stringify(res.data, null, 2));
var rescount = Object.keys(res.data.data.doctorList); //doctorList is the API response object for above GET API
console.log("This is Sorted Id: ");
const sortedResponse = sort(res.data, r => r.doctorListModels.associateId, ['asc']) //using ascending order to sort
console.log(sortedResponse);
Current output:
{
"message": "Record Found",
"data": {
"DoctorsList": [
{
"id": "10",
"name": "William",
"launch_date": "2018-01-24T00:00:00.000-05:00"
},
{
"id": "2",
"name": "Snow",
"launch_date": "2017-08-14T00:00:00.000-05:00"
},
{
"id": "33",
"name": "Thomas",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "3",
"name": "Ismail",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "5",
"name": "Jackson",
"launch_date": "2018-04-10T00:00:00.000-05:00"
}
Expected output after sorting:
{
"message": "Record Found",
"data": {
"DoctorsList": [
{
"id": "2",
"name": "Snow",
"launch_date": "2017-08-14T00:00:00.000-05:00"
},
{
"id": "3",
"name": "Ismail",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "5",
"name": "Jackson",
"launch_date": "2018-04-10T00:00:00.000-05:00"
},
{
"id": "10",
"name": "William",
"launch_date": "2018-01-24T00:00:00.000-05:00"
},
{
"id": "33",
"name": "Thomas",
"launch_date": "2018-11-29T00:00:00.000-05:00"
}
parseInt(r.doctorListModels.associateId) or +r.doctorListModels.associateId
it seems like it sorts the id as string not number
Sorry I can't comment because of low reputation, but here is solution.
const obj = {
"message": "Record Found",
"data": {
"DoctorsList": [{
"id": "10",
"name": "William",
"launch_date": "2018-01-24T00:00:00.000-05:00"
},
{
"id": "2",
"name": "Snow",
"launch_date": "2017-08-14T00:00:00.000-05:00"
},
{
"id": "33",
"name": "Thomas",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "3",
"name": "Ismail",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "5",
"name": "Jackson",
"launch_date": "2018-04-10T00:00:00.000-05:00"
}]
}
}
const sortedResponse = obj.data.DoctorsList.sort(function(a, b) { return parseInt(a.id) - parseInt(b.id) });
console.log(sortedResponse)
const obj = {
"message": "Record Found",
"data": {
"DoctorsList": [{
"id": "10",
"name": "William",
"launch_date": "2018-01-24T00:00:00.000-05:00"
},
{
"id": "2",
"name": "Snow",
"launch_date": "2017-08-14T00:00:00.000-05:00"
},
{
"id": "33",
"name": "Thomas",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "3",
"name": "Ismail",
"launch_date": "2018-11-29T00:00:00.000-05:00"
},
{
"id": "5",
"name": "Jackson",
"launch_date": "2018-04-10T00:00:00.000-05:00"
}
]
}}
obj.data.DoctorsList = obj.data.DoctorsList.sort((a, b) => parseInt(a.id) > parseInt(b.id));
console.log(obj)

Remove an associate object while looping through using map

I have a json array and i need to delete the subarray whose id value is 5, which is falling under the serialNo 1. I tried the following method, but its not deleting any entry in the subarray.
let Details = [
{ "serialNo": "1", "text": "AAA", "subArray": [{ "id": "1", "name": "geo" }, { "id": "5", "name": "gau" }, { "id": "4", "name": "joi" }] },
{ "serialNo": "2", "text": "BBB", "subArray": [{ "id": "7", "name": "rom" }, { "id": "5", "name": "dom" }, { "id": "4", "name": "noi" }] },
{ "serialNo": "3", "text": "CCC", "subArray": [{ "id": "1", "name": "glo" }, { "id": "5", "name": "gum" }, { "id": "4", "name": "lom" }] }
];
Details.map((data) => {
if (data.serialNo === "1") {
data.subArray.map((subDetails) => {
if (subDetails.id === "5") {
delete data.subArray[subDetails];
}
})
}
})
I don't know why you explicitely wants to use the map function. But the following works:
let Details = [
{ "serialNo": "1", "text": "AAA", "subArray": [{ "id": "1", "name": "geo" }, { "id": "5", "name": "gau" }, { "id": "4", "name": "joi" }] },
{ "serialNo": "2", "text": "BBB", "subArray": [{ "id": "7", "name": "rom" }, { "id": "5", "name": "dom" }, { "id": "4", "name": "noi" }] },
{ "serialNo": "3", "text": "CCC", "subArray": [{ "id": "1", "name": "glo" }, { "id": "5", "name": "gum" }, { "id": "4", "name": "lom" }] }
];
Details = Details.map(function (data) {
if (data.serialNo === "1") {
data.subArray = data.subArray.filter(function (sa) {
return (sa.id !== "5");
});
}
return data;
});
console.log(Details);
The first problem is that you're not returning anything from the map functions. The second problem is that data.subArray[subDetails] is undefined, subDetails is an object not an index in the data.subArray array. You can use a combination of map and filter to accomplished this instead of using delete.
let Details = [
{ "serialNo": "1", "text": "AAA", "subArray": [{ "id": "1", "name": "geo" }, { "id": "5", "name": "gau" }, { "id": "4", "name": "joi" }] },
{ "serialNo": "2", "text": "BBB", "subArray": [{ "id": "7", "name": "rom" }, { "id": "5", "name": "dom" }, { "id": "4", "name": "noi" }] },
{ "serialNo": "3", "text": "CCC", "subArray": [{ "id": "1", "name": "glo" }, { "id": "5", "name": "gum" }, { "id": "4", "name": "lom" }] }
];
Details.map((data) => {
if (data.serialNo === "1") {
data.subArray = data.subArray.filter((subDetails) => {
return subDetails.id !== "5";
})
}
return data;
});
console.log(Details);
If you want to stick with map what you need to do is to return undefined when subDetails.id is 5.
let Details = [
{ "serialNo": "1", "text": "AAA", "subArray": [{ "id": "1", "name": "geo" }, { "id": "5", "name": "gau" }, { "id": "4", "name": "joi" }] },
{ "serialNo": "2", "text": "BBB", "subArray": [{ "id": "7", "name": "rom" }, { "id": "5", "name": "dom" }, { "id": "4", "name": "noi" }] },
{ "serialNo": "3", "text": "CCC", "subArray": [{ "id": "1", "name": "glo" }, { "id": "5", "name": "gum" }, { "id": "4", "name": "lom" }] }
];
Details.map((data) => {
if (data.serialNo === "1") {
data.subArray = data.subArray.filter((subDetails) => {
return subDetails.id === "5" ? undefined : subDetails;
})
}
return data;
});
console.log(Details);
One map plus object constructor:
const arr = [
{ "serialNo": "1", "text": "AAA", "subArray": [{ "id": "1", "name": "geo" }, { "id": "5", "name": "gau" }, { "id": "4", "name": "joi" }] },
{ "serialNo": "2", "text": "BBB", "subArray": [{ "id": "7", "name": "rom" }, { "id": "5", "name": "dom" }, { "id": "4", "name": "noi" }] },
{ "serialNo": "3", "text": "CCC", "subArray": [{ "id": "1", "name": "glo" }, { "id": "5", "name": "gum" }, { "id": "4", "name": "lom" }] }
];
const s = 1, id = 5; // conditions
const r = arr.map(e => (e.serialNo == s)
? Object.assign(e, {'subArray': e.subArray.filter(a => a.id != id)})
: e);
console.log(JSON.stringify(r, null, 2));
Object.assign swaps old subArray with the new filtered one.

Error when I try to parse my json

I am using AngularJs doing a simple application And I want to set a variable up in a javascript file in orther to call a json file.
This is my data.js :
[{
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "15",
"qte": "10"
}, {
"id": "c02",
"name": "name2",
"price": "18",
"qte": "11"
}, {
"id": "c03",
"name": "name3",
"price": "11",
"qte": "14"
}],
"subsities": [{
"name": "sub A1",
"elements": [{
"id": "sub01",
"name": "nameSub1",
"price": "1",
"qte": "14"
}, {
"id": "sub02",
"name": "nameSub2",
"price": "8",
"qte": "13"
}, {
"id": "sub03",
"name": "nameSub3",
"price": "1",
"qte": "14"
}]
}, {
"name": "sub A2",
"elements": [{
"id": "ssub01",
"name": "nameSsub1",
"price": "1",
"qte": "7"
}, {
"id": "ssub02",
"name": "nameSsub2",
"price": "8",
"qte": "1"
}, {
"id": "ssub03",
"name": "nameSsub3",
"price": "4",
"qte": "19"
}]
}, {
"name": "sub A3",
"elements": [{
"id": "sssub01",
"name": "nameSssub1",
"price": "1",
"qte": "11"
}, {
"id": "sssub02",
"name": "nameSssub2",
"price": "2",
"qte": "15"
}, {
"id": "sssub03",
"name": "nameSssub3",
"price": "1",
"qte": "15"
}]
}]
}, {
"name": "city B",
"elements": [{
"id": "cc01",
"name": "name11",
"price": "10",
"qte": "11"
}, {
"id": "cc02",
"name": "name22",
"price": "14",
"qte": "19"
}, {
"id": "cc03",
"name": "name33",
"price": "11",
"qte": "18"
}]
}, {
"name": "city C",
"elements": [{
"id": "ccc01",
"name": "name111",
"price": "19",
"qte": "12"
}, {
"id": "ccc02",
"name": "name222",
"price": "18",
"qte": "17"
}, {
"id": "ccc03",
"name": "name333",
"price": "10",
"qte": "5"
}]
}]
And this is my data.json
[{
"date":"01/05/2016"
}]
I call my data here.
angular.module("myApp",['zingchart-angularjs'])
.controller('MainController', ['$scope', '$http', function($scope, $http) {
$scope.chartBase = {
"type": "line",
"plotarea": {
"adjust-layout": true /* For automatic margin adjustment. */
},
"scale-x": {
"label": {
"text": "Above is an example of a category scale" /* Add a scale title with a label object. */
},
"labels": ["name1", "name2", "name3"] /* Add your scale labels with a labels array. */
},
"series": [{
"values": [15, 18, 11] //here the prices of city selected
},{
"values": [10, 11, 14] //here the qte of city selected
}]
};
$scope.chartData = angular.copy($scope.chartBase);
$http.get('data.js')
.then(function(response) {
$scope.cities = response.data; // save the request data
$scope.selectedCity = $scope.cities[0]; // select the first one
$scope.changeCity(); // update chart
}, function(error) { console.log(error); });
$scope.changeCity = function() {
if($scope.selectedSubCity || $scope.selectedCity){ // if something has been selected
$scope.data = ($scope.selectedSubCity || $scope.selectedCity).elements; // update elements field
// initialize the array to be displayed in chart
var labels = [];
var price = {
"values": []
};
var qte = {
"values": []
};
// fill the arrays to be displayed from the selected city (sub city)
angular.forEach($scope.data, function(item, index) {
labels.push(item.name);
price.values.push(parseInt(item.price));
qte.values.push(parseInt(item.qte));
});
console.log($scope.chartData)
// put selected values to the field that is used to render the chart
$scope.chartData["scale-x"].labels = labels;
$scope.chartData.series = [ price, qte ];
}
}
}]);
When I run this, the browser tell that I have this error :
SyntaxError: Unexpected token v in JSON at position 5
at Object.parse (native)
You can't have those in your data.js file as they are not valid JSON
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
The error is pointing to the first "v" of "var". You can test your JSON here http://json.parser.online.fr/ on some other online validator.
Your file should look like this:
[{
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "15",
"qte": "10"
}, {
"id": "c02",
"name": "name2",
"price": "18",
"qte": "11"
}, {
"id": "c03",
"name": "name3",
"price": "11",
"qte": "14"
}],
"subsities": [{
"name": "sub A1",
"elements": [{
"id": "sub01",
"name": "nameSub1",
"price": "1",
"qte": "14"
}, {
"id": "sub02",
"name": "nameSub2",
"price": "8",
"qte": "13"
}, {
"id": "sub03",
"name": "nameSub3",
"price": "1",
"qte": "14"
}]
}, {
"name": "sub A2",
"elements": [{
"id": "ssub01",
"name": "nameSsub1",
"price": "1",
"qte": "7"
}, {
"id": "ssub02",
"name": "nameSsub2",
"price": "8",
"qte": "1"
}, {
"id": "ssub03",
"name": "nameSsub3",
"price": "4",
"qte": "19"
}]
}, {
"name": "sub A3",
"elements": [{
"id": "sssub01",
"name": "nameSssub1",
"price": "1",
"qte": "11"
}, {
"id": "sssub02",
"name": "nameSssub2",
"price": "2",
"qte": "15"
}, {
"id": "sssub03",
"name": "nameSssub3",
"price": "1",
"qte": "15"
}]
}]
}, {
"name": "city B",
"elements": [{
"id": "cc01",
"name": "name11",
"price": "10",
"qte": "11"
}, {
"id": "cc02",
"name": "name22",
"price": "14",
"qte": "19"
}, {
"id": "cc03",
"name": "name33",
"price": "11",
"qte": "18"
}]
}, {
"name": "city C",
"elements": [{
"id": "ccc01",
"name": "name111",
"price": "19",
"qte": "12"
}, {
"id": "ccc02",
"name": "name222",
"price": "18",
"qte": "17"
}, {
"id": "ccc03",
"name": "name333",
"price": "10",
"qte": "5"
}]
}]
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
Why do you add those at the beginning of the JSON ? They cannot be parsed by your script, thus displaying the Syntax Error. What do you want to do exactly ?

Import JSON and convert to array

I tried alot of things, but nothing worked. I'm using jQuery.
So what I have to do is really to import http://jacce.dyndns.org/game/resources/game/map.php (so far I succeeded with $.getJSON), convert it to an array and then place it in a global variable.
I don't really know how to do any of the last two things (well, I know how to create global variables, but not inside jQuery functions). So, any help?
EDIT: Here's the JSON:
{
"-5": {
"-5": {
"id": "1",
"colour": ""
},
"-4": {
"id": "2",
"colour": ""
},
"-3": {
"id": "3",
"colour": ""
},
"-2": {
"id": "4",
"colour": ""
},
"-1": {
"id": "5",
"colour": ""
},
"1": {
"id": "6",
"colour": ""
},
"2": {
"id": "7",
"colour": ""
},
"3": {
"id": "8",
"colour": ""
},
"4": {
"id": "9",
"colour": ""
},
"5": {
"id": "10",
"colour": ""
}
},
"-4": {
"-5": {
"id": "11",
"colour": " fill=\"#A90000\""
},
"-4": {
"id": "12",
"colour": ""
},
"-3": {
"id": "13",
"colour": ""
},
"-2": {
"id": "14",
"colour": ""
},
"-1": {
"id": "15",
"colour": ""
},
"1": {
"id": "16",
"colour": ""
},
"2": {
"id": "17",
"colour": ""
},
"3": {
"id": "18",
"colour": ""
},
"4": {
"id": "19",
"colour": " fill=\"#A90000\""
},
"5": {
"id": "20",
"colour": ""
}
},
"-3": {
"-5": {
"id": "21",
"colour": ""
},
"-4": {
"id": "22",
"colour": ""
},
"-3": {
"id": "23",
"colour": ""
},
"-2": {
"id": "24",
"colour": ""
},
"-1": {
"id": "25",
"colour": ""
},
"1": {
"id": "26",
"colour": ""
},
"2": {
"id": "27",
"colour": ""
},
"3": {
"id": "28",
"colour": ""
},
"4": {
"id": "29",
"colour": " fill=\"#A90000\""
},
"5": {
"id": "30",
"colour": ""
}
},
"-2": {
"-5": {
"id": "31",
"colour": ""
},
"-4": {
"id": "32",
"colour": ""
},
"-3": {
"id": "33",
"colour": ""
},
"-2": {
"id": "34",
"colour": ""
},
"-1": {
"id": "35",
"colour": ""
},
"1": {
"id": "36",
"colour": ""
},
"2": {
"id": "37",
"colour": ""
},
"3": {
"id": "38",
"colour": ""
},
"4": {
"id": "39",
"colour": ""
},
"5": {
"id": "40",
"colour": ""
}
},
"-1": {
"-5": {
"id": "41",
"colour": ""
},
"-4": {
"id": "42",
"colour": ""
},
"-3": {
"id": "43",
"colour": " fill=\"#A90000\""
},
"-2": {
"id": "44",
"colour": ""
},
"-1": {
"id": "45",
"colour": ""
},
"1": {
"id": "46",
"colour": ""
},
"2": {
"id": "47",
"colour": ""
},
"3": {
"id": "48",
"colour": ""
},
"4": {
"id": "49",
"colour": ""
},
"5": {
"id": "50",
"colour": ""
}
},
"1": {
"-5": {
"id": "51",
"colour": " fill=\"#A90000\""
},
"-4": {
"id": "52",
"colour": ""
},
"-3": {
"id": "53",
"colour": ""
},
"-2": {
"id": "54",
"colour": " fill=\"#A90000\""
},
"-1": {
"id": "55",
"colour": ""
},
"1": {
"id": "56",
"colour": ""
},
"2": {
"id": "57",
"colour": ""
},
"3": {
"id": "58",
"colour": ""
},
"4": {
"id": "59",
"colour": ""
},
"5": {
"id": "60",
"colour": ""
}
},
"2": {
"-5": {
"id": "61",
"colour": ""
},
"-4": {
"id": "62",
"colour": ""
},
"-3": {
"id": "63",
"colour": ""
},
"-2": {
"id": "64",
"colour": ""
},
"-1": {
"id": "65",
"colour": ""
},
"1": {
"id": "66",
"colour": ""
},
"2": {
"id": "67",
"colour": ""
},
"3": {
"id": "68",
"colour": ""
},
"4": {
"id": "69",
"colour": ""
},
"5": {
"id": "70",
"colour": ""
}
},
"3": {
"-5": {
"id": "71",
"colour": ""
},
"-4": {
"id": "72",
"colour": " fill=\"#000D81\""
},
"-3": {
"id": "73",
"colour": ""
},
"-2": {
"id": "74",
"colour": ""
},
"-1": {
"id": "75",
"colour": ""
},
"1": {
"id": "76",
"colour": ""
},
"2": {
"id": "77",
"colour": ""
},
"3": {
"id": "78",
"colour": ""
},
"4": {
"id": "79",
"colour": ""
},
"5": {
"id": "80",
"colour": " fill=\"#A90000\""
}
},
"4": {
"-5": {
"id": "81",
"colour": ""
},
"-4": {
"id": "82",
"colour": ""
},
"-3": {
"id": "83",
"colour": ""
},
"-2": {
"id": "84",
"colour": ""
},
"-1": {
"id": "85",
"colour": ""
},
"1": {
"id": "86",
"colour": " fill=\"#A90000\""
},
"2": {
"id": "87",
"colour": ""
},
"3": {
"id": "88",
"colour": ""
},
"4": {
"id": "89",
"colour": ""
},
"5": {
"id": "90",
"colour": ""
}
},
"5": {
"-5": {
"id": "91",
"colour": ""
},
"-4": {
"id": "92",
"colour": ""
},
"-3": {
"id": "93",
"colour": " fill=\"#A90000\""
},
"-2": {
"id": "94",
"colour": ""
},
"-1": {
"id": "95",
"colour": ""
},
"1": {
"id": "96",
"colour": ""
},
"2": {
"id": "97",
"colour": ""
},
"3": {
"id": "98",
"colour": ""
},
"4": {
"id": "99",
"colour": ""
},
"5": {
"id": "100",
"colour": ""
}
}
}
Ima place this answer cos I think the OP has confused arrays and objects.
This will work:
var t;
$.getJSON('/url', {param: someparam}, function(data){
t = data;
});
The data var represents the stringifyed JSON output and you basically just pass the output to a globally defined var...done.
It will get your JSON out all into one Object. You cannot have an array since minus numbers are not counted as int arrray keys in JS.
Bu this will allow you to do:
$.each(t, function(){
console.log($(this));
});
And get your "row" out.
Or like:
t['-3'];
To understand this difference between arrays and Objects read here: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
And another link: What is the difference between an array and an object?

Categories

Resources