My json array with duplicates looks like:
var list = [
{"id":"123","class": "Math K", "subject": "Counting & Cardinality"},
{"id":"124","class": "Math K", "subject": "Counting & Cardinality"},
{"id":"125","class": "Math K", "subject": "Counting & Cardinality"},
{"id":"126","class": "Math K", "subject": "Counting & Cardinality"},
{"id":"127","class": "Math K", "subject": "Geometry"},
{"id":"128","class": "Math 1", "subject": "Counting & Cardinality"},
{"id":"129","class": "Math 1", "subject": "Counting & Cardinality"},
{"id":"120","class": "Math 1", "subject": "Orders of Operation"},
{"id":"121","class": "Math 2", "subject": "Geometry"},
{"id":"122","class": "Math 2", "subject": "Geometry"}
];
I need to remove duplicates with the same Class and Subject (e.g Math K and Counting & Cardinality or Math 2 and Geometry) but keep IDs for the first record, so it should look like:
var newList = [
{"id":"123","class": "Math K", "subject": "Counting & Cardinality"},
{"id":"127","class": "Math K", "subject": "Geometry"},
{"id":"128","class": "Math 1", "subject": "Counting & Cardinality"},
{"id":"120","class": "Math 1", "subject": "Orders of Operation"},
{"id":"121","class": "Math 2", "subject": "Geometry"},
];
I have tried this code:
var newList = [];
for( var class in list ) {
for( var subject in list[class] ) {
outputList.push({ id: id, class: class, subject: subject });
}
}
JSON.stringify( newList, null, 4 );
But it doesn't bring the results. I would really appreciate your help with tweaking my code. Thank you.
idis undeclared but you try to read its value). Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help.