0

I have an array which looks like below

var nestedArray = [
  { id: 1, filter: { type: "nestedArray", name: "5" } },
  { id: 2, filter: { type: "nestedArray1", name: "6" } },
  { id: 3, filter: { type: "nestedArray", name: "5" } }
];

Now here I have a duplicate object, I just want to identify the duplicates using a Lodash method. Any help is appreciated.

Have already tried map, filter options but need something in Lodash method.

1
  • Can you provide the expected output? Commented Jan 8, 2019 at 9:31

2 Answers 2

1

You can use reject with a Property.

var myArray = [
  {
    "id": 1,
    "filter": {
      "type": "nestedArray",
      "name": "5"
    }
  },
  {
    "id": 2,
    "filter": {
      "type": "nestedArray1",
      "name": "6"
    }
  },
  {
    "id": 3,
    "filter": {
      "type": "nestedArray",
      "name": "5"
    }
  }
];

var result = _.reject(myArray, ['filter.name', '5']);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

This would work if the OP can use the id field as a unique identifier for the object.
1

So u want to reject duplicate ones by value in filter key?

_.uniqWith(nestedArray, (x, y) => _.isEqual(x.filter, y.filter), myArray)

If you have the flexibility to use Ramda instead, it could be more concise like because of every functions are auto-curried

const rejectDuplicateFilter = R.uniqWith(R.eqProps('filter'));
rejectDuplicateFilter(myArray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.