-1

I got the following json:

[
{
    "course": {
        "active": true,
        "abstract": "abstract",
        "display_title": "Display title",
    "completed_at": "2019-08-30",
    "document": "/course/certificate/1/",
},
{
    "course": {
        "active": true,
        "abstract": "abstract",
        "display_title": "ANOTHER title",
    "completed_at": "2019-09-08",
    "document": "/course/certificate/3/",
},
{
    "course": {
        "active": true,
        "abstract": "abstract",
        "display_title": "Display title",
    "completed_at": "2019-09-08",
    "document": "/course/certificate/3/",
}
]

from this I want to create a new list that do not contains any duplicates based on course.display_title. So if an array in the list allready exist with that course.display_title that whole array should be gone.

So the end result from above should be:

[
{
    "course": {
        "active": true,
        "abstract": "abstract",
        "display_title": "Display title",
    "completed_at": "2019-08-30",
    "document": "/course/certificate/1/",
},
{
    "course": {
        "active": true,
        "abstract": "abstract",
        "display_title": "ANOTHER title",
    "completed_at": "2019-09-08",
    "document": "/course/certificate/3/",
}]

So the third item should not be in the list since the first have the same display_title as it does.

I have tried some filter and map function but they only return all the unique titles and not everything else that belongs in that array.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Peter
  • 37
  • 7
  • Duplicate of https://stackoverflow.com/questions/57296086/how-to-remove-item-from-json-by-passing-ids/57296700#57296700. – Cat Sep 22 '19 at 18:15

2 Answers2

0

Loop through the data, keeping track of all titles you encounter. If you haven't seen the title before, save the item to an array.

var uniqueTitles = [];
var filteredData = [];

data.forEach(function (item) {
    if (uniqueTitles.indexOf(item.course.display_title) === -1) {
        uniqueTitles.push(item.course.display_title);
        filteredData.push(item);
    }
});

console.log(filteredData); // contains the unique items

If you happen to use Lodash in your project, you could use uniqBy.

Brother Woodrow
  • 6,092
  • 3
  • 18
  • 20
0

you can use Lodash uniqBy to do this.

        var data = [
        {
            "course": {
                "active": true,
                "abstract": "abstract",
                "display_title": "Display title",
                },
            "completed_at": "2019-08-30",
            "document": "/course/certificate/1/",
        },
        {
            "course": {
                "active": true,
                "abstract": "abstract",
                "display_title": "ANOTHER title",
            },
            "completed_at": "2019-09-08",
            "document": "/course/certificate/3/",
        },
        {
            "course": {
                "active": true,
                "abstract": "abstract",
                "display_title": "Display title",
            },
            "completed_at": "2019-09-08",
            "document": "/course/certificate/3/",
        }
        ]

Then using Lodash uniqBy

var uniqueResult = _.uniqBy(data, 'course.display_title');
Cristian Cimuca
  • 177
  • 1
  • 8