I have the following JSON File: http://pastebin.com/1TguvZXc
The duplicate 'body' can be found in the array by traversing through the array:
models[x].years[y].styles[z].submodel.body
In other words:
models[0].years[0].styles[0].submodel.body
should be checked for duplicates in:
models[0].years[0].styles[1].submodel.body
models[0].years[0].styles[2].submodel.body
models[0].years[0].styles[n].submodel.body
The rest of the data is useless for me.
I have the following pipe:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterByCategory'
})
export class FilterByCategoryPipe implements PipeTransform {
transform(input: any , search: string): any[] {
if (input === undefined || input.length === 0) {
return input;
}
const filteredArr: Array<any> = JSON.parse(JSON.stringify(input));
for (const model of filteredArr) {
for (const year of model.years) {
year.styles = year.styles.filter(style => {
return style.submodel.body === search;
});
}
}
return filteredArr;
}
}
How would I add an additional 'test' to my .filter function that checks if the value of 'submodel.body' exists in any of the styles array?
Basically, you need to use the filter
function to remove the duplicate elements.
So, the function which removes duplicate would look like below and uses the indexOf
function
function removeDuplicate(element, index, array) {
return array.map(style => style.submodel.body).indexOf(element.submodel.body) === index;
}
Following is working JS version of the code, you can make use of it and add the where condition for the search as defined in your post.
function removeDuplicate(element, index, array) {
return array.map(style => style.submodel.body).indexOf(element.submodel.body) === index;
}
var data = '{"models":[{"id":"Acura_ILX","name":"ILX","niceName":"ilx","years":[{"id":401640361,"year":2017,"styles":[{"id":401640368,"name":"AcuraWatch Plus Package 4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"AcuraWatch Plus Package"},{"id":401640367,"name":"Technology Plus and A-SPEC Packages 4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"Technology Plus and A-SPEC Packages"},{"id":401640366,"name":"Premium Package 4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"Premium Package"},{"id":401640365,"name":"Premium and A-SPEC Packages 4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"Premium and A-SPEC Packages"},{"id":401640364,"name":"Technology Plus Package 4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"Technology Plus Package"},{"id":401640363,"name":"4dr Sedan (2.4L 4cyl 8AM)","submodel":{"body":"Sedan","modelName":"ILX Sedan","niceName":"sedan"},"trim":"Base"}]}]},{"id":"Acura_NSX","name":"NSX","niceName":"nsx","years":[{"id":200779937,"year":2017,"styles":[{"id":101418796,"name":"2dr Coupe AWD (3.5L 6cyl Turbo gas/electric hybrid 9AM)","submodel":{"body":"Coupe","modelName":"NSX Coupe","niceName":"coupe"},"trim":"Base"}]}]}],"modelsCount":6}';
var jsonData = JSON.parse(data);
jsonData.models.forEach(model =>
model.years.forEach(year =>
year.styles = year.styles.filter(removeDuplicate)
));
console.log(jsonData);