Nested Array Tree Node JS - javascript

I am comparing 2 arrays with the ID's and adding nested children objects to generate the tree.
if you look in the code comparing issue_id with descendents_parent_issue_id
I am able to generate the tree but a children is adding only 1 object. Expecting tree level nested objects.
Following Code Output
[{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"hierarchy_level": 1,
"children": [{
"issue_id": 7,
"parent_issue_id": 2,
"issue": "Breakdown",
"issue_code": "bd7catA",
"entity_id": null,
"hierarchy_level": 2,
"children": [{
"issue_id": 881,
"parent_issue_id": 7,
"issue": "Hydraulic arm failure",
"issue_code": null,
"entity_id": 52,
"hierarchy_level": 3
}]
}]
}, {
"issue_id": 1,
"parent_issue_id": null,
"issue": "Loading",
"issue_code": null,
"hierarchy_level": 1
}, {
"issue_id": 3,
"parent_issue_id": null,
"issue": "Performance",
"issue_code": null,
"hierarchy_level": 1
}, {
"issue_id": 4,
"parent_issue_id": null,
"issue": "Quality",
"issue_code": null,
"hierarchy_level": 1
}]
My Code
const issuesList = [
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 1,
"parent_issue_id": null,
"issue": "Loading",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 3,
"parent_issue_id": null,
"issue": "Performance",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 4,
"parent_issue_id": null,
"issue": "Quality",
"issue_code": null,
"hierarchy_level": 1
}
];
const issues = [{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 7,
"descendents_parent_issue_id": 2,
"descendents_issue": "Breakdown",
"descendents_issue_code": "bd7catA",
"descendents_entity_id": null,
"descendents_hierarchy_level": 2,
"descendents_issuesancestor_issues_issue_id": 7,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 881,
"descendents_parent_issue_id": 7,
"descendents_issue": "Hydraulic arm failure",
"descendents_issue_code": null,
"descendents_entity_id": 52,
"descendents_hierarchy_level": 3,
"descendents_issuesancestor_issues_issue_id": 881,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 8,
"descendents_parent_issue_id": 2,
"descendents_issue": "Setup/ Changeover",
"descendents_issue_code": "st8catA",
"descendents_entity_id": null,
"descendents_hierarchy_level": 2,
"descendents_issuesancestor_issues_issue_id": 8,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 942,
"descendents_parent_issue_id": 7,
"descendents_issue": "Pump Overload",
"descendents_issue_code": null,
"descendents_entity_id": 52,
"descendents_hierarchy_level": 3,
"descendents_issuesancestor_issues_issue_id": 942,
"descendents_issuesancestor_ancestor_issue_id": 2
}
];
function processIssues(arr, arrayB) {
return arr.reduce((result, item) => {
const itemInB = arrayB.find(itemB => itemB.descendents_parent_issue_id == item.issue_id)
if (itemInB) {
let child = [];
child.push({
issue_id: itemInB.descendents_issue_id,
parent_issue_id: itemInB.descendents_parent_issue_id,
issue: itemInB.descendents_issue,
issue_code: itemInB.descendents_issue_code,
entity_id: itemInB.descendents_entity_id,
hierarchy_level: itemInB.descendents_hierarchy_level
});
item.children = child;
}
if (item.children) {
processIssues(item.children, arrayB)
}
return [...result, item];
}, []);
}
var sourceIssue = processIssues(issuesList, issues);
console.log(JSON.stringify(sourceIssue));
Expected output
[{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"hierarchy_level": 1,
"children": [{
"issue_id": 7,
"parent_issue_id": 2,
"issue": "Breakdown",
"issue_code": "bd7catA",
"entity_id": null,
"hierarchy_level": 2,
"children": [{
"issue_id": 881,
"parent_issue_id": 7,
"issue": "Hydraulic arm failure",
"issue_code": null,
"entity_id": 52,
"hierarchy_level": 3
},{
"issue_id": 942,
"parent_issue_id": 7,
"issue": "Pump Overload",
"issue_code": null,
"entity_id": 52,
"hierarchy_level": 3
}]
}]
}, {
"issue_id": 1,
"parent_issue_id": null,
"issue": "Loading",
"issue_code": null,
"hierarchy_level": 1
}, {
"issue_id": 3,
"parent_issue_id": null,
"issue": "Performance",
"issue_code": null,
"hierarchy_level": 1
}, {
"issue_id": 4,
"parent_issue_id": null,
"issue": "Quality",
"issue_code": null,
"hierarchy_level": 1
}]
Please help

const issuesList = [
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 1,
"parent_issue_id": null,
"issue": "Loading",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 3,
"parent_issue_id": null,
"issue": "Performance",
"issue_code": null,
"hierarchy_level": 1
},
{
"issue_id": 4,
"parent_issue_id": null,
"issue": "Quality",
"issue_code": null,
"hierarchy_level": 1
}
];
const issues = [{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 7,
"descendents_parent_issue_id": 2,
"descendents_issue": "Breakdown",
"descendents_issue_code": "bd7catA",
"descendents_entity_id": null,
"descendents_hierarchy_level": 2,
"descendents_issuesancestor_issues_issue_id": 7,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 881,
"descendents_parent_issue_id": 7,
"descendents_issue": "Hydraulic arm failure",
"descendents_issue_code": null,
"descendents_entity_id": 52,
"descendents_hierarchy_level": 3,
"descendents_issuesancestor_issues_issue_id": 881,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 8,
"descendents_parent_issue_id": 2,
"descendents_issue": "Setup/ Changeover",
"descendents_issue_code": "st8catA",
"descendents_entity_id": null,
"descendents_hierarchy_level": 2,
"descendents_issuesancestor_issues_issue_id": 8,
"descendents_issuesancestor_ancestor_issue_id": 2
},
{
"issue_id": 2,
"parent_issue_id": null,
"issue": "Availability",
"issue_code": null,
"entity_id": null,
"hierarchy_level": 1,
"descendents_issue_id": 942,
"descendents_parent_issue_id": 7,
"descendents_issue": "Pump Overload",
"descendents_issue_code": null,
"descendents_entity_id": 52,
"descendents_hierarchy_level": 3,
"descendents_issuesancestor_issues_issue_id": 942,
"descendents_issuesancestor_ancestor_issue_id": 2
}
];
function processIssues(arr, arrayB) {
return arr.reduce((result, item) => {
const itemInB = arrayB.find(itemB => itemB.descendents_parent_issue_id == item.issue_id)
if (itemInB) {
let child = [];
child.push({
issue_id: itemInB.descendents_issue_id,
parent_issue_id: itemInB.descendents_parent_issue_id,
issue: itemInB.descendents_issue,
issue_code: itemInB.descendents_issue_code,
entity_id: itemInB.descendents_entity_id,
hierarchy_level: itemInB.descendents_hierarchy_level
});
item.children = processIssues(child, arrayB);
}
return [...result, item];
}, []);
}
var sourceIssue = processIssues(issuesList, issues);
console.log(JSON.stringify(sourceIssue));

