Home > Net >  React Native - How to merge User items and show on his profile?
React Native - How to merge User items and show on his profile?

Time:09-27

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?

CodePudding user response:

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')

CodePudding user response:

const uniqueTags = [];
authors.map((item) => {
  var findItem = uniqueTags.find((x) => x.name === item.name);
  if (!findItem) uniqueTags.push(item);
});
  • Related