skip to Main Content
let input = [
  {
    id: 1,
    rollNo: 5,
    Name: "A",
    stDetails: {
      city: "city1",
      pin: 1234,
    },
  },
  {
    id: 2,
    rollNo: 5,
    Name: "B",
    stDetails: {
      city: "city1",
      pin: 1234,
    },
  },
  {
    id: 3,
    rollNo: 5,
    Name: "C",
    stDetails: {
      city: "city1",
      pin: 1234,
    },
  },
  {
    id: 4,
    rollNo: 5,
    Name: "D",
    stDetails: {
      city: "city1",
      pin: 1234,
    },
  },
  {
    id: 5,
    rollNo: 10,
    Name: "E",
    stDetails: {
      city: "city2",
      pin: 1234,
    },
  },
];
let uniqueKey = ["rollNo", "stDetails.city"];

I want to get the id of all non-unique values in more than one occurrence and to check record is unique we need to check by rollNo and stDetails.city

So the expected output is:

[{id:2},{id:3},{id:4}];

Here you can see id 2,3 and 4 have the same details as id 1 so that’s why it is expected output

2

Answers


  1. var input=[{"id":1,"rollNo":5,"Name":"A","stDetails":{"city":"city1","pin":1234}},
    {"id":2,"rollNo":5,"Name":"B","stDetails":{"city":"city1","pin":1234}},
    {"id":3,"rollNo":5,"Name":"C","stDetails":{"city":"city1","pin":1234}},
    {"id":4,"rollNo":5,"Name":"D","stDetails":{"city":"city1","pin":1234}},
    {"id":5,"rollNo":10,"Name":"E","stDetails":{"city":"city2","pin":1234}}
    ];
    // Group by column rollNo & stDetails's city
    var groupByCol=_.groupBy(input,function(x){return x.rollNo+'#'+x.stDetails.city});
    var finaldata=[];
    //Loop the record of grouping
    _.forEach(groupByCol,function(c){
    // If find match more than one then check.
    if(c.length>1){
    var finaldata=_.drop(_.map(c,function(x){return {"id":x.id}}))
    console.log(finaldata);
    }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

    You can do it with this way.

    Login or Signup to reply.
  2. Pure JS Solution

    Of course you can use libraries but pure ones are always the best!

    You should loop through the input then save unique ones and compare each one to uniques array!

    If you have trouble with comparison of objects, simply convert it to JSON!

    Comment if you have questions…

    var input=[{"id":1,"rollNo":5,"Name":"A","stDetails":{"city":"city1","pin":1234}},
    {"id":2,"rollNo":5,"Name":"B","stDetails":{"city":"city1","pin":1234}},
    {"id":3,"rollNo":5,"Name":"C","stDetails":{"city":"city1","pin":1234}},
    {"id":4,"rollNo":5,"Name":"D","stDetails":{"city":"city1","pin":1234}},
    {"id":5,"rollNo":10,"Name":"E","stDetails":{"city":"city2","pin":1234}}
    ];
    
    let unique_keys = ["rollNo", "stDetails"];
    let uniques = [];
    let non_uniques = [];
    let uic;
    
    for (let i of input) {
      for (let u of uniques) {
        uic = unique_keys.length;
        for (let uk of unique_keys) {
          let iitem = JSON.stringify(i[uk]);
          let uitem = JSON.stringify(u[uk]);
          if (iitem == uitem && --uic == 0) break;
        }
      }
      if (uic != 0) uniques.push(i);
      else non_uniques.push(i);
    }
    
    console.log(non_uniques);

    Notice: If you needed the unique ones, print the uniques array instead of non_uniques array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search