javascript remove elements from object efficiently - javascript

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

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

Looping through objects and creating array then combining on id

I have a problem trying to build an array, then combine and output multiple objects depending on the id.
This is the problem (I hope it makes sense):
I am looping through multiple objects, then create an array depending on the value of the type of the object. I then take that array and build it into the object. Once that is done I need to run through all the objects and combine any that have the same id.
this is an example of how it would originally look:
[{
"id": "755",
"entities": ["14394551"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 43,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"],
"exchangerate": 1
}, {
"id": "755",
"entities": ["14394553"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 28,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
"exchangerate": 1
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 38,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14434400"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 18,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14430895"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 63,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "Dep",
"salesOrderId": ["14430894"],
"memo": ["MICGB2452"],
"exchangerate": 1
}, {
"id": "762",
"entities": ["14350538"],
"accountID": "762",
"accountName": "122100 TEST1",
"amount": 45,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
"exchangerate": 1
}, {
"id": "760",
"entities": ["14350538"],
"accountID": "760",
"accountName": "122100 TEST2",
"amount": 49,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"],
"exchangerate": 1
}]
then this is after the array has been created -> Depending on the type.
[
{
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394551"],
"amount": 43,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"]
}],
"deposit": []
}, {
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394553"],
"amount": 28,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
}],
"deposit": []
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14439896"],
"amount": 38,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"]
}],
"deposit": []
}, {
"id": "758",
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit" : [{
"entities": ["14434400"],
"amount": 18,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"]
}],
"deposit": []
}, {
"id": "758",
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "Dep",
"exchangerate": 1,
"customerDeposit": [],
"deposit": [{
"entities": ["14430895"],
"amount": 63,
"memo": ["MICGB2452"],
}]
}, {
"id": "762",
"accountID": "762",
"accountName": "122100 TEST1",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [
{
"entities": ["14350538"],
"amount": 45,
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
}
],
"deposit": []
}, {
"id": "760",
"accountID": "760",
"accountName": "122100 TEST2",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14350538"],
"amount": 49,
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"]
}],
"deposit": []
}]
then this would be what I want the final output to be - all the same id objects have the customerDeposit / deposit combined.
[
{
"id": "755",
"accountID": "755",
"accountName": "122060 Test7",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14394551"],
"amount": 43,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"]
}, {
"entities": ["14394553"],
"amount": 28,
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
}],
"deposit": []
}, {
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14439896"],
"amount": 38,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"]
}, {
"entities": ["14434400"],
"amount": 18,
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"]
}],
"deposit": [{
"entities": ["14430895"],
"amount": 63,
"memo": ["MICGB2452"],
}]
}, {
"id": "762",
"accountID": "762",
"accountName": "122100 TEST1",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [
{
"entities": ["14350538"],
"amount": 45,
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
}
],
"deposit": []
}, {
"id": "760",
"accountID": "760",
"accountName": "122100 TEST2",
"subsidiary": "2",
"currency": "1",
"type": "CustDep",
"exchangerate": 1,
"customerDeposit": [{
"entities": ["14350538"],
"amount": 49,
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"]
}],
"deposit": []
}]
This is my current code but it is not working as i want as it skips any ids that dont have more than one customerDeposit / deposit as it doesn't get into the if (objForId) statement.
function createDeposit(values) {
deposit = {
entities: values.entities,
amount: values.amount,
memo: values.memo,
};
return deposit;
}
function createCustomerDeposit(values) {
customerDeposit = {
entities: values.entities,
amount: values.amount,
memo: values.memo,
webPaymentMethod: values.webPaymentMethod,
webPaymentMethodID: values.webPaymentMethodID,
salesOrderId: values.salesOrderId,
};
return customerDeposit;
}
function run() {
const transferData = [{
"id": "755",
"entities": ["14394551"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 43,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394550"],
"memo": ["MAINGB588328"],
"exchangerate": 1
}, {
"id": "755",
"entities": ["14394553"],
"accountID": "755",
"accountName": "122060 Test7",
"amount": 28,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test7",
"webPaymentMethodID": "14",
"type": "CustDep",
"salesOrderId": ["14394552"],
"memo": ["MAINGB588333"],
"exchangerate": 1
},{
"id": "758",
"entities": ["14439896"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 38,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14439895"],
"memo": ["MICGB2454"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14434400"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 18,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "CustDep",
"salesOrderId": ["14434399"],
"memo": ["MICGB2453"],
"exchangerate": 1
}, {
"id": "758",
"entities": ["14430895"],
"accountID": "758",
"accountName": "122070 Test5",
"amount": 63,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "Test5",
"webPaymentMethodID": "12",
"type": "Dep",
"salesOrderId": ["14430894"],
"memo": ["MICGB2452"],
"exchangerate": 1
}, {
"id": "762",
"entities": ["14350538"],
"accountID": "762",
"accountName": "122100 TEST1",
"amount": 45,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST1",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14350537"],
"memo": ["MAINGB586991"],
"exchangerate": 1
}, {
"id": "760",
"entities": ["14350538"],
"accountID": "760",
"accountName": "122100 TEST2",
"amount": 49,
"subsidiary": "2",
"currency": "1",
"webPaymentMethod": "TEST2",
"webPaymentMethodID": "13",
"type": "CustDep",
"salesOrderId": ["14321538"],
"memo": ["MAINGB452756"],
"exchangerate": 1
}];
var customerDeposit = [];
var deposit = [];
var res = transferData.reduce(function (agg, obj) {
var objForId = agg.filter(function (idObj) {
return idObj.id === obj.id;
})[0];
if (objForId) {
if(obj.type === 'CustDep') {
objForId.customerDeposit.push(createCustomerDeposit(obj));
}
if(obj.type === 'Dep') {
objForId.deposit.push(createDeposit(obj));
}
} else {
agg.push({
id: obj.id,
accountID: obj.accountID,
accountName: obj.accountName,
subsidiary: obj.subsidiary,
currency: obj.currency,
exchangerate: obj.exchangerate,
customerDeposit,
deposit
});
customerDeposit = [];
deposit = [];
}
return agg;
}, []);
console.log(res);
}
run();
Any help would be really appreciated. Hopefully the question makes sense.
Thanks

Filter Nested array of objects in angular java script

Hi I am Having following array. I want to filter this array depending on user input.I have search it on internet gone through many solutions on stack overflow i have also gone through documentation of array but i didn't get any solution. Please Help...
{
"data": [
{
"categoryId": "1",
"categoryName": "Pens",
"subcat": [
{
"SubCatId": "1",
"SubCategoryName": "Classic Cakes",
"item": [
{
"DisplayName": "Excellent 500gms",
"ItemData": {
"ItemName": "Excellent"
}
},
{
"DisplayName": "choco vanila 500gms",
"ItemData": {
"Id": "26",
"ItemName": "choco vanila "
}
}
]
},
{
"SubCatId": "2",
"SubCategoryName": "Classic Cakes2",
"item": [
{
"DisplayName": "xyz 500gms",
"ItemData": {
"ItemName": "xyz"
}
},
{
"DisplayName": "abc 500gms",
"ItemData": {
"Id": "26",
"ItemName": "abc vanila "
}
}
]
}
]
},
{
"categoryId": "2",
"categoryName": "Markers",
"subcat": [
{
"SubCatId": "2",
"SubCategoryName": "Premium Cakes I",
"item": [
{
"DisplayName": "choco caramel 500gms",
"ItemData": {
"Id": "65",
"ItemName": "choco caramel"
}
},
{
"DisplayName": "choco almond 500gms",
"ItemData": {
"Id": "52",
"ItemName": "choco almond "
}
}
]
}
]
}
]
}
I want to apply filter on 'categoryName' , 'SubCategoryName' , 'DisplayName' , 'ItemName'
for example if I search 'choco almond' (which is 'ItemName') then the resultant array should be like
{
"data": [
{
"categoryId": "2",
"categoryName": "Markers",
"subcat": [
{
"SubCatId": "2",
"SubCategoryName": "Premium Cakes I",
"item": [
{
"DisplayName": "choco almond 500gms",
"ItemData": {
"Id": "52",
"ItemName": "choco almond "
}
}
]
}
]
}
]
}
if i search 'Pens' (which is 'categoryName') then thw resultant array should be like
{
"data": [
{
"categoryId": "1",
"categoryName": "Pens",
"subcat": [
{
"SubCatId": "1",
"SubCategoryName": "Classic Cakes",
"item": [
{
"DisplayName": "Excellent 500gms",
"ItemData": {
"ItemName": "Excellent"
}
},
{
"DisplayName": "choco vanila 500gms",
"ItemData": {
"Id": "26",
"ItemName": "choco vanila "
}
}
]
},
{
"SubCatId": "2",
"SubCategoryName": "Classic Cakes2",
"item": [
{
"DisplayName": "xyz 500gms",
"ItemData": {
"ItemName": "xyz"
}
},
{
"DisplayName": "abc 500gms",
"ItemData": {
"Id": "26",
"ItemName": "choco vanila "
}
}
]
}
]
}
]
}
What you can do is do a filter inside a filter.
var filter = "Pens";
var result = d["data"].filter(c=>
c.categoryName==filter ||
c.subcat.filter(s => s.SubCategoryName == filter).length > 0 ||
c.subcat.filter(i => i.item.filter(it => it.ItemData.ItemName == filter).length > 0).length > 0
);
console.log(result)
Where variable "d" is the json/object.

Unable to get array from array in Javascript

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 ?

Kendo UI HierarchicalDataSource for Treeview with dynamic schema

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": [
]
}
]

Categories

Resources