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

I am trying to filter data from a response and remove duplicate elements and push data into an array, my api response looks like this:

{
   "_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 answer above, if "object_id" exists in the object and "booking_id._id" is the same, I need to filter and click only unique objects in the array.

I need an answer as shown 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. Thank.

+4
source share
4 answers

Use Array.prototype.reduceand hashtableto highlight 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;}
Run code
+1
source

booking_id._id , :

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

You can use array#reduceandarray#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);
Run code
0
source

Data table as follows ==>

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 shown below, (s) returnunique string ==>

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

0
source

Source: https://habr.com/ru/post/1683535/


All Articles