I have an API call in my React js component which is returning a JSON object.
Below is the JSON response:
[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
Now for every date I want to form an array depending on Type(i.e. 'C' or 'D') with all success, failure and Data array but I am unable to get through Details.
Basically,I want an array of data for each date as such:
[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
Below is what I tried:
var final=[];
for (const item of data){
final.push(item.Details);
}
console.log(final);
});
This is not returning the array with date.
I tried foreach loop but that does not seems to work.
Any idea how can I achieve that?
let input=[
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
let output =input.map((item)=>{
return {"date":item.date,"Details":item.Details}
});
console.log(output)
var obj = [
{
"date": "2019-05-03 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
},
{
"date": "2019-05-06 ",
"Details": [
{
"Type": "D",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABWW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
},
{
"Type": "C",
"total": "1",
"success": "0",
"failure": "1",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "GWTHBC",
"Number": "3340373"
}
]
}
]
}
]
},
{
"date": "2019-05-07 ",
"Details": [
{
"Type": "D",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
},
{
"Type": "C",
"total": "11",
"success": "8",
"failure": "3",
"Data": [
{
"name": "FailurePoint1",
"count": 2,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "53345342"
},
{
"Cd": "SABW",
"NAME": "OCYHEH",
"Number": "5334534"
}
]
},
{
"name": "FailurePoint2",
"count": 1,
"TransDetails": [
{
"Cd": "SABW",
"NAME": "ISMIWP",
"Number": "7020191"
}
]
}
]
}
]
},
{
"date": "2019-05-04 ",
"Details": [
{
"Type": "D",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
},
{
"Type": "C",
"total": "0",
"success": "0",
"failure": "0",
"Data": [
{
"name": "FailurePoint1",
"count": 0,
"TransDetails": []
},
{
"name": "FailurePoint2",
"count": 0,
"TransDetails": []
}
]
}
]
}
]
var output = []
obj.forEach(elem => {
elem.Details.forEach( det => {
if(det.Type === "D")
output.push( {"date": elem.date, "Details": det} )
})
})
console.log(output);
This is what you need right ?
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));
[{
"_id": "5fd6288a155cda5a10d067fa",
"contacts": [
{
"_id": "5fd6288a155cda5a10d067fb",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd6288a155cda5a10d067fc",
"id": "12",
"name": "john"
},
{
"_id": "5fd6288a155cda5a10d067fd",
"id": "1",
"name": "brad"
}
],
"messages": [],
"__v": 0
},
{
"_id": "5fd63ab97aac3826f8e64558",
"contacts": [
{
"_id": "5fd63ab97aac3826f8e64559",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd63ab97aac3826f8e6455a",
"id": "12",
"name": "brad"
}
],
"messages": [],
"__v": 0
}]
Quick overview: each object contains a different conversation data.
I want to extract the names out of each conversation and store them in a separate array.
for instance: [[dean, john, brad], [dean, brad]]
const arr = [{
"_id": "5fd6288a155cda5a10d067fa",
"contacts": [{
"_id": "5fd6288a155cda5a10d067fb",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd6288a155cda5a10d067fc",
"id": "12",
"name": "john"
},
{
"_id": "5fd6288a155cda5a10d067fd",
"id": "1",
"name": "brad"
}
],
"messages": [],
"__v": 0
},
{
"_id": "5fd63ab97aac3826f8e64558",
"contacts": [{
"_id": "5fd63ab97aac3826f8e64559",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd63ab97aac3826f8e6455a",
"id": "12",
"name": "brad"
}
],
"messages": [],
"__v": 0
}
];
const people = arr.map(ele => ele.contacts.map(contact => contact.name));
console.log(people);
Map the array:
const people = arr.map(ele => ele.contacts.map(contact => contact.name));
You can map over the elements and extract the data out.
var your_elements_array = [{
"_id": "5fd6288a155cda5a10d067fa",
"contacts": [
{
"_id": "5fd6288a155cda5a10d067fb",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd6288a155cda5a10d067fc",
"id": "12",
"name": "john"
},
{
"_id": "5fd6288a155cda5a10d067fd",
"id": "1",
"name": "brad"
}
],
"messages": [],
"__v": 0
},
{
"_id": "5fd63ab97aac3826f8e64558",
"contacts": [
{
"_id": "5fd63ab97aac3826f8e64559",
"id": "5fd35931ec23f76d387d8464",
"name": "dean"
},
{
"_id": "5fd63ab97aac3826f8e6455a",
"id": "12",
"name": "brad"
}
],
"messages": [],
"__v": 0
}];
let x = your_elements_array.map(element => {
return element["contacts"].map(element => {
return element["name"];
});
});
console.log(x);
your_elements_array.map(element => {
return element["contacts"].map(element => {
return element["name"];
});
});
I have a javascript object returned from an elastcisearch query that looks like this:
let resp = {
'var1': 'ex',
"aggregations": {
"terms_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "118917059",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "2",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "118917059"
}
}]
}
}
},
{
"key": "20584263",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "1",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "20584263"
}
}]
}
}
},
{
"key": "253981722",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "3",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "253981722"
}
}]
}
}
},
}
}
}
I only want the hits.hits[0] object for each element in the aggregations.buckets list. So I am trying to convert my resp object into a newResp object that would look like this:
newResp = [
{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "2",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "118917059"
}
},
{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "1",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "20584263"
}
},
{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "3",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "253981722"
}
},
]
]
What is the most efficient way to go about doing this? the first solution that comes to my mind is the brute force approach like this:
let newResp = []
for (item in resp.aggregations.buckets) {
newResp.append(item.hits.hits[0)
}
But is there a cleaner solution for creating newResp?
Just map() it.
let resp = {
'var1': 'ex',
"aggregations": {
"terms_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "118917059",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "2",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "118917059"
}
}]
}
}
},
{
"key": "20584263",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "1",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "20584263"
}
}]
}
}
},
{
"key": "253981722",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "3",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "253981722"
}
}]
}
}
},
]
}
}
}
const result = resp.aggregations.terms_agg.buckets.map(bucket => bucket.top_hits_agg.hits.hits[0]);
console.log(result);
Just map() it.
let resp = {
'var1': 'ex',
"aggregations": {
"terms_agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "118917059",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "2",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "118917059"
}
}]
}
}
},
{
"key": "20584263",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "1",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "20584263"
}
}]
}
}
},
{
"key": "253981722",
"doc_count": 4,
"top_hits_agg": {
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 14.090103,
"hits": [{
"_index": "x-18-07-20201",
"_type": "_doc",
"_id": "3",
"_score": 14.090103,
"_source": {
"default_asin": null,
"scraping_status": null,
"sku": "253981722"
}
}]
}
}
},
]
}
}
}
const result = resp
.aggregations
.terms_agg
.buckets
.map(bucket => bucket.top_hits_agg.hits.hits[0]);
console.log(result);
I'm trying to display a Kendo treeview thanks to a HierarchicalDataSource.
It's a simple region, country, city hierarchy but the difficulty comes with the fact that we have 3 possible levels of regions and countries can appear at the second or at the third level.
Basically,
- First level of regions contains only other regions
- Second level of regions contains other regions or countries
- Third level of regions contains only countries
Here are my schemas:
var portsSchema = {
schema: {
data: "PortList",
model: {
id: "Code"
}
}
};
var countrySchema = {
schema: {
data: "CountryList",
model: {
id: "Code",
children: portsSchema
}
}
};
var regionCountrySchema = {
schema: {
data: "RegionList",
model: {
id: "id",
children: countrySchema
}
}
};
var regionSchema = {
schema: {
data: "RegionList",
model: {
id: "id",
children: regionCountrySchema
}
}
};
Depending on the fact that the region has countries or not, I would like to specify a specific type of children (regionSchema or regionCountrySchema).
var tvDataSource = new kendo.data.HierarchicalDataSource({
data: regions,
schema: {
model: function (data) {
**how to return the right children schema ?**
}
}
});
Returning { children: regionSchema } or {children: regionCountrySchema } triggers a js kendo error.
Any idea to achieve this ? Thank you.
JSON data sample below.
[
{
"Id": 1,
"Name": "MIDDLE EAST AND RED SEA",
"RegionList": [
{
"Id": 12,
"Name": "MIDDLE EAST",
"RegionList": [
{
"Id": 45,
"Name": "M. EAST",
"RegionList": [
],
"CountryList": [
{
"Id": 12007,
"Code": "AE",
"Name": "UAE",
"PortList": [
{
"Id": 6005,
"Code": "AEJEA",
"Name": "JEBEL ALI"
},
{
"Id": 16014,
"Code": "AEAJM",
"Name": "AJMAN"
},
{
"Id": 16015,
"Code": "AEAUH",
"Name": "ABU DHABI"
},
{
"Id": 15109,
"Code": "AEKLF",
"Name": "KHOR AL FAKKAN"
},
{
"Id": 15001,
"Code": "AERKT",
"Name": "RAS AL KHAIMAH"
},
{
"Id": 16018,
"Code": "AESHJ",
"Name": "SHARJAH"
},
{
"Id": 14863,
"Code": "AEQIW",
"Name": "UMM AL QAIWAIN"
},
{
"Id": 15647,
"Code": "AEFJR",
"Name": "AL - FUJAYRAH"
}
]
},
{
"Id": 12018,
"Code": "OM",
"Name": "OMAN",
"PortList": [
{
"Id": 6011,
"Code": "OMSLL",
"Name": "SALALAH"
},
{
"Id": 16218,
"Code": "OMSOH",
"Name": "SOHAR"
}
]
},
{
"Id": 10069,
"Code": "BH",
"Name": "BAHRAIN",
"PortList": [
{
"Id": 15345,
"Code": "BHKBS",
"Name": "BAHRAIN"
}
]
},
{
"Id": 62292,
"Code": "IQ",
"Name": "IRAQ",
"PortList": [
{
"Id": 15383,
"Code": "IQBSR",
"Name": "BASRA"
},
{
"Id": 14673,
"Code": "IQUQR",
"Name": "UMM QASR PT"
}
]
},
{
"Id": 62291,
"Code": "IR",
"Name": "IRAN, ISLAMIC REPUBLIC OF",
"PortList": [
{
"Id": 15250,
"Code": "IRBKM",
"Name": "BANDAR KHOMEINI"
},
{
"Id": 15249,
"Code": "IRBND",
"Name": "BANDAR ABBAS"
},
{
"Id": 14973,
"Code": "IRBUZ",
"Name": "BUSHEHR"
},
{
"Id": 14671,
"Code": "IRKHO",
"Name": "KHORRAMSHAHR"
}
]
},
{
"Id": 62306,
"Code": "KW",
"Name": "KUWAIT",
"PortList": [
{
"Id": 15810,
"Code": "KWSAA",
"Name": "SHUAIBA"
},
{
"Id": 15811,
"Code": "KWSWK",
"Name": "SHUWAIKH"
}
]
},
{
"Id": 12002,
"Code": "SA",
"Name": "SAUDI ARABIA",
"PortList": [
{
"Id": 15039,
"Code": "SAJUB",
"Name": "JUBAIL"
},
{
"Id": 16147,
"Code": "SADMM",
"Name": "AD DAMMAM"
}
]
},
{
"Id": 62364,
"Code": "QA",
"Name": "QATAR",
"PortList": [
{
"Id": 15739,
"Code": "QADOH",
"Name": "DOHA"
},
{
"Id": 14795,
"Code": "QAMES",
"Name": "MESAIEED"
}
]
}
]
}
],
"CountryList": [
]
},
{
"Id": 30,
"Name": "RED SEA",
"RegionList": [
{
"Id": 65,
"Name": "RED SEA",
"RegionList": [
],
"CountryList": [
]
}
],
"CountryList": [
{
"Id": 12002,
"Code": "SA",
"Name": "SAUDI ARABIA",
"PortList": [
{
"Id": 6003,
"Code": "SAKAC",
"Name": "KING ABDULLAH PORT"
},
{
"Id": 15731,
"Code": "SAJED",
"Name": "JEDDAH"
}
]
},
{
"Id": 10114,
"Code": "DJ",
"Name": "DJIBOUTI",
"PortList": [
{
"Id": 8059,
"Code": "DJJIB",
"Name": "DJIBOUTI"
}
]
},
{
"Id": 10122,
"Code": "ER",
"Name": "ERITREA",
"PortList": [
{
"Id": 15031,
"Code": "ERMSW",
"Name": "MASSAWA"
}
]
},
{
"Id": 62300,
"Code": "JO",
"Name": "JORDAN",
"PortList": [
{
"Id": 14801,
"Code": "JOAQJ",
"Name": "AL \u0027AQABAH"
}
]
},
{
"Id": 50001,
"Code": "SD",
"Name": "sd",
"PortList": [
{
"Id": 15734,
"Code": "SDPZU",
"Name": "PORT SUDAN"
}
]
},
{
"Id": 62425,
"Code": "YE",
"Name": "YEMEN",
"PortList": [
{
"Id": 15302,
"Code": "YEHOD",
"Name": "HODEIDAH"
},
{
"Id": 15304,
"Code": "YEMKX",
"Name": "MUKALLA"
},
{
"Id": 15300,
"Code": "YEADE",
"Name": "ADEN"
}
]
},
{
"Id": 10118,
"Code": "EG",
"Name": "EGYPT",
"PortList": [
{
"Id": 16030,
"Code": "EGSOK",
"Name": "SOKHNA PORT"
}
]
}
]
}
],
"CountryList": [
]
}
]
My MVC return this JSON
[
{
"$id":"1",
"Tags":
[
{"$id":"2","Values":
[
{"$id":"3","Tag":{"$ref":"2"},"ID":4,"TagID":1,"Value1":5.00,"TimeStamp":"2015-10-26T15:23:50","Quality":1,"Tags_ID":1},
{"$id":"4","Tag":{"$ref":"2"},"ID":7,"TagID":1,"Value1":4.00,"TimeStamp":"2015-10-26T15:25:50","Quality":2,"Tags_ID":1}
],"Reports":[{"$ref":"1"}],"ID":1,"Name":"0101WT370/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"5","Values":[],"Reports":[{"$ref":"1"}],"ID":2,"Name":"0101WT371/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"6","Values":[],"Reports":[{"$ref":"1"}],"ID":3,"Name":"0101WT395/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"7","Values":[],"Reports":[{"$ref":"1"}],"ID":4,"Name":"0101WT396/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null}
],
"ID":3,"Name":"A"
},
{
"$id":"8",
"Tags":[],"ID":4,"Name":"B"
}
]
And i want winth Angular to group values by Timestamp part - hourly, daily or monthly
I want this result example for daily:
Tag | Timestamp | Value |
Tag1| 26-10-2015 | 4.5 = (9/2) = value1+value2 / count
Can you give me any suggestion?
EDIT:
I found this where give me something like way to start from :)
Try the code below :
JAVASCRIPT
function sortTimeStamp(obj) {
for(i=0; i<obj.length;i++) {
for(z=0; z<obj[i].Tags.length;z++) {
for(y=0; y<obj[i].Tags[z].Values.length;y++) {
obj[i].Tags[z].Values.sort(function (a, b) {
return new Date(b.TimeStamp).getTime() - new Date(a.TimeStamp).getTime();
});
}
}
}
return obj;
}
var myObj = [
{
"$id": "1",
"Tags": [
{
"$id": "2",
"Values": [
{
"$id": "3",
"Tag": {
"$ref": "2"
},
"ID": 4,
"TagID": 1,
"Value1": 5,
"TimeStamp": "2015-10-26T15:23:50",
"Quality": 1,
"Tags_ID": 1
},
{
"$id": "4",
"Tag": {
"$ref": "2"
},
"ID": 7,
"TagID": 1,
"Value1": 4,
"TimeStamp": "2015-10-26T15:25:50",
"Quality": 2,
"Tags_ID": 1
}
],
"Reports": [
{
"$ref": "1"
}
],
"ID": 1,
"Name": "0101WT370/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "5",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 2,
"Name": "0101WT371/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "6",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 3,
"Name": "0101WT395/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "7",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 4,
"Name": "0101WT396/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
}
],
"ID": 3,
"Name": "A"
},
{
"$id": "8",
"Tags": [],
"ID": 4,
"Name": "B"
}
];
myObj = sortTimeStamp(myObj);
console.log(myObj);
https://jsfiddle.net/3y7wfqwq/