0

I have tried to below way to get the duplicate record, but I got the only filtered record means only an uniq record gets.

var filtered = _.uniqWith(form_data.itemRows, _.isEqual);

ArrayList:

 [
    {
        "PlanId": "",
        "Id": "",
        "Type": "Beverages",
        "Beverages": "2019/12/7 11:40",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "Beverages": "2019/12/7 11:30",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "Beverages": "2019/12/7 11:30",
        "Frequency": 1
    }
]

Type, Beverages, Frequency wise get the duplicate records.

3
  • do you want to remove the duplicate Object from the array or you only want the duplicate Object value Commented Dec 6, 2019 at 6:39
  • I want only a duplicate object value. Commented Dec 6, 2019 at 6:54
  • i have added the solution please check it @Bansi29 Commented Dec 6, 2019 at 6:55

2 Answers 2

2

This way you can do it

let jsonArray = [
    {
        "PlanId": "",
        "Id": "",
        "Type": "Beverages",
        "ActivityTime": "2019/12/7 11:40",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "ActivityTime": "2019/12/7 11:30",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "ActivityTime": "2019/12/7 11:30",
        "Frequency": 1
    }
]
let Result = _.filter(
    _.uniq(
        _.map(jsonArray, function (item) {
            if (_.filter(jsonArray, {
                PlanId: item.PlanId,
                Id: item.Id,
                Type: item.Type,
                ActivityTime: item.ActivityTime,
                Frequency: item.Frequency
            }).length > 1) {
                return item
            }

            return false;
        })),
function (value) { return value; });

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

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

1 Comment

Hi @Narendra Not only type-wise checked it check same time & activity time and Frequency wise get the duplicate record.
0

You can go through this way -

const arrayList =[
    {
        "PlanId": "",
        "Id": "",
        "Type": "Beverages",
        "Beverages": "2019/12/7 11:40",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "Beverages": "2019/12/7 11:30",
        "Frequency": 1
    },
    {
        "PlanId": "",
        "Id": "",
        "Type": "Medication",
        "Beverages": "2019/12/7 11:30",
        "Frequency": 1
    }
];
const returnObjects = _.chain(arrayList)
    .groupBy((elem) => JSON.stringify(elem))
    .reduce((nObj, obj) => {
        if (obj.length > 1) nObj.push(_.head(obj));
        return nObj
    }, [])
    .value();
console.log(returnObjects);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

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.