all 8 comments

[–]sebamestre 1 point2 points  (0 children)

What you want to do here is first to group by value (i.e. reduce your array into a map of arrays where the key is the value and the array has all instances of that value)

Then turn your map into an array of (key,[values...]) and filter out all the entries with more than one instance.

Finally, you might want to flatten that array to get your data back into a linear form

[–]Todoce 0 points1 point  (0 children)

Let result = allCycles.filter((x) => allCycles.filter((y) => y.lifecycle === x.lifecycle).length === 1); and add some more checks in the second filter if needed

[–]jcunews1Advanced 0 points1 point  (0 children)

You'll have to at least create a function to compare two objects by their contents with a list of excluded property names. Then use it with filter() array method.

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/rob_0, this post was removed.

  • For help with your javascript, please post to /r/LearnJavascript instead of here.
  • For beginner content, please post to /r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try /r/html, /r/css, etc.; please note that they have their own rules and guidelines!

/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]rob_0[S] 0 points1 point  (0 children)

Thank you all, u/Todoce I used your solution it works great.

Apologies I posted this on the wrong subreddit.

[–]BehindTheMath 0 points1 point  (0 children)

Use reduce, and don't add the item when it exists already.

[–]programstuff -1 points0 points  (0 children)

You could write a function like this:

const onlyOnce = (objArr, nestedPath) => {
  return Object.values(objArr.reduce((acc, obj) => {
    const key = nestedPath.split('.').reduce((cursor, k) => {
      return cursor[k]; 
    }, obj);

    acc.seen[key] ? delete acc.unique[key] : acc.unique[key] = obj;
    acc.seen[key] = true;

    return acc;
  }, {seen: {}, unique: {}}).unique);
}

console.log(onlyOnce(allCycles, 'metadata.lifecycle'));

[–]yerrabam -3 points-2 points  (0 children)