0

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?

Moshe
  • 2,583
  • 4
  • 27
  • 70
  • 1
    you can make use of `Array` `indexOf` or `includes` method. Check the browser compatibility for `includes` – Abhinav Galodha Feb 10 '17 at 16:45
  • I'm relatively new to javascript, can you elaborate where I would place this method in order to filter the results of `year.styles = year.styles.filter(style => { return style.submodel.body === search; });` ? – Moshe Feb 10 '17 at 16:46
  • 1
    I suggest, you create a new array to store the `styles` and then check for the existence of style in the created array. – Abhinav Galodha Feb 10 '17 at 16:48
  • @Agalo create a new array to store the styles? They're already in arrays of style objects (which is what I need) – Moshe Feb 10 '17 at 16:52
  • 1
    Do you want to have unique elements in the `year.styles` array ? – Abhinav Galodha Feb 10 '17 at 16:57
  • @Agalo yes, see the JSON file I included. year.styles are indeed unique, however, since I don't care about the styles, I only care about the uniqueness of 'styles.submodel.body' --> note that even though MOST styles share the same styles.submodel.body, often enough, they have different styles.submodel.body – Moshe Feb 10 '17 at 17:02
  • @Alago, misunderstood your question. Essentially, yes. if styles[0] has body = 'Coupe', and styles[1] has the same body, I would prefer styles[1] to be removed from the array (or conversely, since styles[0] already has body 'coupe', styles[1] is not added to new array) – Moshe Feb 10 '17 at 17:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135403/discussion-between-miguel-and-agalo). – Moshe Feb 10 '17 at 17:11

1 Answers1

2

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);
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
  • exactly what I was looking for! Will apply and respond with any troubles. Thanks! @Agalo – Moshe Feb 10 '17 at 17:50