skip to Main Content

What i have:

{"name": "author-1", "slug": {"_type": "slug", "current": "author-1-slug"}}
{"name": "author-2", "slug": {"_type": "slug", "current": "author-2-slug"}}
{"name": "author-1", "slug": {"_type": "slug", "current": "author-1-slug"}}
{"name": "author-3", "slug": {"_type": "slug", "current": "author-3-slug"}}

What i want:

{"name": "author-2", "slug": {"_type": "slug", "current": "author-2-slug"}}
{"name": "author-1", "slug": {"_type": "slug", "current": "author-1-slug"}}
{"name": "author-3", "slug": {"_type": "slug", "current": "author-3-slug"}}

What i tried:

const filtered = _.uniqBy(authors, 'slug')

//result [], [], [], []

Any solution?

2

Answers


  1. If this is lodash function, you shoud try like this, if you don’t want to have 2 same authors:

    const filtered = _.uniqBy(authors, 'name')
    
    Login or Signup to reply.
  2. const uniqueTags = [];
    authors.map((item) => {
      var findItem = uniqueTags.find((x) => x.name === item.name);
      if (!findItem) uniqueTags.push(item);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search