Related

JavaScript Recursive Search on An Array Of Objects, and getting a new array of objects

I have 2 big arrays. when I send them to function, with require labels, i would like to create an object with existing label with all values added as a array. it it possible?
if so, how to achieve it? i tried by no result.
here is my try:
// Add your code here
const data= [
{
"ProgramSubjectGroupAssocId": 0,
"SubjectSubGroupId": 3,
"SubjectId": 4,
"SubjectSrcId": null,
"ProgramSubjectGroupAssoc": null,
"Subject": {
"SubjectSrcId": "4",
"SubjectName": "Literature",
"AlternateIdentifier": "4.0",
"IsActive": true,
"ProgrammeId": 0,
"SubjectGroupId": null,
"SubjectSubGroupId": null,
"TranslatedName": null,
"TranslatedShortCode": null,
"LanguageCode": null,
"SubjectGroup": null,
"SubjectSubGroup": null,
"SubjectCurriculum": [
{
"VersionNumber": 3,
"VersionStartDate": "2021-03-01T00:00:00+00:00",
"VersionEndDate": "2036-12-01T00:00:00+00:00",
"FirstTeachingDate": "2023-06-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2024-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 0,
"GradingSystemId": 123,
"SubjectId": 4,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [
{
"AlternateCourseId": 3,
"SubjectCurriculumId": 383,
"SubjectLevelId": 50,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 543,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 512,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 543,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
{
"AlternateCourseId": 1,
"SubjectCurriculumId": 383,
"SubjectLevelId": 51,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 544,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 513,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 544,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 383,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:57.087+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
}
],
"SubjectOption": [],
"Id": 4,
"CreatedBy": 1,
"CreatedDate": "2020-02-13T08:46:03.37+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"SubjectSubGroup": {
"SubGroupSrcId": "3",
"ProgrammeId": 0,
"SubGroupNumber": 1,
"SubGroupName": "Languages",
"TranslatedName": null,
"IsActive": true,
"TranslatedShortCode": null,
"LanguageCode": null,
"AlternateIdentifier": 1,
"SubjectGroupId": 3,
"SubjectGroup": null,
"Subject": null,
"GroupSubGroupSubjectAssoc": [
{
"ProgramSubjectGroupAssocId": 0,
"SubjectSubGroupId": 3,
"SubjectId": 4,
"SubjectSrcId": null,
"ProgramSubjectGroupAssoc": null,
"Subject": {
"SubjectSrcId": "4",
"SubjectName": "Literature",
"AlternateIdentifier": "4.0",
"IsActive": true,
"ProgrammeId": 0,
"SubjectGroupId": null,
"SubjectSubGroupId": null,
"TranslatedName": null,
"TranslatedShortCode": null,
"LanguageCode": null,
"SubjectGroup": null,
"SubjectSubGroup": null,
"SubjectCurriculum": [
{
"VersionNumber": 3,
"VersionStartDate": "2021-03-01T00:00:00+00:00",
"VersionEndDate": "2036-12-01T00:00:00+00:00",
"FirstTeachingDate": "2023-06-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2024-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 0,
"GradingSystemId": 123,
"SubjectId": 4,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [
{
"AlternateCourseId": 3,
"SubjectCurriculumId": 383,
"SubjectLevelId": 50,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 543,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 512,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 543,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
{
"AlternateCourseId": 1,
"SubjectCurriculumId": 383,
"SubjectLevelId": 51,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 544,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 513,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 544,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 383,
"CreatedBy": 1,
"CreatedDate": "2020-03-03T09:59:57.087+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-03T09:59:56+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
}
],
"SubjectOption": [],
"Id": 4,
"CreatedBy": 1,
"CreatedDate": "2020-02-13T08:46:03.37+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"Id": 6,
"CreatedBy": 12345,
"CreatedDate": "2020-02-18T09:43:56.823+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 3,
"CreatedBy": 1,
"CreatedDate": "2020-02-13T08:53:21.85+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"Id": 6,
"CreatedBy": 12345,
"CreatedDate": "2020-02-18T09:43:56.823+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
{
"ProgramSubjectGroupAssocId": 0,
"SubjectSubGroupId": 58,
"SubjectId": 153,
"SubjectSrcId": null,
"ProgramSubjectGroupAssoc": null,
"Subject": {
"SubjectSrcId": "21",
"SubjectName": "Bio 14",
"AlternateIdentifier": "8.0",
"IsActive": true,
"ProgrammeId": 0,
"SubjectGroupId": null,
"SubjectSubGroupId": null,
"TranslatedName": null,
"TranslatedShortCode": null,
"LanguageCode": null,
"SubjectGroup": null,
"SubjectSubGroup": null,
"SubjectCurriculum": [
{
"VersionNumber": 7,
"VersionStartDate": "2020-09-01T00:00:00+00:00",
"VersionEndDate": "2036-09-01T00:00:00+00:00",
"FirstTeachingDate": "2023-02-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2021-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 1,
"GradingSystemId": 123,
"SubjectId": 153,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 357,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:23:58.38+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-02T12:23:57+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
},
{
"VersionNumber": 9,
"VersionStartDate": "2020-09-01T00:00:00+00:00",
"VersionEndDate": "2036-09-01T00:00:00+00:00",
"FirstTeachingDate": "2023-02-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2021-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 0,
"GradingSystemId": 123,
"SubjectId": 153,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [
{
"AlternateCourseId": 3,
"SubjectCurriculumId": 359,
"SubjectLevelId": 50,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 506,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 464,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 506,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 359,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32.96+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
}
],
"SubjectOption": [],
"Id": 153,
"CreatedBy": 1,
"CreatedDate": "2020-02-25T10:43:32.79+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"SubjectSubGroup": {
"SubGroupSrcId": "44",
"ProgrammeId": 0,
"SubGroupNumber": 9,
"SubGroupName": "Languageandarts12",
"TranslatedName": null,
"IsActive": true,
"TranslatedShortCode": null,
"LanguageCode": null,
"AlternateIdentifier": 9,
"SubjectGroupId": 79,
"SubjectGroup": null,
"Subject": null,
"GroupSubGroupSubjectAssoc": [
{
"ProgramSubjectGroupAssocId": 0,
"SubjectSubGroupId": 58,
"SubjectId": 153,
"SubjectSrcId": null,
"ProgramSubjectGroupAssoc": null,
"Subject": {
"SubjectSrcId": "21",
"SubjectName": "Bio 14",
"AlternateIdentifier": "8.0",
"IsActive": true,
"ProgrammeId": 0,
"SubjectGroupId": null,
"SubjectSubGroupId": null,
"TranslatedName": null,
"TranslatedShortCode": null,
"LanguageCode": null,
"SubjectGroup": null,
"SubjectSubGroup": null,
"SubjectCurriculum": [
{
"VersionNumber": 7,
"VersionStartDate": "2020-09-01T00:00:00+00:00",
"VersionEndDate": "2036-09-01T00:00:00+00:00",
"FirstTeachingDate": "2023-02-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2021-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 1,
"GradingSystemId": 123,
"SubjectId": 153,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 357,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:23:58.38+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-02T12:23:57+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
},
{
"VersionNumber": 9,
"VersionStartDate": "2020-09-01T00:00:00+00:00",
"VersionEndDate": "2036-09-01T00:00:00+00:00",
"FirstTeachingDate": "2023-02-01T00:00:00+00:00",
"FirstAssessmentDate": "2023-06-01T00:00:00+00:00",
"LastAssessmentDate": "2021-06-01T00:00:00+00:00",
"GradePredicted": true,
"CurriculumTypeId": 0,
"GradingSystemId": 123,
"SubjectId": 153,
"ProgrammeId": 0,
"SubjectGroupId": 0,
"SubjectSubGroupId": null,
"GradingSystem": null,
"Course": [
{
"AlternateCourseId": 3,
"SubjectCurriculumId": 359,
"SubjectLevelId": 50,
"SubjectOptionId": null,
"IsActive": true,
"SubjectLevel": null,
"SubjectOption": null,
"CourseComponentAssoc": [],
"CourseResponseLanguageAssoc": [
{
"CourseId": 506,
"IblanguageId": 1,
"Iblanguage": {
"LanguageCode": "en-GB",
"LanguageCode2": "1",
"LanguageName": "English",
"IsActive": true,
"IblanguageTypeAssoc": [],
"Id": 1,
"CreatedBy": 1,
"CreatedDate": "2019-10-14T00:00:00+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"languageName": null,
"Id": 464,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 506,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"CourseTypeId": 0,
"CourseName": null,
"GradingName": null,
"Id": 359,
"CreatedBy": 1,
"CreatedDate": "2020-03-02T12:25:32.96+00:00",
"UpdatedBy": 1,
"UpdatedDate": "2020-03-02T12:25:32+00:00",
"UpdatedByName": "User, 01",
"CreatedByName": "User, 01"
}
],
"SubjectOption": [],
"Id": 153,
"CreatedBy": 1,
"CreatedDate": "2020-02-25T10:43:32.79+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"Id": 32,
"CreatedBy": 1,
"CreatedDate": "2020-02-25T10:52:26.11+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
],
"Id": 58,
"CreatedBy": 1,
"CreatedDate": "2020-02-26T16:48:26.443+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
},
"Id": 32,
"CreatedBy": 1,
"CreatedDate": "2020-02-25T10:52:26.11+00:00",
"UpdatedBy": null,
"UpdatedDate": null,
"UpdatedByName": null,
"CreatedByName": "User, 01"
}
]
function findId(ids, arr) {
return arr.reduce((a, item) => {
for(let [key, value] of Object.entries(item)){
if (a) return a;
if (key === id) return [key, value];
if (Array.isArray(item)) {
console.log(item);
}
}
}, {});
}
data.forEach(data => console.log(findId('IsActive, CreatedBy, CreatedDate ', data)));
expected result:
{
IsActive: [{IsActive:true},...all instance from the data],
CreatedBy: [{CreatedBy:1},...all instance from the data],
CreatedDate: [{CreatedDate:"2020-02-26T16:48:26.443+00:00"}... all instance]
}
I am not able to proceed with further. any one help me?
As I told, I don't not understand the purpose. But this could be your solution. I would suggest just use regex match faster. Stringify and regex match.
function collectAll(key, object, values = []) {
if (object && typeof object === "object") {
Object.entries(object).forEach(([x, value]) => {
if (key == x) {
values.push({ [key]: value });
} else {
collectAll(key, value, values);
}
});
} else {
if (key == object) {
values.push({ [key]: object });
}
}
return values;
}
function findId(ids, data) {
let obj = {}
ids.forEach(key => {
obj[key] = collectAll(key, data)
})
return obj
}
console.log(findId(["IsActive", "CreatedBy", "CreatedDate"], data))
function collectAll(key, object, values = []) {
if (object && typeof object === "object") {
Object.entries(object).forEach(([x, value]) => {
if (key == x) {
values.push({ [key]: value });
} else {
collectAll(key, value, values);
}
});
} else {
if (key == object) {
values.push({ [key]: object });
}
}
return values;
}
function findId(ids, data) {
let obj = {}
ids.forEach(key => {
obj[key] = collectAll(key, data)
})
return obj
}
const data = [{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":3,"SubjectId":4,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"4","SubjectName":"Literature","AlternateIdentifier":"4.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":3,"VersionStartDate":"2021-03-01T00:00:00+00:00","VersionEndDate":"2036-12-01T00:00:00+00:00","FirstTeachingDate":"2023-06-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2024-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":4,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":383,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":543,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":512,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":543,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"AlternateCourseId":1,"SubjectCurriculumId":383,"SubjectLevelId":51,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":544,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":513,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":544,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":383,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:57.087+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-03T09:59:56+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":4,"CreatedBy":1,"CreatedDate":"2020-02-13T08:46:03.37+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"SubjectSubGroup":{"SubGroupSrcId":"3","ProgrammeId":0,"SubGroupNumber":1,"SubGroupName":"Languages","TranslatedName":null,"IsActive":true,"TranslatedShortCode":null,"LanguageCode":null,"AlternateIdentifier":1,"SubjectGroupId":3,"SubjectGroup":null,"Subject":null,"GroupSubGroupSubjectAssoc":[{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":3,"SubjectId":4,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"4","SubjectName":"Literature","AlternateIdentifier":"4.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":3,"VersionStartDate":"2021-03-01T00:00:00+00:00","VersionEndDate":"2036-12-01T00:00:00+00:00","FirstTeachingDate":"2023-06-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2024-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":4,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":383,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":543,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":512,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":543,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"AlternateCourseId":1,"SubjectCurriculumId":383,"SubjectLevelId":51,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":544,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":513,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":544,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":383,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:57.087+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-03T09:59:56+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":4,"CreatedBy":1,"CreatedDate":"2020-02-13T08:46:03.37+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":6,"CreatedBy":12345,"CreatedDate":"2020-02-18T09:43:56.823+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":3,"CreatedBy":1,"CreatedDate":"2020-02-13T08:53:21.85+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":6,"CreatedBy":12345,"CreatedDate":"2020-02-18T09:43:56.823+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":58,"SubjectId":153,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"21","SubjectName":"Bio 14","AlternateIdentifier":"8.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":7,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":1,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":357,"CreatedBy":1,"CreatedDate":"2020-03-02T12:23:58.38+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:23:57+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"},{"VersionNumber":9,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":359,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":506,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":464,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":506,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":359,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32.96+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:25:32+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":153,"CreatedBy":1,"CreatedDate":"2020-02-25T10:43:32.79+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"SubjectSubGroup":{"SubGroupSrcId":"44","ProgrammeId":0,"SubGroupNumber":9,"SubGroupName":"Languageandarts12","TranslatedName":null,"IsActive":true,"TranslatedShortCode":null,"LanguageCode":null,"AlternateIdentifier":9,"SubjectGroupId":79,"SubjectGroup":null,"Subject":null,"GroupSubGroupSubjectAssoc":[{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":58,"SubjectId":153,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"21","SubjectName":"Bio 14","AlternateIdentifier":"8.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":7,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":1,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":357,"CreatedBy":1,"CreatedDate":"2020-03-02T12:23:58.38+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:23:57+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"},{"VersionNumber":9,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":359,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":506,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":464,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":506,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":359,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32.96+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:25:32+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":153,"CreatedBy":1,"CreatedDate":"2020-02-25T10:43:32.79+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":32,"CreatedBy":1,"CreatedDate":"2020-02-25T10:52:26.11+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":58,"CreatedBy":1,"CreatedDate":"2020-02-26T16:48:26.443+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":32,"CreatedBy":1,"CreatedDate":"2020-02-25T10:52:26.11+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}]
console.log(findId(["IsActive", "CreatedBy", "CreatedDate"], data))
Single Loop Implementation:
function collectAll(keys, object, values = {}) {
if (object && typeof object === "object") {
Object.entries(object).forEach(([x, value]) => {
if (keys.indexOf(x) !== -1) {
values[x].push({ [x]: value });
} else {
collectAll(keys, value, values);
}
});
}
return values;
}
function findId(ids, data) {
let obj = {}
ids.forEach(key => {
obj[key] = []
})
collectAll(ids, data, obj)
return obj
}
const data = [{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":3,"SubjectId":4,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"4","SubjectName":"Literature","AlternateIdentifier":"4.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":3,"VersionStartDate":"2021-03-01T00:00:00+00:00","VersionEndDate":"2036-12-01T00:00:00+00:00","FirstTeachingDate":"2023-06-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2024-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":4,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":383,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":false,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":543,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":512,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":543,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"AlternateCourseId":1,"SubjectCurriculumId":383,"SubjectLevelId":51,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":544,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":513,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":544,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":383,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:57.087+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-03T09:59:56+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":4,"CreatedBy":1,"CreatedDate":"2020-02-13T08:46:03.37+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"SubjectSubGroup":{"SubGroupSrcId":"3","ProgrammeId":0,"SubGroupNumber":1,"SubGroupName":"Languages","TranslatedName":null,"IsActive":true,"TranslatedShortCode":null,"LanguageCode":null,"AlternateIdentifier":1,"SubjectGroupId":3,"SubjectGroup":null,"Subject":null,"GroupSubGroupSubjectAssoc":[{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":3,"SubjectId":4,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"4","SubjectName":"Literature","AlternateIdentifier":"4.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":3,"VersionStartDate":"2021-03-01T00:00:00+00:00","VersionEndDate":"2036-12-01T00:00:00+00:00","FirstTeachingDate":"2023-06-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2024-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":4,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":383,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":543,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":512,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":543,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"AlternateCourseId":1,"SubjectCurriculumId":383,"SubjectLevelId":51,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":544,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":513,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":544,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:56+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":383,"CreatedBy":1,"CreatedDate":"2020-03-03T09:59:57.087+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-03T09:59:56+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":4,"CreatedBy":1,"CreatedDate":"2020-02-13T08:46:03.37+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":6,"CreatedBy":12345,"CreatedDate":"2020-02-18T09:43:56.823+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":3,"CreatedBy":1,"CreatedDate":"2020-02-13T08:53:21.85+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":6,"CreatedBy":12345,"CreatedDate":"2020-02-18T09:43:56.823+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":58,"SubjectId":153,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"21","SubjectName":"Bio 14","AlternateIdentifier":"8.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":7,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":1,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":357,"CreatedBy":1,"CreatedDate":"2020-03-02T12:23:58.38+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:23:57+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"},{"VersionNumber":9,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":359,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":506,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":464,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":506,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":359,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32.96+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:25:32+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":153,"CreatedBy":1,"CreatedDate":"2020-02-25T10:43:32.79+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"SubjectSubGroup":{"SubGroupSrcId":"44","ProgrammeId":0,"SubGroupNumber":9,"SubGroupName":"Languageandarts12","TranslatedName":null,"IsActive":true,"TranslatedShortCode":null,"LanguageCode":null,"AlternateIdentifier":9,"SubjectGroupId":79,"SubjectGroup":null,"Subject":null,"GroupSubGroupSubjectAssoc":[{"ProgramSubjectGroupAssocId":0,"SubjectSubGroupId":58,"SubjectId":153,"SubjectSrcId":null,"ProgramSubjectGroupAssoc":null,"Subject":{"SubjectSrcId":"21","SubjectName":"Bio 14","AlternateIdentifier":"8.0","IsActive":true,"ProgrammeId":0,"SubjectGroupId":null,"SubjectSubGroupId":null,"TranslatedName":null,"TranslatedShortCode":null,"LanguageCode":null,"SubjectGroup":null,"SubjectSubGroup":null,"SubjectCurriculum":[{"VersionNumber":7,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":1,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":357,"CreatedBy":1,"CreatedDate":"2020-03-02T12:23:58.38+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:23:57+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"},{"VersionNumber":9,"VersionStartDate":"2020-09-01T00:00:00+00:00","VersionEndDate":"2036-09-01T00:00:00+00:00","FirstTeachingDate":"2023-02-01T00:00:00+00:00","FirstAssessmentDate":"2023-06-01T00:00:00+00:00","LastAssessmentDate":"2021-06-01T00:00:00+00:00","GradePredicted":true,"CurriculumTypeId":0,"GradingSystemId":123,"SubjectId":153,"ProgrammeId":0,"SubjectGroupId":0,"SubjectSubGroupId":null,"GradingSystem":null,"Course":[{"AlternateCourseId":3,"SubjectCurriculumId":359,"SubjectLevelId":50,"SubjectOptionId":null,"IsActive":true,"SubjectLevel":null,"SubjectOption":null,"CourseComponentAssoc":[],"CourseResponseLanguageAssoc":[{"CourseId":506,"IblanguageId":1,"Iblanguage":{"LanguageCode":"en-GB","LanguageCode2":"1","LanguageName":"English","IsActive":true,"IblanguageTypeAssoc":[],"Id":1,"CreatedBy":1,"CreatedDate":"2019-10-14T00:00:00+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"languageName":null,"Id":464,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":506,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"CourseTypeId":0,"CourseName":null,"GradingName":null,"Id":359,"CreatedBy":1,"CreatedDate":"2020-03-02T12:25:32.96+00:00","UpdatedBy":1,"UpdatedDate":"2020-03-02T12:25:32+00:00","UpdatedByName":"User, 01","CreatedByName":"User, 01"}],"SubjectOption":[],"Id":153,"CreatedBy":1,"CreatedDate":"2020-02-25T10:43:32.79+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":32,"CreatedBy":1,"CreatedDate":"2020-02-25T10:52:26.11+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}],"Id":58,"CreatedBy":1,"CreatedDate":"2020-02-26T16:48:26.443+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"},"Id":32,"CreatedBy":1,"CreatedDate":"2020-02-25T10:52:26.11+00:00","UpdatedBy":null,"UpdatedDate":null,"UpdatedByName":null,"CreatedByName":"User, 01"}]
const result = findId(["IsActive", "CreatedBy", "CreatedDate"], data)
console.log(result)

Angular 4 switching from hard coded data for cascading dropdowns to getting a complex object from web api call

On a new project with .net core and Angular 4 - I had to wait till a real database was in place.
Now that it IS there a DBA , along with a backend developer ended up a web api that I need to call and fetch a nested object with data that I had much simpler in arrays.
Current Data calls
Here is where I call a method to retrieve all the categories
getCategory() {
return [
new Category(1, 1, 'VAMC-Cat-1'),
new Category(2, 1, 'VAMC-Cat-2'),
new Category(3, 1, 'VAMC-Cat-3'),
new Category(4, 2, 'Vet-Cat-1'),
new Category(5, 2, 'Vet-Cat-2'),
new Category(6, 2, 'Vet-Cat-3'),
new Category(7, 3, 'Provider-Cat-1'),
new Category(8, 3, 'Provider-Cat-2'),
new Category(9, 3, 'Provider-Cat-3'),
new Category(10, 4, 'Other-Cat-1'),
new Category(11, 4, 'Other-Cat-2'),
new Category(12, 4, 'Other-Cat-3'),
new Category(13, 5, 'None-Cat-1'),
new Category(14, 5, 'None-Cat-3'),
new Category(15, 5, 'None-Cat-2'),
];
}
If you scroll down you can see that my single call for 3 cascading dropdowns is all in 1 big complex object. HOW can I make calls to this data that I need to persist in Angular4/typescript . Any ideas / examples ?
New Data that will return (swagger ui ) I used to call the web api
[
{
"customerTypeId": 1,
"customerTypeName": "VAMC",
"childCategories": [
{
"categoryId": 1,
"categoryName": "VAMC-Cat-1",
"customerTypeID": 1,
"childSubCategories": [
{
"subCategoryId": 1,
"subCategoryName": "VAMC-SubCat-1-1",
"categoryID": 1
},
{
"subCategoryId": 2,
"subCategoryName": "VAMC-SubCat-1-2",
"categoryID": 1
}
]
},
{
"categoryId": 2,
"categoryName": "VAMC-Cat-2",
"customerTypeID": 1,
"childSubCategories": [
{
"subCategoryId": 3,
"subCategoryName": "VAMC-SubCat-2-1",
"categoryID": 2
},
{
"subCategoryId": 4,
"subCategoryName": "VAMC-SubCat-2-2",
"categoryID": 2
}
]
}
]
},
{
"customerTypeId": 2,
"customerTypeName": "Vet",
"childCategories": [
{
"categoryId": 3,
"categoryName": "Vet-Cat-1",
"customerTypeID": 2,
"childSubCategories": [
{
"subCategoryId": 5,
"subCategoryName": "Vet-SubCat-1-1",
"categoryID": 3
},
{
"subCategoryId": 6,
"subCategoryName": "Vet-SubCat-1-2",
"categoryID": 3
}
]
},
{
"categoryId": 4,
"categoryName": "Vet-Cat-2",
"customerTypeID": 2,
"childSubCategories": [
{
"subCategoryId": 7,
"subCategoryName": "Vet-SubCat-2-1",
"categoryID": 4
},
{
"subCategoryId": 8,
"subCategoryName": "Vet-SubCat-2-2",
"categoryID": 4
}
]
}
]
},
{
"customerTypeId": 3,
"customerTypeName": "Provider",
"childCategories": [
{
"categoryId": 5,
"categoryName": "Provider-Cat-1",
"customerTypeID": 3,
"childSubCategories": [
{
"subCategoryId": 9,
"subCategoryName": "Provider-SubCat-1-1",
"categoryID": 5
},
{
"subCategoryId": 10,
"subCategoryName": "Provider-SubCat-1-2",
"categoryID": 5
}
]
},
{
"categoryId": 6,
"categoryName": "Provider-Cat-2",
"customerTypeID": 3,
"childSubCategories": [
{
"subCategoryId": 11,
"subCategoryName": "Provider-SubCat-2-1",
"categoryID": 6
},
{
"subCategoryId": 12,
"subCategoryName": "Provider-SubCat-2-2",
"categoryID": 6
}
]
}
]
},
{
"customerTypeId": 4,
"customerTypeName": "Other",
"childCategories": [
{
"categoryId": 7,
"categoryName": "Other-Cat-1",
"customerTypeID": 4,
"childSubCategories": [
{
"subCategoryId": 13,
"subCategoryName": "Other-SubCat-1-1",
"categoryID": 7
},
{
"subCategoryId": 14,
"subCategoryName": "Other-SubCat-1-2",
"categoryID": 7
}
]
},
{
"categoryId": 8,
"categoryName": "Other-Cat-2",
"customerTypeID": 4,
"childSubCategories": [
{
"subCategoryId": 15,
"subCategoryName": "Other-SubCat-2-1",
"categoryID": 8
},
{
"subCategoryId": 16,
"subCategoryName": "Other-SubCat-2-2",
"categoryID": 8
}
]
}
]
},
{
"customerTypeId": 5,
"customerTypeName": "None",
"childCategories": [
{
"categoryId": 9,
"categoryName": "None-Cat-1",
"customerTypeID": 5,
"childSubCategories": [
{
"subCategoryId": 17,
"subCategoryName": "None-SubCat-1-1",
"categoryID": 9
},
{
"subCategoryId": 18,
"subCategoryName": "None-SubCat-1-2",
"categoryID": 9
}
]
},
{
"categoryId": 10,
"categoryName": "None-Cat-2",
"customerTypeID": 5,
"childSubCategories": [
{
"subCategoryId": 19,
"subCategoryName": "None-SubCat-2-1",
"categoryID": 10
},
{
"subCategoryId": 20,
"subCategoryName": "None-SubCat-2-2",
"categoryID": 10
}
]
}
]
}
]
You can use Array map(), reduce() in different ways as you prefer. Its nice to look into filter() as well.
There is a usage of destruction in parameters also here.
In Angular 4, I hope you already have a service for this, if not create one. From the service you can call the API, transform the result, return a Promise or Observer that you can resolve / subscribeto from your component.
let result = data.map(item => item["childCategories"])
.reduce((a, item) => a.concat(item), [])
.map(({
categoryId,
categoryName,
customerTypeID
}) => ({
categoryId,
categoryName,
customerTypeID
}));
The last map can be changed to have your class in there:
.map(({
categoryId,
categoryName,
customerTypeID
}) => new Category(
categoryId,
categoryName,
customerTypeID
));
const data = [{
"customerTypeId": 1,
"customerTypeName": "VAMC",
"childCategories": [{
"categoryId": 1,
"categoryName": "VAMC-Cat-1",
"customerTypeID": 1,
"childSubCategories": [{
"subCategoryId": 1,
"subCategoryName": "VAMC-SubCat-1-1",
"categoryID": 1
},
{
"subCategoryId": 2,
"subCategoryName": "VAMC-SubCat-1-2",
"categoryID": 1
}
]
},
{
"categoryId": 2,
"categoryName": "VAMC-Cat-2",
"customerTypeID": 1,
"childSubCategories": [{
"subCategoryId": 3,
"subCategoryName": "VAMC-SubCat-2-1",
"categoryID": 2
},
{
"subCategoryId": 4,
"subCategoryName": "VAMC-SubCat-2-2",
"categoryID": 2
}
]
}
]
},
{
"customerTypeId": 2,
"customerTypeName": "Vet",
"childCategories": [{
"categoryId": 3,
"categoryName": "Vet-Cat-1",
"customerTypeID": 2,
"childSubCategories": [{
"subCategoryId": 5,
"subCategoryName": "Vet-SubCat-1-1",
"categoryID": 3
},
{
"subCategoryId": 6,
"subCategoryName": "Vet-SubCat-1-2",
"categoryID": 3
}
]
},
{
"categoryId": 4,
"categoryName": "Vet-Cat-2",
"customerTypeID": 2,
"childSubCategories": [{
"subCategoryId": 7,
"subCategoryName": "Vet-SubCat-2-1",
"categoryID": 4
},
{
"subCategoryId": 8,
"subCategoryName": "Vet-SubCat-2-2",
"categoryID": 4
}
]
}
]
},
{
"customerTypeId": 3,
"customerTypeName": "Provider",
"childCategories": [{
"categoryId": 5,
"categoryName": "Provider-Cat-1",
"customerTypeID": 3,
"childSubCategories": [{
"subCategoryId": 9,
"subCategoryName": "Provider-SubCat-1-1",
"categoryID": 5
},
{
"subCategoryId": 10,
"subCategoryName": "Provider-SubCat-1-2",
"categoryID": 5
}
]
},
{
"categoryId": 6,
"categoryName": "Provider-Cat-2",
"customerTypeID": 3,
"childSubCategories": [{
"subCategoryId": 11,
"subCategoryName": "Provider-SubCat-2-1",
"categoryID": 6
},
{
"subCategoryId": 12,
"subCategoryName": "Provider-SubCat-2-2",
"categoryID": 6
}
]
}
]
},
{
"customerTypeId": 4,
"customerTypeName": "Other",
"childCategories": [{
"categoryId": 7,
"categoryName": "Other-Cat-1",
"customerTypeID": 4,
"childSubCategories": [{
"subCategoryId": 13,
"subCategoryName": "Other-SubCat-1-1",
"categoryID": 7
},
{
"subCategoryId": 14,
"subCategoryName": "Other-SubCat-1-2",
"categoryID": 7
}
]
},
{
"categoryId": 8,
"categoryName": "Other-Cat-2",
"customerTypeID": 4,
"childSubCategories": [{
"subCategoryId": 15,
"subCategoryName": "Other-SubCat-2-1",
"categoryID": 8
},
{
"subCategoryId": 16,
"subCategoryName": "Other-SubCat-2-2",
"categoryID": 8
}
]
}
]
},
{
"customerTypeId": 5,
"customerTypeName": "None",
"childCategories": [{
"categoryId": 9,
"categoryName": "None-Cat-1",
"customerTypeID": 5,
"childSubCategories": [{
"subCategoryId": 17,
"subCategoryName": "None-SubCat-1-1",
"categoryID": 9
},
{
"subCategoryId": 18,
"subCategoryName": "None-SubCat-1-2",
"categoryID": 9
}
]
},
{
"categoryId": 10,
"categoryName": "None-Cat-2",
"customerTypeID": 5,
"childSubCategories": [{
"subCategoryId": 19,
"subCategoryName": "None-SubCat-2-1",
"categoryID": 10
},
{
"subCategoryId": 20,
"subCategoryName": "None-SubCat-2-2",
"categoryID": 10
}
]
}
]
}
];
let result = data.map(item => item["childCategories"])
.reduce((a, item) => a.concat(item), [])
.map(({
categoryId,
categoryName,
customerTypeID
}) => ({
categoryId,
categoryName,
customerTypeID
}));
console.log(result);

how to create jquery variables from json file?

Any help is very much appreciated.
Basically I am using an api from mashape, but I'm a bit of a newbie to JSON files.
What I want to do is create a load of jquery variables for each teams total points.
Bare in mind that the teams change position in the JSON file depending on their position in the table.
Below is my jquery code so far (without the auth code) and the JSON file.
$.ajax({
url: 'https://heisenbug-premier-league-live-scores-v1.p.mashape.com/api/premierleague/table',
type: 'GET',
data: {},
dataType: 'json',
success: function(data) {
$(data.records).each(function(index, value) {
});
console.dir((data.source));
},
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization",
"Auth Code");
}
});
And the JSON file.
{
"records": [
{
"team": "Manchester City",
"played": 10,
"win": 8,
"draw": 0,
"loss": 2,
"goalsFor": 29,
"goalsAgainst": 12,
"points": 24
},
{
"team": "Arsenal",
"played": 10,
"win": 7,
"draw": 2,
"loss": 1,
"goalsFor": 16,
"goalsAgainst": 6,
"points": 23
},
{
"team": "Tottenham",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 18,
"goalsAgainst": 7,
"points": 19
},
{
"team": "Leicester",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 16,
"goalsAgainst": 13,
"points": 19
},
{
"team": "Manchester United",
"played": 10,
"win": 5,
"draw": 4,
"loss": 1,
"goalsFor": 12,
"goalsAgainst": 4,
"points": 19
},
{
"team": "West Ham",
"played": 10,
"win": 4,
"draw": 4,
"loss": 2,
"goalsFor": 16,
"goalsAgainst": 12,
"points": 16
},
{
"team": "Liverpool",
"played": 9,
"win": 4,
"draw": 3,
"loss": 2,
"goalsFor": 11,
"goalsAgainst": 11,
"points": 15
},
{
"team": "Norwich",
"played": 10,
"win": 4,
"draw": 3,
"loss": 3,
"goalsFor": 12,
"goalsAgainst": 10,
"points": 15
},
{
"team": "Southampton",
"played": 10,
"win": 4,
"draw": 2,
"loss": 4,
"goalsFor": 17,
"goalsAgainst": 13,
"points": 14
},
{
"team": "Chelsea",
"played": 10,
"win": 4,
"draw": 2,
"loss": 4,
"goalsFor": 15,
"goalsAgainst": 14,
"points": 14
},
{
"team": "West Bromwich Albion",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 14,
"goalsAgainst": 17,
"points": 14
},
{
"team": "Crystal Palace",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 12,
"goalsAgainst": 12,
"points": 14
},
{
"team": "Watford",
"played": 11,
"win": 4,
"draw": 2,
"loss": 5,
"goalsFor": 11,
"goalsAgainst": 10,
"points": 14
},
{
"team": "Stoke",
"played": 9,
"win": 4,
"draw": 1,
"loss": 4,
"goalsFor": 10,
"goalsAgainst": 9,
"points": 13
},
{
"team": "Swansea",
"played": 10,
"win": 3,
"draw": 4,
"loss": 3,
"goalsFor": 9,
"goalsAgainst": 12,
"points": 13
},
{
"team": "Everton",
"played": 11,
"win": 3,
"draw": 4,
"loss": 4,
"goalsFor": 23,
"goalsAgainst": 20,
"points": 13
},
{
"team": "Sunderland",
"played": 10,
"win": 3,
"draw": 2,
"loss": 5,
"goalsFor": 12,
"goalsAgainst": 11,
"points": 11
},
{
"team": "Bournemouth",
"played": 9,
"win": 2,
"draw": 4,
"loss": 3,
"goalsFor": 10,
"goalsAgainst": 13,
"points": 10
},
{
"team": "Newcastle United",
"played": 10,
"win": 2,
"draw": 4,
"loss": 4,
"goalsFor": 14,
"goalsAgainst": 14,
"points": 10
},
{
"team": "Aston Villa",
"played": 9,
"win": 0,
"draw": 3,
"loss": 6,
"goalsFor": 6,
"goalsAgainst": 13,
"points": 3
}
]
}
Thank You Very Much!
To create an object and add the total points for each team, you'll need to create a map (key/value) where the key is each team's name and the value is the total points for each team.
I'm hosting your data on MyJSON.
Here's a function that retrieves the data from the remote server, and initializes an object containing the total points for each team:
function getData() {
// https://api.myjson.com/bins/1c6utx
var teamScores = {};
$.get("https://api.myjson.com/bins/1c6utx", function(data, status){
$.each(data.records, function(index, value) {
if (!teamScores[value.team]) {
teamScores[value.team] = 0;
}
teamScores[value.team] += value.points;
});
console.log(JSON.stringify(teamScores));
});
}
/* Output:
{"Manchester City":24,"Arsenal":23,"Tottenham":19,"Leicester":19,"Manchester United":19,"West Ham":16,"Liverpool":15,"Norwich":15,"Southampton":14,"Chelsea":14,"West Bromwich Albion":14,"Crystal Palace":14,"Watford":14,"Stoke":13,"Swansea":13,"Everton":13,"Sunderland":11,"Bournemouth":10,"Newcastle United":10,"Aston Villa":3}
*/
You can check this fiddle to see it in action. Hope this helps!

data-ng-change does not work after set selectedIndex property

var app = angular.module('app', []);
app.controller('appController', ['$scope', function($scope) {
$scope.allTheme = [{
"theme_id": 2,
"productData": [{
"store_id": 1,
"product_id": 3,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - FD",
}, {
"store_id": 1,
"product_id": 4,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - FE"
}, {
"store_id": 1,
"product_id": 8,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - QS",
}]
}, {
"theme_id": 5,
"productData": [{
"store_id": 1,
"product_id": 99,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - FD"
}, {
"store_id": 1,
"product_id": 100,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - FE"
}, {
"store_id": 1,
"product_id": 101,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - QS"
}]
}, {
"theme_id": 7,
"productData": [{
"store_id": 1,
"product_id": 129,
"style_id": 1,
"size_id": 3,
"theme_id": 7,
"name": "Nautical"
}]
}, {
"theme_id": 10,
"productData": [{
"store_id": 1,
"product_id": 12,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - FD"
}, {
"store_id": 1,
"product_id": 13,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - FE"
}, {
"store_id": 1,
"product_id": 17,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - QS"
}]
}];
$scope.targetField = null;
$scope.selectBoxClick = function($event) {
if ($event.target === null) {
return;
}
$scope.targetField = $event.target;
}
$scope.changeTheme = function(theme, selectedProducts) {
console.log("chagne Theme");
if ($scope.targetField) {
$scope.activeTheme = theme;
if (selectedProducts) {
$scope.isSizedAndThemeSelected = true;
var targetSelect_ = $($scope.targetField);
$scope.targetField = null;
targetSelect_.closest(".owl-item").siblings().each(function() {
if ($(this).find("select").length) {
var option = $(this).find("select option").eq(0);
//$(this).find("select").selectbox("change", option.attr('value'), option.html());
var select_ = $(this).find("select");
select_.val(option.attr('value'));
select_.prop('selectedIndex',0);
}
})
} else {
$scope.isSizedAndThemeSelected = false;
$scope.activeTheme = {};
}
}
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://project-progress.net/projects/kodak-express-local-angular/js/jquery.selectbox-0.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div data-ng-app="app">
<div data-ng-controller="appController">
<div data-ng-repeat="theme in allTheme" style="width:500px;" class="owl-item">
<div class="select-box" data-ng-if="theme.productData.length > 1">
<!--<select class="customSelect" data-ng-model="theme.SelectedProduct" data-ng-click="selectBoxClick($event)" data-ng-change="changeTheme(theme, theme.SelectedProduct)">
<option value="">Select an option</option>
<option data-custom-select-item data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
</select>-->
<select data-ng-model="theme.SelectedProduct" data-ng-click="selectBoxClick($event)" data-ng-change="changeTheme(theme, theme.SelectedProduct)">
<option value="">Select an option</option>
<option data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
</select>
</div>
</div>
</div>
</div>
If I select 'Boy - FD' from the first select box option and after second select box option, choose 'boy-fed' in both can function fire, but again, select first selectbox 'Boy-FD' option than can function does not work. thanks in advance.
The reason why your dropdowns are not working properly is because when you update the DOM directly then your data models linked with "data-ng-model" dont get updated, because you updated the dropdowns outside of angular's digest cycle.
Here is how i got your dropdowns to work with angular, i hope this helps.
var app = angular.module('app', []);
app.controller('appController', ['$scope', function($scope) {
$scope.allTheme = [{
"theme_id": 2,
"productData": [{
"store_id": 1,
"product_id": 3,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - FD",
}, {
"store_id": 1,
"product_id": 4,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - FE"
}, {
"store_id": 1,
"product_id": 8,
"style_id": 1,
"size_id": 3,
"theme_id": 2,
"name": "Boy - QS",
}]
}, {
"theme_id": 5,
"productData": [{
"store_id": 1,
"product_id": 99,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - FD"
}, {
"store_id": 1,
"product_id": 100,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - FE"
}, {
"store_id": 1,
"product_id": 101,
"style_id": 1,
"size_id": 3,
"theme_id": 5,
"name": "Blank - QS"
}]
}, {
"theme_id": 7,
"productData": [{
"store_id": 1,
"product_id": 129,
"style_id": 1,
"size_id": 3,
"theme_id": 7,
"name": "Nautical"
}]
}, {
"theme_id": 10,
"productData": [{
"store_id": 1,
"product_id": 12,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - FD"
}, {
"store_id": 1,
"product_id": 13,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - FE"
}, {
"store_id": 1,
"product_id": 17,
"style_id": 1,
"size_id": 3,
"theme_id": 10,
"name": "Girl - QS"
}]
}];
$scope.changeTheme = function(theme) {
console.log("change Theme");
$scope.activeTheme = theme;
angular.forEach($scope.allTheme, function(item, index) {
if (item.SelectedProduct != theme.SelectedProduct) {
item.SelectedProduct = "";
}
});
console.log(theme.SelectedProduct);
}
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://project-progress.net/projects/kodak-express-local-angular/js/jquery.selectbox-0.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div data-ng-app="app">
<div data-ng-controller="appController">
<div data-ng-repeat="theme in allTheme" style="width:500px;" class="owl-item">
<div class="select-box" data-ng-if="theme.productData.length > 1">
<select data-ng-model="theme.SelectedProduct" data-ng-change="changeTheme(theme)">
<option value="">Select an option</option>
<option data-ng-repeat="product in theme.productData" data-ng-value="{{product}}">{{product.name}}</option>
</select>
</div>
</div>
</div>
</div>

How can be convert object arrays into multidimensional arrays in node.js?

We have a object arrays the below format in node.js and we want to this array in multidimensional arrays.
object arrays :
[ { id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 6,
attribute_label: 'level4',
attribute_order: 1,
attribute_name: 'level4_1'
{ id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 7,
attribute_label: 'level5',
attribute_order: 2,
attribute_name: 'level5_1'
{ id: 15,
process_id: 94,
process_type: 'Section',
process_type_order: 2,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null },
{ id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 8,
attribute_label: 'level6',
attribute_order: 1,
attribute_name: 'level6_1'
{ id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 9,
attribute_label: 'level7',
attribute_order: 2,
attribute_name: 'level7_1'
{ id: 6,
process_id: 94,
process_type: 'Section',
process_type_order: 4,
attribute_id: 10,
attribute_label: 'level8',
attribute_order: 1,
attribute_name: 'level8_1'
{ id: 14,
process_id: 94,
process_type: 'Section',
process_type_order: 5,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null } ]
and we want to this object array in below format (multidimensional arrays). please suggest that how can we convert it the below formate
[ [ { id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 6,
attribute_label: 'level4',
attribute_order: 1,
attribute_name: 'level4_1' },
{ id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 7,
attribute_label: 'level5',
attribute_order: 2,
attribute_name: 'level5_1' } ],
[ { id: 15,
process_id: 94,
process_type: 'Section',
process_type_order: 2,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null } ],
[ { id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 8,
attribute_label: 'level6',
attribute_order: 1,
attribute_name: 'level6_1' },
{ id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 9,
attribute_label: 'level7',
attribute_order: 2,
attribute_name: 'level7_1' } ],
[ { id: 6,
process_id: 94,
process_type: 'Section',
process_type_order: 4,
attribute_id: 10,
attribute_label: 'level8',
attribute_order: 1,
attribute_name: 'level8_1' } ],
[ { id: 14,
process_id: 94,
process_type: 'Section',
process_type_order: 5,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null } ],
]
Hope this helps createMultiArray creates your desired Array based on your objects id's:
var array = [ { id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 6,
attribute_label: 'level4',
attribute_order: 1,
attribute_name: 'level4_1'
},
{ id: 4,
process_id: 94,
process_type: 'Section',
process_type_order: 1,
attribute_id: 7,
attribute_label: 'level5',
attribute_order: 2,
attribute_name: 'level5_1'
},
{ id: 15,
process_id: 94,
process_type: 'Section',
process_type_order: 2,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null },
{ id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 8,
attribute_label: 'level6',
attribute_order: 1,
attribute_name: 'level6_1',
},
{ id: 5,
process_id: 94,
process_type: 'Section',
process_type_order: 3,
attribute_id: 9,
attribute_label: 'level7',
attribute_order: 2,
attribute_name: 'level7_1'
},
{ id: 6,
process_id: 94,
process_type: 'Section',
process_type_order: 4,
attribute_id: 10,
attribute_label: 'level8',
attribute_order: 1,
attribute_name: 'level8_1'
},
{ id: 14,
process_id: 94,
process_type: 'Section',
process_type_order: 5,
attribute_id: null,
attribute_label: null,
attribute_order: null,
attribute_name: null } ]
function createMultiArray(array){
var newArray = [];
array.forEach(function(obj,index){
var subarray;
var subArrayExists = false;
for(var sub=0; sub < newArray.length; sub++){
subarray = newArray[sub];
if(subarray.some(function(subobj){
return subobj.id == obj.id;
})){
subArrayExists = true;
break;
}
}
if(!subArrayExists){
newArray.push([obj]);
}
else{
subarray.push(obj);
}
});
return newArray;
}
createMultiArray(array);

Categories

Resources