How to gather only specific data from remote JSON file - javascript

I have a local group of about 50 computers running Open Hardware Monitor and outputting a JSON file to http://theirip:8085/data.json . I have renamed the texts on certain parts to respectively CPU, RAM, GPU, HDD. I'm trying to get stuff like a Value which is a child of a CPU Core 1 which is a child of Temperatures which is a child of CPU. But I just can't figure it out.
I have some other code that loads the json (which i can see in the console), copied from an example.
I need to modify this to fetch just the temperature values of each CPU, GPU. The % use of RAM and % use of the HDD.
This is where I am stuck as I can't figure out the specific ways to output only these parts of the JSON.
Example JSON:
{
"id": 0,
"Text": "Sensor",
"Children": [{
"id": 1,
"Text": "DESKTOP-PEJT98F",
"Children": [{
"id": 2,
"Text": "Lenovo 20EV0013MB",
"Children": [],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/mainboard.png"
}, {
"id": 3,
"Text": "CPU",
"Children": [{
"id": 4,
"Text": "Clocks",
"Children": [{
"id": 5,
"Text": "Bus Speed",
"Children": [],
"Min": "100 MHz",
"Value": "100 MHz",
"Max": "100 MHz",
"ImageURL": "images/transparent.png"
}, {
"id": 6,
"Text": "CPU Core #1",
"Children": [],
"Min": "500 MHz",
"Value": "600 MHz",
"Max": "2800 MHz",
"ImageURL": "images/transparent.png"
}, {
"id": 7,
"Text": "CPU Core #2",
"Children": [],
"Min": "500 MHz",
"Value": "600 MHz",
"Max": "2800 MHz",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/clock.png"
}, {
"id": 8,
"Text": "Temperatures",
"Children": [{
"id": 9,
"Text": "CPU Core #1",
"Children": [],
"Min": "31,0 °C",
"Value": "33,0 °C",
"Max": "58,0 °C",
"ImageURL": "images/transparent.png"
}, {
"id": 10,
"Text": "CPU Core #2",
"Children": [],
"Min": "32,0 °C",
"Value": "34,0 °C",
"Max": "63,0 °C",
"ImageURL": "images/transparent.png"
}, {
"id": 11,
"Text": "CPU Package",
"Children": [],
"Min": "34,0 °C",
"Value": "37,0 °C",
"Max": "63,0 °C",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/temperature.png"
}, {
"id": 12,
"Text": "Load",
"Children": [{
"id": 13,
"Text": "CPU Total",
"Children": [],
"Min": "0,0 %",
"Value": "3,1 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}, {
"id": 14,
"Text": "CPU Core #1",
"Children": [],
"Min": "0,0 %",
"Value": "2,3 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}, {
"id": 15,
"Text": "CPU Core #2",
"Children": [],
"Min": "0,0 %",
"Value": "3,9 %",
"Max": "100,0 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}, {
"id": 16,
"Text": "Powers",
"Children": [{
"id": 17,
"Text": "CPU Package",
"Children": [],
"Min": "1,2 W",
"Value": "1,2 W",
"Max": "12,6 W",
"ImageURL": "images/transparent.png"
}, {
"id": 18,
"Text": "CPU Cores",
"Children": [],
"Min": "0,2 W",
"Value": "0,2 W",
"Max": "10,9 W",
"ImageURL": "images/transparent.png"
}, {
"id": 19,
"Text": "CPU Graphics",
"Children": [],
"Min": "0,0 W",
"Value": "0,0 W",
"Max": "0,3 W",
"ImageURL": "images/transparent.png"
}, {
"id": 20,
"Text": "CPU DRAM",
"Children": [],
"Min": "0,8 W",
"Value": "0,9 W",
"Max": "2,1 W",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/power.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/cpu.png"
}, {
"id": 21,
"Text": "RAM",
"Children": [{
"id": 22,
"Text": "Load",
"Children": [{
"id": 23,
"Text": "Memory",
"Children": [],
"Min": "22,2 %",
"Value": "30,9 %",
"Max": "33,3 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}, {
"id": 24,
"Text": "Data",
"Children": [{
"id": 25,
"Text": "Used Memory",
"Children": [],
"Min": "1,7 GB",
"Value": "2,3 GB",
"Max": "2,5 GB",
"ImageURL": "images/transparent.png"
}, {
"id": 26,
"Text": "Available Memory",
"Children": [],
"Min": "5,0 GB",
"Value": "5,2 GB",
"Max": "5,8 GB",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/power.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/ram.png"
}, {
"id": 27,
"Text": "HDD",
"Children": [{
"id": 28,
"Text": "Temperatures",
"Children": [{
"id": 29,
"Text": "Temperature",
"Children": [],
"Min": "29,0 °C",
"Value": "30,0 °C",
"Max": "37,0 °C",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/temperature.png"
}, {
"id": 30,
"Text": "Load",
"Children": [{
"id": 31,
"Text": "Used Space",
"Children": [],
"Min": "60,6 %",
"Value": "60,6 %",
"Max": "60,6 %",
"ImageURL": "images/transparent.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/load.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/hdd.png"
}],
"Min": "",
"Value": "",
"Max": "",
"ImageURL": "images_icon/computer.png"
}],
"Min": "Min",
"Value": "Value",
"Max": "Max",
"ImageURL": ""
}
Script in header (from an example):
<script>
$(document).ready(function() {
$("#retrieve-resources").click(function() {
var displayResources = $("#display-resources");
displayResources.text("Loading data from JSON source...");
$.ajax({
type: "GET",
url: "http://exampleIPhere:8085/data.json",
success: function(result) {
console.log(result);
var output =
"<table><thead><tr><th>Name</th><th>Provider</th><th>URL</th></thead><tbody>";
for (var i in result) {
output +=
"<tr><td>" +
result[i].name +
"</td><td>" +
result[i].provider +
"</td><td>" +
result[i].url +
"</td></tr>";
}
output += "</tbody></table>";
displayResources.html(output);
$("table").addClass("table");
}
});
});
});
</script>
HTML:
<div id="display-resources">
<p>jQuery example using jQuery.ajax() to retrieve a list of JavaScript Training Resources.</p>
</div>
Retrieve List
Any push in the right direction is massively appreciated!

I think the tricky part of this question is traversing the JSON. When working with JSON files I often find it helpful to paste the sample output into a site like this to make it more readable:
http://jsoneditoronline.org/
In this particular example, you might not need to even iterate over the results if each computer is only outputting a JSON file as you've pasted above.
console.log(obj.Children[0].Children[1].Children[0].Children[1].Text);
However, the previous poster is correct about iterating through the results if there are multiple. I notice you're using jQuery in your sample code. You'd do something like this in that example:
obj = JSONResult; // your JSON
$.each(obj, function(index, element) {
});

Related

How Make TreeData to Array

I am having a hard time applying the data received from the database to select components.
My database is
id
code
name
parent_name
1
1000
Marvel
2
1100
X-man
Marvel
3
1110
Wolverine
X-man
4
1120
Professor X
X-man
5
1200
Avengers
Marvel
6
1210
Captain America
Avengers
7
1211
Peggy Carter
Captain America
8
1220
Iron Man
Avengers
9
1221
Tony Startk
Iron Man
10
1222
War Machines
Iron Man
11
2000
DC
12
2100
BatMan
DC
13
2200
SuperMan
DC
dbDataArray is here
type TreeSelectType = {
title: string;
value: string;
key: string;
parentName: string | undefined;
children: TreeSelectType[];
};
const dbDataArray: TreeSelectType[] = [];
dbData.forEach(data => {
dbDataArray.push({
"title": data.name,
"value": data.code,
"key": data.code,
"parentName": data.parent_name,
"children": []
});
});
console.log(dbDataArray)
[
{
"title": "Marvel",
"value": "1000",
"key": "1000",
"parentName": null,
"children": []
},
{
"title": "X-man",
"value": "1100",
"key": "1100",
"parentName": "Marvel",
"children": []
},
{
"title": "Wolverine",
"value": "1110",
"key": "1110",
"parentName": "X-man",
"children": []
},
{
"title": "Professor X",
"value": "1120",
"key": "1120",
"parentName": "X-man",
"children": []
},
{
"title": "Avengers",
"value": "1200",
"key": "1200",
"parentName": "Marvel",
"children": []
},
{
"title": "Captain America",
"value": "1210",
"key": "1210,
"parentName": "Avengers",
"children": []
},
{
"title": "Peggy Carter",
"value": "1211",
"key": "1211",
"parentName": "Captain America",
"children": []
},
{
"title": "Iron Man",
"value": "1220",
"key": "1220",
"parentName": "Avengers",
"children": []
},
{
"title": "Tony Startk",
"value": "1221",
"key": "1221",
"parentName": "Iron Man",
"children": []
},
{
"title": "War Machines",
"value": "1222",
"key": "1222",
"parentName": "Iron Man",
"children": []
},
{
"title": "DC",
"value": "2000",
"key": "2000",
"parentName": null,
"children": []
},
{
"title": "BatMan",
"value": "2100",
"key": "2100",
"parentName": "DC",
"children": []
},
{
"title": "SuperMan",
"value": "2200",
"key": "2200",
"parentName": "DC",
"children": []
},
]
I need to treeDataArray. like this.
[
{
"title": "Marvel",
"value": "1000",
"key": "1000",
"parentName": "Marvel",
"children": [
{
"title": "X-man",
"value": "1100",
"key": "1100",
"parentName": "Marvel",
"children": [
{
"title": "Wolverine",
"value": "1110",
"key": "1110",
"parentName": "X-man",
"children": []
},
{
"title": "Professor X",
"value": "1120",
"key": "1120",
"parentName": "X-man",
"children": []
},
],
},
{
"title": "Avengers",
"value": "1200",
"key": "1200",
"parentName": "Marvel",
"children": [
{
"title": "Captain America",
"value": "1210",
"key": "1210,
"parentName": "Avengers",
"children": [
{
"title": "Peggy Carter",
"value": "1211",
"key": "1211",
"parentName": "Captain America",
"children": []
},
]
},
{
"title": "Iron Man",
"value": "1220",
"key": "1220",
"parentName": "Avengers",
"children": [
{
"title": "Tony Startk",
"value": "1221",
"key": "1221",
"parentName": "Iron Man",
"children": []
},
{
"title": "War Machines",
"value": "1222",
"key": "1222",
"parentName": "Iron Man",
"children": []
},
]
},
]
},
]
},
{
"title": "DC",
"value": "2000",
"key": "2000",
"parentName": null,
"children": [
{
"title": "BatMan",
"value": "2100",
"key": "2100",
"parentName": "DC",
"children": []
},
{
"title": "SuperMan",
"value": "2200",
"key": "2200",
"parentName": "DC",
"children": []
},
]
},
]
How Can I... please help me
You could create a Map keyed by title and with the corresponding objects as values. Then use that map to retrieve for each object the parent object, and insert it in the parent's children array.
Here is how that could look:
const dbDataArray = [{"title": "Marvel","value": "1000","key": "1000","parentName": null,"children": []},{"title": "X-man","value": "1100","key": "1100","parentName": "Marvel","children": []},{"title": "Wolverine","value": "1110","key": "1110","parentName": "X-man","children": []},{"title": "Professor X","value": "1120","key": "1120","parentName": "X-man","children": []},{"title": "Avengers","value": "1200","key": "1200","parentName": "Marvel","children": []},{"title": "Captain America","value": "1210","key": "1210","parentName": "Avengers","children": []},{"title": "Peggy Carter","value": "1211","key": "1211","parentName": "Captain America","children": []},{"title": "Iron Man","value": "1220","key": "1220","parentName": "Avengers","children": []},{"title": "Tony Startk","value": "1221","key": "1221","parentName": "Iron Man","children": []},{"title": "War Machines","value": "1222","key": "1222","parentName": "Iron Man","children": []},{"title": "DC","value": "2000","key": "2000","parentName": null,"children": []},{"title": "BatMan","value": "2100","key": "2100","parentName": "DC","children": []},{"title": "SuperMan","value": "2200","key": "2200","parentName": "DC","children": []},]
const result = [];
const map = new Map(dbDataArray.map(o => [o.title, o])).set(null, {children: result});
for (const o of dbDataArray) map.get(o.parentName).children.push(o);
console.log(result);

Search the key of the highest value and specific value [duplicate]

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]);

Traverse json and display property value

I am trying to output parts of the json below:
How would I output the value pint from beer.name.568.name ?
{
"beer": {
"name": "Beer or cider",
"sizes": {
"568": {
"name": "Pint",
"size": 0.568,
"id": "pint",
"max": 10,
"icon": "beer_pint"
},
"440": {
"name": "Can",
"size": 0.44,
"id": "can",
"max": 10,
"icon": "beer_can"
},
"330": {
"name": "Bottle",
"size": "0.33",
"id": "bottle",
"max": "10",
"icon": "beer_bottle_330"
},
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "smallBottle",
"max": 10,
"icon": "beer_bottle_275"
}
},
"strength": [4, 4.5, 5, 6, 7, 8, 9]
},
"wine": {
"name": "Wine or champagne",
"sizes": {
"125": {
"name": "Small glass",
"size": 0.125,
"id": "small",
"max": 10,
"icon": "wine_small_glass"
},
"175": {
"name": "Standard glass",
"size": 0.175,
"id": "std",
"max": 10,
"icon": "wine_standard_glass"
},
"250": {
"name": "Large glass",
"size": 0.25,
"id": "large",
"max": 10,
"icon": "wine_large_glass"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 10,
"icon": "wine_bottle"
}
},
"strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
},
"spirits": {
"name": "Spirits or shots",
"sizes": {
"25": {
"name": "Single",
"size": 0.025,
"id": "single",
"max": 10,
"icon": "spirit_single"
},
"35": {
"name": "Large single",
"size": 0.035,
"id": "lgSingle",
"max": 10,
"icon": "spirit_large_single"
},
"50": {
"name": "Double",
"size": 0.05,
"id": "double",
"max": 10,
"icon": "spirit_double"
},
"70": {
"name": "Large double",
"size": 0.07,
"id": "lgDouble",
"max": 10,
"icon": "spirit_large_double"
},
"700": {
"name": "Bottle",
"size": 0.7,
"id": "bottle700",
"max": 3,
"icon": "spirit_bottles"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 3,
"icon": "spirit_bottles"
}
},
"strength": [37, 40]
},
"alcopop": {
"name": "Alcopop",
"sizes": {
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "small",
"max": 10,
"icon": "alcopops_small_bottle"
},
"330": {
"name": "Standard bottle",
"size": 0.33,
"id": "std",
"max": 10,
"icon": "alcopops_standard_bottle"
},
"750": {
"name": "Large bottle",
"size": 0.75,
"id": "large",
"max": 10,
"icon": "alcopops_large_bottle"
}
},
"strength": [5, 5.5]
}
}
Obviously this approach may have many drawbacks as the loop can go infinite in some situations, but, if you know that your structure is "stable", this can simulate a search in some dynamic json schema, when you know a key and want to get a property value.
var data = {
"beer": {
"name": "Beer or cider",
"sizes": {
"568": {
"name": "Pint",
"size": 0.568,
"id": "pint",
"max": 10,
"icon": "beer_pint"
},
"440": {
"name": "Can",
"size": 0.44,
"id": "can",
"max": 10,
"icon": "beer_can"
},
"330": {
"name": "Bottle",
"size": "0.33",
"id": "bottle",
"max": "10",
"icon": "beer_bottle_330"
},
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "smallBottle",
"max": 10,
"icon": "beer_bottle_275"
}
},
"strength": [4, 4.5, 5, 6, 7, 8, 9]
},
"wine": {
"name": "Wine or champagne",
"sizes": {
"125": {
"name": "Small glass",
"size": 0.125,
"id": "small",
"max": 10,
"icon": "wine_small_glass"
},
"175": {
"name": "Standard glass",
"size": 0.175,
"id": "std",
"max": 10,
"icon": "wine_standard_glass"
},
"250": {
"name": "Large glass",
"size": 0.25,
"id": "large",
"max": 10,
"icon": "wine_large_glass"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 10,
"icon": "wine_bottle"
}
},
"strength": [9, 10, 10.5, 11, 11.5, 12, 13, 14, 15, 16, 17]
},
"spirits": {
"name": "Spirits or shots",
"sizes": {
"25": {
"name": "Single",
"size": 0.025,
"id": "single",
"max": 10,
"icon": "spirit_single"
},
"35": {
"name": "Large single",
"size": 0.035,
"id": "lgSingle",
"max": 10,
"icon": "spirit_large_single"
},
"50": {
"name": "Double",
"size": 0.05,
"id": "double",
"max": 10,
"icon": "spirit_double"
},
"70": {
"name": "Large double",
"size": 0.07,
"id": "lgDouble",
"max": 10,
"icon": "spirit_large_double"
},
"700": {
"name": "Bottle",
"size": 0.7,
"id": "bottle700",
"max": 3,
"icon": "spirit_bottles"
},
"1000": {
"name": "Bottle",
"size": 1,
"id": "bottle",
"max": 3,
"icon": "spirit_bottles"
}
},
"strength": [37, 40]
},
"alcopop": {
"name": "Alcopop",
"sizes": {
"275": {
"name": "Small bottle",
"size": 0.275,
"id": "small",
"max": 10,
"icon": "alcopops_small_bottle"
},
"330": {
"name": "Standard bottle",
"size": 0.33,
"id": "std",
"max": 10,
"icon": "alcopops_standard_bottle"
},
"750": {
"name": "Large bottle",
"size": 0.75,
"id": "large",
"max": 10,
"icon": "alcopops_large_bottle"
}
},
"strength": [5, 5.5]
}
};
function findPropertyValueForKey(prev, key, property, data) {
if (typeof data != "object") return;
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
if (keys[i] == key) {
prev.push(data[key][property]);
} else {
findPropertyValueForKey(prev, key, property, data[keys[i]]);
}
}
};
var results = [];
findPropertyValueForKey(results, "568", "name", data);
console.log(results);
var beer = {
"sizes": {
"568": {
"name": "Pint"
}
}
}
console.log(beer.sizes[568].name); // "Pint"
console.log(beer['sizes'][568]['name']); // "Pint"

JavaScript time comparison with arrays

I'm working on a project where I need to get all the vacant classrooms for the day and basically filter out the ones being in use.
I get all the data from the school's own API with JSON response body that looks like this:
{
"status": "success",
"reservations": [
{
"id": "18935",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:51",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "50",
"type": "room",
"code": "A440.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A440.5"
},
{
"id": "2995",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20362",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T14:00:00",
"resources": [
{
"id": "51",
"type": "room",
"code": "A450.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.1"
},
{
"id": "2402",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3064",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20237",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:15:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "45",
"type": "room",
"code": "A420.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A420.4"
},
{
"id": "2433",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3058",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20888",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:57",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "62",
"type": "room",
"code": "A520.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.5"
},
{
"id": "3092",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2444",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "22586",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:48",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "52",
"type": "room",
"code": "A450.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.3"
},
{
"id": "3004",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2294",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "525",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18816",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:58",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "41",
"type": "room",
"code": "A340.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.1"
},
{
"id": "2989",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "795",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "599",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20431",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:56",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "40",
"type": "room",
"code": "A320.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A320.7/8"
},
{
"id": "2416",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2386",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18588",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:49",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "25",
"type": "room",
"code": "A130.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.1"
},
{
"id": "26",
"type": "room",
"code": "A130.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.3"
},
{
"id": "2979",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "582",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18940",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "2996",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2268",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "31",
"type": "room",
"code": "A210.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A210.2"
}
],
"description": ""
},
{
"id": "12041",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "2510",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "23",
"type": "room",
"code": "A520.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.7"
}
],
"description": ""
},
{
"id": "24630",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "3277",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "42",
"type": "room",
"code": "A340.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.2"
}
],
"description": ""
},
{
"id": "27205",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:07",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "35",
"type": "room",
"code": "A240.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.2"
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3384",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "25917",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:00",
"startDate": "2017-04-27T15:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "36",
"type": "room",
"code": "A240.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.4"
},
{
"id": "593",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "595",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "21932",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:06",
"startDate": "2017-04-27T16:00:00",
"endDate": "2017-04-27T18:00:00",
"resources": [
{
"id": "43",
"type": "room",
"code": "A350.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A350.1"
},
{
"id": "2464",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2747",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
}
]
}
In order to get the JSON response I need to put startDate (the moment user searches the vacant rooms, in this situation it is: 2017-04-27T10:55) and endDate (set to end of the day, 2017-04-27T22:00). The result for this query is the JSON response above.
The problem is that the API I'm using doesn't contain any data for the vacant rooms but only for the ones booked for certain times so I made a list of all the rooms in the building and compared it to the booked ones to filter them out:
var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1',
'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];
var data = JSON.parse(responseText);
var booking = Object.create(null);
var free;
data.reservations.forEach(function (reservation) {
reservation.resources.some(function (resource) {
if (resource.type === 'room') {
booking[resource.code] = booking[resource.code] || [];
booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
return true;
}
});
});
free = rooms.filter(function (a) {
return !booking[a];
});
console.log(free);
Results:
A210.3
A220.5
A320.2
A520.6
A510.4
This only returns the ones that are not being used at all during the day but I need to get the ones that are vacant for hour or two.
For this booked class for example:
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"type": "room",
"code": "A520.5"
for this I would need to print out:
A520.5 - 2 hours 20 minutes
So I need to get the room / time from the startDate of the search (2017-04-27T10:55) and compare it to the startDate of the booked room (2017-04-27T13:15:00) to get the time remaining for that room to be vacant.
TO CLARIFY:
I have all the data for booked rooms starting from 2017-04-27T10:55
until 2017-04-27T22:00 as you can see from the JSON response above.
I need to somehow compare the startDate of the JSON query when the user
searches for the rooms (2017-04-27T10:55) and compare it to the booked
rooms startDate to get the time how long the room stays vacant.
This is how I'm doing my startDate and endDate for the JSON query:
// Timestamp needs to be formed (YYYY-MM-DDTxx:xx) in order for JSON query to work
var todaysDate = new Date();
function convertDate(date) {
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth() + 1).toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
}
// Current time when user searches for vacant rooms
var currentTime = new Date();
var time = "T" + currentTime.getHours() + ":" + currentTime.getMinutes();
// variables for the JSON query
var startDate = convertDate(todaysDate) + time;
var endDate = convertDate(todaysDate) + ("T22:00");
// JSON-query
var getRooms = {
"startDate": startDate,
"endDate": endDate
};

Angular track by object.id

I have following JSON data received form REST api.
[
{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
},
{
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}
]
I'm trying to display it using directive ng-repeat. Using track by object.id.
It is used like this:
<tr ng-repeat="chapter in chapters | filter: search track by chapter.id">
But the problem is that the ngRepeat:Dupes error still appear. I have checked the data contained in JSON, but there is no duplicite id in it. Do you know why the ngRepeat:Dupes error persists?
Based on the data given, no duplicate error is thrown, below is jsfiddle.
<div ng-controller="MainCtrl">
<input type="text" ng-model="search">
<div ng-repeat="chapter in chapters | filter: search track by chapter.id">{{chapter.id}}</div>
</div>
controller
$scope.chapters = [{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}, {
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}]
http://jsfiddle.net/75sdsuz2/2/

Categories

Resources