This question already has answers here:
Finding the max value of an attribute in an array of objects
(21 answers)
Closed 1 year ago.
I try to find the highest key value of 'sequence' where it's value is 'true'.
I know it's not sql, but I would like know if it's possible to do this request on javascript.
For example, in my case I would like to have : 5 because "70" it's the highest value with bug_tab is true.
Here my js array myTab :
[
{
"value": "AHAH",
"field": "15",
"color": "",
"bug_tab": true,
"sequence": "40",
"text": "slash"
},
{
"value": "BABA",
"field": "8",
"color": "",
"bug_tab": true,
"sequence": "50",
"text": "zip"
},
{
"value": "CACA",
"field": "25",
"color": "",
"bug_tab": false,
"sequence": "63",
"text": "vite"
},
{
"value": "DADA",
"field": "22",
"color": "",
"bug_tab": true,
"sequence": "66",
"text": "meat"
},
{
"value": "EVA",
"field": "13",
"color": "",
"bug_tab": true,
"sequence": "70",
"text": "zut"
},
{
"value": "FAFA",
"field": "jut",
"color": "",
"bug_tab": false,
"sequence": "90",
"text": "cut"
}
]
What I have do :
This return the first occurence where bug_tab is equal to true :
var indexbugTabArray = myTab.map(function(o) { return o.bug_tab; }).indexOf(true);
Advance thanks,
Use array.reduce to get the highest index.
Also, as noted by another user, the correct answer based on your description is 4, not 5 like you said.
var arr = [{
"value": "AHAH",
"field": "15",
"color": "",
"bug_tab": true,
"sequence": "40",
"text": "slash"
},
{
"value": "BABA",
"field": "8",
"color": "",
"bug_tab": true,
"sequence": "50",
"text": "zip"
},
{
"value": "CACA",
"field": "25",
"color": "",
"bug_tab": false,
"sequence": "63",
"text": "vite"
},
{
"value": "DADA",
"field": "22",
"color": "",
"bug_tab": true,
"sequence": "66",
"text": "meat"
},
{
"value": "EVA",
"field": "13",
"color": "",
"bug_tab": true,
"sequence": "70",
"text": "zut"
},
{
"value": "FAFA",
"field": "jut",
"color": "",
"bug_tab": false,
"sequence": "90",
"text": "cut"
}
];
var res = arr.reduce((acc, curr, idx, arr) =>
curr.bug_tab && +curr.sequence > +arr[acc].sequence ? idx : acc
, 0);
console.log(res);
It can be done like that, maybe it's not the most efficient way but it works as expected
const toto = [
{
"value": "AHAH",
"field": "15",
"color": "",
"bug_tab": true,
"sequence": "40",
"text": "slash"
},
{
"value": "BABA",
"field": "8",
"color": "",
"bug_tab": true,
"sequence": "50",
"text": "zip"
},
{
"value": "CACA",
"field": "25",
"color": "",
"bug_tab": false,
"sequence": "63",
"text": "vite"
},
{
"value": "DADA",
"field": "22",
"color": "",
"bug_tab": true,
"sequence": "66",
"text": "meat"
},
{
"value": "EVA",
"field": "13",
"color": "",
"bug_tab": true,
"sequence": "70",
"text": "zut"
},
{
"value": "FAFA",
"field": "jut",
"color": "",
"bug_tab": false,
"sequence": "90",
"text": "cut"
}
];
const max = {
index: -1, // -1 so you can check if you find one
value: 0,
};
toto.forEach((el, index) => {
if (+el.sequence > max.value && el.bug_tab) {
max.index = index;
max.value = +el.sequence;
}
});
console.log(max.index, max.value, toto[max.index]);
Related
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));
I build FusionCharts multilevelpie graph. But huge indents appear.
500 x 500
With a decrease in space, the graph is greatly reduced.
padding 1
100% x 100%
When doing a full size chart, the indents become too large.
padding 2
When I reduce the space of the graph, the graph becomes too small.
My code:
FusionCharts.ready(function(){
var fusioncharts = new FusionCharts({
type: 'multilevelpie',
renderAt: 'chart-container',
width: '500',
height: '500',
dataFormat: 'json',
dataSource: {
"chart": {
"caption": "",
"subcaption": "",
"showPlotBorder": "1",
"piefillalpha": "60",
"pieborderthickness": "2",
"hoverfillcolor": "#CCCCCC",
"piebordercolor": "#FFFFFF",
"hoverfillcolor": "#CCCCCC",
"numberprefix": "#",
"plottooltext": "$label",
"theme": "fusion",
},
"category": {{ pie }},
}
}
);
fusioncharts.render();
});
You can adjust the size of the outer part of the pie using pieRadius attribute at the chart object level, take a look at this sample -
FusionCharts.ready(function() {
var topProductsChart = new FusionCharts({
type: 'multilevelpie',
renderAt: 'chart-container',
id: "myChart",
width: '400',
height: '400',
dataFormat: 'json',
dataSource: {
"chart": {
"theme": "fusion",
"caption": "Split of Top Products Sold",
"subCaption": "Last Quarter",
"captionFontSize": "14",
"subcaptionFontSize": "14",
"baseFontColor": "#333333",
"baseFont": "Helvetica Neue,Arial",
"basefontsize": "9",
"subcaptionFontBold": "0",
"bgColor": "#ffffff",
"canvasBgColor": "#ffffff",
"showBorder": "0",
"showShadow": "0",
"showCanvasBorder": "0",
"pieFillAlpha": "60",
"pieBorderThickness": "2",
"hoverFillColor": "#cccccc",
"pieBorderColor": "#ffffff",
"useHoverColor": "1",
"showValuesInTooltip": "1",
"showPercentInTooltip": "0",
"numberPrefix": "$",
"plotTooltext": "$label, $$valueK, $percentValue",
"pieRadius": "170"
},
"category": [{
"label": "Sales by category",
"color": "#ffffff",
"value": "150",
"category": [{
"label": "Food & {br}Beverages",
"color": "#f8bd19",
"value": "55.5",
"category": [{
"label": "Breads",
"color": "#f8bd19",
"value": "11.1"
},
{
"label": "Juice",
"color": "#f8bd19",
"value": "27.75"
},
{
"label": "Noodles",
"color": "#f8bd19",
"value": "9.99"
},
{
"label": "Seafood",
"color": "#f8bd19",
"value": "6.66"
}
]
},
{
"label": "Apparel &{br}Accessories",
"color": "#e44a00",
"value": "42",
"category": [{
"label": "Sun Glasses",
"color": "#e44a00",
"value": "10.08"
},
{
"label": "Clothing",
"color": "#e44a00",
"value": "18.9"
},
{
"label": "Handbags",
"color": "#e44a00",
"value": "6.3"
},
{
"label": "Shoes",
"color": "#e44a00",
"value": "6.72"
}
]
},
{
"label": "Baby {br}Products",
"color": "#008ee4",
"value": "22.5",
"category": [{
"label": "Bath &{br}Grooming",
"color": "#008ee4",
"value": "9.45"
},
{
"label": "Feeding",
"color": "#008ee4",
"value": "6.3"
},
{
"label": "Diapers",
"color": "#008ee4",
"value": "6.75"
}
]
},
{
"label": "Electronics",
"color": "#33bdda",
"value": "30",
"category": [{
"label": "Laptops",
"color": "#33bdda",
"value": "8.1"
},
{
"label": "Televisions",
"color": "#33bdda",
"value": "10.5"
},
{
"label": "SmartPhones",
"color": "#33bdda",
"value": "11.4"
}
]
}
]
}]
}
});
topProductsChart.render();
});
Here is a live demo - http://jsfiddle.net/cebu68vt/
There is an Javascript object (formData) that I need to replace some of the values by comparing with another object (dataObject). The JSON for both objects is below. Now what I need is, snippet:
Main formData Object:
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}
I need to compare the "value" to the other dataObject by "name" property and replace if it's different.
dataObject to compare to:
{
"name": "abNotes",
"value": "Example Notes 123456"
}
So the replaced FormData object will be changed to:
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes 123456",
"active": true
}
Main object that needs to be replaced
JSON.stringify (formData);
form data::
{
"name": "Demo",
"fieldGroupList": [{
"name": "instructions",
"label": "Instructions",
"fieldList": [{
"name": "INSTRUCTION",
"instructionList": [{
"instructionText": "All enabled fields are required."
}],
"type": "INSTRUCTION"
}]
},
{
"name": "ab",
"label": "Ab",
"fieldList": [{
"name": "abDate",
"label": "Ab Date",
"type": "DATE",
"value": "1425186000000",
"active": true
},
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}]
},
{
"name": "Record",
"label": "Record",
"fieldList": [{
"name": "RecordNumber",
"label": "Record Number: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "1234567890",
"active": true
},
{
"name": "otherNumber",
"label": "Other: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "887766",
"active": true
},
{
"name": "eligibleAll",
"instructionList": [{
"instructionText": "Is eligible for ALL?"
}],
"type": "SINGLE_FROM_SET",
"value": "true",
"optionList": [{
"name": "Yes",
"value": "true"
},
{
"name": "No",
"value": "false"
}],
"active": true
},
{
"name": "exclusionReasonCode",
"instructionList": [{
"instructionText": "Select from the following list of sample:"
}],
"type": "SINGLE_FROM_SET",
"value": "DCS",
"optionList": [{
"name": "DCS",
"value": "Test"
}],
"active": true
}]
},
{
"name": "bDemo",
"label": "Demo",
"fieldList": [{
"name": "mId",
"label": "M ID:",
"type": "TEXT_BOX_SINGLE",
"active": false
},
{
"name": "firstName",
"label": "First Name:",
"type": "TEXT_BOX_SINGLE",
"value": "John",
"active": true
},
{
"name": "lastName",
"label": "Last Name",
"type": "TEXT_BOX_SINGLE",
"value": "Doe",
"active": true
},
{
"name": "genderCode",
"instructionList": [{
"instructionText": "Gender:"
}],
"type": "SINGLE_FROM_SET",
"optionList": [{
"name": "FEMALE",
"value": "FEMALE"
},
{
"name": "MALE",
"value": "MALE"
},
{
"name": "UNKNOWN",
"value": "UNKNOWN"
}],
"active": true
},
{
"name": "dateOfBirth",
"label": "Date of Birth:",
"type": "DATE",
"value": "-157748400000",
"active": true
}]
},
{
"name": "generalComments",
"label": "General Comments",
"fieldList": [{
"name": "comments",
"label": "Comments: (Optional)",
"type": "TEXT_BOX_MULTIPLE",
"value": "Comments Text Example",
"active": true
}]
}],
"Id": 1,
"periodId": 2015,
"orgId": 4,
"version": 1
}
The values that I need from this object:
var dataObject = $('#'+formName).serializeArray();
console.log("data:::"+JSON.stringify(dataObject));
dataObject:::
[{
"name": "abDate",
"value": "Sun Mar 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
},
{
"name": "abNotes",
"value": "Example Notes 123456"
},
{
"name": "Number",
"value": "1234567890"
},
{
"name": "otherNumber",
"value": "887766"
},
{
"name": "Yes",
"value": "true"
},
{
"name": "No",
"value": "false"
},
{
"name": "DCS",
"value": "Test"
},
{
"name": "firstName",
"value": "John"
},
{
"name": "lastName",
"value": "Doe"
},
{
"name": "FEMALE",
"value": "FEMALE"
},
{
"name": "MALE",
"value": "MALE"
},
{
"name": "UNKNOWN",
"value": "UNKNOWN"
},
{
"name": "dateOfBirth",
"value": "Fri Jan 01 1965 00:00:00 GMT-0500 (Eastern Standard Time)"
},
{
"name": "comments",
"value": "Comments Text Example"
}]
EDIT:
What I have tried so far:
$.extend(formData ,dataObject);
deepCopy:
function deepCopy(src, dest) {
var name,
value,
isArray,
toString = Object.prototype.toString;
// If no `dest`, create one
if (!dest) {
isArray = toString.call(src) === "[object Array]";
if (isArray) {
dest = [];
dest.length = src.length;
}
else { // You could have lots of checks here for other types of objects
dest = {};
}
}
// Loop through the props
for (name in src) {
// If you don't want to copy inherited properties, add a `hasOwnProperty` check here
// In our case, we only do that for arrays, but it depends on your needs
if (!isArray || src.hasOwnProperty(name)) {
value = src[name];
if (typeof value === "object") {
// Recurse
value = deepCopy(value);
}
dest[name] = value;
}
}
return dest;
}
First, convert dataObject from an array of objects to an object that maps names to values. Then go through the main form data, replacing values from the corresponding elements of doHash.
var formData = {
"name": "Demo",
"fieldGroupList": [{
"name": "instructions",
"label": "Instructions",
"fieldList": [{
"name": "INSTRUCTION",
"instructionList": [{
"instructionText": "All enabled fields are required."
}],
"type": "INSTRUCTION"
}]
}, {
"name": "ab",
"label": "Ab",
"fieldList": [{
"name": "abDate",
"label": "Ab Date",
"type": "DATE",
"value": "1425186000000",
"active": true
}, {
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}]
}, {
"name": "Record",
"label": "Record",
"fieldList": [{
"name": "RecordNumber",
"label": "Record Number: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "1234567890",
"active": true
}, {
"name": "otherNumber",
"label": "Other: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "887766",
"active": true
}, {
"name": "eligibleAll",
"instructionList": [{
"instructionText": "Is eligible for ALL?"
}],
"type": "SINGLE_FROM_SET",
"value": "true",
"optionList": [{
"name": "Yes",
"value": "true"
}, {
"name": "No",
"value": "false"
}],
"active": true
}, {
"name": "exclusionReasonCode",
"instructionList": [{
"instructionText": "Select from the following list of sample:"
}],
"type": "SINGLE_FROM_SET",
"value": "DCS",
"optionList": [{
"name": "DCS",
"value": "Test"
}],
"active": true
}]
}, {
"name": "bDemo",
"label": "Demo",
"fieldList": [{
"name": "mId",
"label": "M ID:",
"type": "TEXT_BOX_SINGLE",
"active": false
}, {
"name": "firstName",
"label": "First Name:",
"type": "TEXT_BOX_SINGLE",
"value": "John",
"active": true
}, {
"name": "lastName",
"label": "Last Name",
"type": "TEXT_BOX_SINGLE",
"value": "Doe",
"active": true
}, {
"name": "genderCode",
"instructionList": [{
"instructionText": "Gender:"
}],
"type": "SINGLE_FROM_SET",
"optionList": [{
"name": "FEMALE",
"value": "FEMALE"
}, {
"name": "MALE",
"value": "MALE"
}, {
"name": "UNKNOWN",
"value": "UNKNOWN"
}],
"active": true
}, {
"name": "dateOfBirth",
"label": "Date of Birth:",
"type": "DATE",
"value": "-157748400000",
"active": true
}]
}, {
"name": "generalComments",
"label": "General Comments",
"fieldList": [{
"name": "comments",
"label": "Comments: (Optional)",
"type": "TEXT_BOX_MULTIPLE",
"value": "Comments Text Example",
"active": true
}]
}],
"Id": 1,
"periodId": 2015,
"orgId": 4,
"version": 1
};
var dataObject = [{
"name": "abDate",
"value": "Sun Mar 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
}, {
"name": "abNotes",
"value": "Example Notes 123456"
}, {
"name": "Number",
"value": "1234567890"
}, {
"name": "otherNumber",
"value": "887766"
}, {
"name": "Yes",
"value": "true"
}, {
"name": "No",
"value": "false"
}, {
"name": "DCS",
"value": "Test"
}, {
"name": "firstName",
"value": "John"
}, {
"name": "lastName",
"value": "Doe"
}, {
"name": "FEMALE",
"value": "FEMALE"
}, {
"name": "MALE",
"value": "MALE"
}, {
"name": "UNKNOWN",
"value": "UNKNOWN"
}, {
"name": "dateOfBirth",
"value": "Fri Jan 01 1965 00:00:00 GMT-0500 (Eastern Standard Time)"
}, {
"name": "comments",
"value": "Comments Text Example"
}];
var doHash = {};
$.each(dataObject, function() {
doHash[this.name] = this.value;
});
$.each(formData.fieldGroupList, function() {
$.each(this.fieldList, function() {
if (this.name in doHash) {
this.value = doHash[this.name];
}
});
});
console.log(JSON.stringify(formData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
I know to work out simple concepts using backbone.js.below is my nested json file
{
"Re":
{
"Si":
[
{
"Def":
{
"StName": "Gau00",
"SID": "1",
"Parent": "",
"ParentID": "",
"Ty": "GAU",
"TypID": "2"
},
"Entities":
[
{
"EntityId": "2003",
"Index": "1",
"Value": "00"
},
{
"EntityId": "2006",
"Index": "1",
"Value": "B"
},
{
"EntityId": "2004",
"Index": "1",
"Value": "B"
},
{
"EntityId": "5",
"Index": "1",
"Value": "54"
},
{
"EntityId": "9007",
"Index": "1",
"Value": "1"
},
{
"EntityId": "9703",
"Index": "1",
"Value": "0"
}
],
"Connections":
[
{
"SourceID": "2",
"DestinationID": "1"
}
]
},
{
"Def":
{
"StName": "Tan",
"ID": "2",
"Parent": "",
"ParentID": "",
"Ty": "TA",
"TypID": "3"
},
"Entities": "",
"Connections":
[
{
"SourceElementID": "5",
"DestinationID": "2"
},
{
"SourceID": "2",
"DestinationID": "1"
}
]
}
]
}
}
Now with the StName i have to get all the other details from this nested json using backbone.js.Can anyone help me with ideas.
First convert JSON string to Javascript Object then use the following algorithm:
for each element in "Si":
if element["Def"]:
if element["Def"]["StName"] == "YOUR REQUIRED VALUE":
return element["Def"]
Write a function that takes your object, and iterate through the object and check StName with your required value. If condition true, return the current object.
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?