Shaik Nizamuddin
Shaik Nizamuddin

Reputation: 609

How to filter different keys in a array and get unique result in angular js

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.

Upvotes: 5

Views: 258

Answers (4)

Rajib Chy
Rajib Chy

Reputation: 880

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 );

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22524

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);

Upvotes: 1

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10356

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);

Upvotes: 1

kukkuz
kukkuz

Reputation: 42352

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;}

Upvotes: 1

Related Questions