5

I'm trying to filter data from the response and remove duplicate items and push the data into an array, my api response goes as below:

{
   "_id":"0",
   "yacht_id":"200",
   "promo_id":"300",
   "blocked_thru":"promotions",
   "dates":"2017-08-23T00:00:00.000Z",
},
{
  "_id":"1",
  "booking_id":{
        "_id":"100",
        "booking_id":"BK163041494",
               },
  "blocked_thru":"booked",
  "dates":"2017-08-30T00:00:00.000Z",
 },
 {
   "_id":"2",
   "booking_id":{
        "_id":"100",
        "booking_id":"BK163041494",
                 },
    "blocked_thru":"booked",
    "dates":"2017-08-30T00:00:00.000Z",
 }

From the above response, if "booking_id" exist in object and "booking_id._id" is same then, I need to filter and push only unique objects to array.

I need a response as below:

{
   "_id":"0",
   "yacht_id":"200",
   "promo_id":"300",
   "blocked_thru":"promotions",
   "dates":"2017-08-23T00:00:00.000Z",
},
{
  "_id":"1",
  "booking_id":{
        "_id":"100",
        "booking_id":"BK163041494",
               },
  "blocked_thru":"booked",
  "dates":"2017-08-30T00:00:00.000Z",
 },

Any Help would be Appreciated. Thanks.

3
  • What have you done so far ? Add your code please not only your JSON Commented Aug 11, 2017 at 13:32
  • If you want a simple solution you could go with: _.uniqWith(objects, _.isEqual); lodash.com/docs/#uniqWith Commented Aug 11, 2017 at 13:32
  • @user3492940 Thanks a lot,It worked for me. Commented Aug 11, 2017 at 14:22

4 Answers 4

1

You can use array#reduce and array#some

var response =[{"_id":"0","yacht_id":"200","promo_id":"300","blocked_thru":"promotions","dates":"2017-08-23T00:00:00.000Z",},{"_id":"1","booking_id":{"_id":"100","booking_id":"BK163041494",},"blocked_thru":"booked","dates":"2017-08-30T00:00:00.000Z",},{"_id":"2","booking_id":{"_id":"100","booking_id":"BK163041494",},"blocked_thru":"booked","dates":"2017-08-30T00:00:00.000Z",}];

var unique = response.reduce((res, obj) => {
  let isFound = res.some(o =>
    o['booking_id'] && o['booking_id']['_id'] === obj['booking_id']['_id'] );
  if(!isFound) {
    res.push(obj);
  }
  return res;
}, []);

console.log(unique);

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

Comments

1

Use Array.prototype.reduce and a hashtable to pick out the unique elements - see demo below:

var object=[{"_id":"0","yacht_id":"200","promo_id":"300","blocked_thru":"promotions","dates":"2017-08-23T00:00:00.000Z",},{"_id":"1","booking_id":{"_id":"100","booking_id":"BK163041494",},"blocked_thru":"booked","dates":"2017-08-30T00:00:00.000Z",},{"_id":"2","booking_id":{"_id":"100","booking_id":"BK163041494",},"blocked_thru":"booked","dates":"2017-08-30T00:00:00.000Z",}];

var result = object.reduce(function(hash){
    return function(p, c) {
      if(!c.booking_id || (c.booking_id && !hash[c.booking_id.booking_id])) {
        if(c.booking_id)
          hash[c.booking_id.booking_id] = true;
        p.push(c);
      }
      return p;
    };
}(Object.create(null)),[]);
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

Comments

1

You can use an object as a map to hold only one object per booking_id._id for those which have such field:

var objs=[{_id:"0",yacht_id:"200",promo_id:"300",blocked_thru:"promotions",dates:"2017-08-23T00:00:00.000Z"},{_id:"1",booking_id:{_id:"100",booking_id:"BK163041494"},blocked_thru:"booked",dates:"2017-08-30T00:00:00.000Z"},{_id:"2",booking_id:{_id:"100",booking_id:"BK163041494"},blocked_thru:"booked",dates:"2017-08-30T00:00:00.000Z"}];

var uniqueObjs = [];
var bookingObjsMap = {};

objs.forEach((obj) => {
  if (obj.booking_id) {
     bookingObjsMap[obj.booking_id._id]= obj;
  }
  else {
     uniqueObjs.push(obj);
  }
});

uniqueObjs = uniqueObjs.concat(Object.values(bookingObjsMap));
console.log(uniqueObjs);

Comments

0

Data Table as following==>

var dataTable = [{
    "_id": "0", "yacht_id": "200",
    "promo_id": "300", "blocked_thru": "promotions", "dates": "2017-08-23T00:00:00.000Z"
}, {
    "_id": "1", "booking_id": {
        "_id": "100", "booking_id": "BK163041494"
    },
    "blocked_thru": "booked", "dates": "2017-08-30T00:00:00.000Z"
}, {
    "_id": "2", "booking_id": {
        "_id": "100", "booking_id": "BK163041494"
    },
    "blocked_thru": "booked", "dates": "2017-08-30T00:00:00.000Z"
}];

Create a method as like as bellow, that(s) return you unique row(s)==>

function getUniqueRows( data ) {
    let uniqueRows = []; let book = [];
    for ( let i = 0, l = data.length; i < l; i++ ) {
        let obj = data[i];
        if ( typeof ( obj.booking_id ) !== 'object' ) {
            uniqueRows.push( obj );
            continue;
        }
        if ( book.find( a => a == obj.booking_id._id ) !== undefined )
            continue;
        uniqueRows.push( obj );
        book.push( obj.booking_id._id );
    }
    return uniqueRows;
};

and use var myUniqueRows = getUniqueRows( dataTable );

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.