David
David

Reputation: 4283

How to remove an array object with reference to the object

I have an object and one array which having most of the identical data. But few of the data in array is more than in object. I want to remove those perticular data from array which is not in object.

Here is my object

Obj = {
    "USA" :{
        "Country" : "USA",
        "Capital" : "WDC",
        "Rank" : "1",
        "UID" : "USA"
    },
    "UK" :{
        "Country" : "UK",
        "Capital" : "LONDON",
        "Rank" : "2",
        "UID" : "UK"
    }
}

Here is my array.

myArray = [ {
                "Country" : "USA",
                "Capital" : "WDC",
                "Rank" : "1",
                "UID" : "USA"
            },{
                "Country" : "UK",
                "Capital" : "LONDON",
                "Rank" : "2",
                "UID" : "UK"
            },{
                "Country" : "China",
                "Capital" : "Beijing",
                "Rank" : "3",
                "UID" : "China"
            }]

You can myArray[2] is extra in myArraywhich is not in object. How to remove this.

My code which I am trying is :

myArray = myArray.filter(function( obj ) {
        return obj.UID !== 'China';
    });

But this is not a dynamic. I mean UID could be anything.

Upvotes: 1

Views: 74

Answers (4)

gurvinder372
gurvinder372

Reputation: 68443

Assuming that Obj's keys are not always exactly same as its value's UID, try below solution.

You need to first iterate your Obj's keys and make an array of all the allowed UID's

var allowedUIDs = Object.values(Obj).map( s => sObj[s].UID );

IE compatible version

var allowedUIDs = Object.keys(Obj).map( function( s ){ return Obj[s].UID });

Now filter out myarray by those values which are not in allowedUIDs

myArray = myArray.filter(function( obj ) {
    return allowedUIDs.indexOf( obj.UID ) != -1;  //only those which are in allowedIDs are kept
}); 

Demo

var Obj = {
    "USA" :{
        "Country" : "USA",
        "Capital" : "WDC",
        "Rank" : "1",
        "UID" : "USA"
    },
    "UK" :{
        "Country" : "UK",
        "Capital" : "LONDON",
        "Rank" : "2",
        "UID" : "UK"
    }
};

var myArray = [ {
    "Country" : "USA",
    "Capital" : "WDC",
    "Rank" : "1",
    "UID" : "USA"
},{
    "Country" : "UK",
    "Capital" : "LONDON",
    "Rank" : "2",
    "UID" : "UK"
},{
    "Country" : "China",
    "Capital" : "Beijing",
    "Rank" : "3",
    "UID" : "China"
}];

var allowedUIDs = Object.values(Obj).map( s => s.UID );

console.log( allowedUIDs )

var output = myArray.filter(function( obj ) {
    return allowedUIDs.indexOf( obj.UID ) != -1; 
}); 

console.log( output );

Upvotes: 2

Shubham Khatri
Shubham Khatri

Reputation: 282140

Assuming your object keys to be UIDs, you can filter by checking if those keys exist in the Object like

Obj = {
    "USA" :{
        "Country" : "USA",
        "Capital" : "WDC",
        "Rank" : "1",
        "UID" : "USA"
    },
    "UK" :{
        "Country" : "UK",
        "Capital" : "LONDON",
        "Rank" : "2",
        "UID" : "UK"
    }
}

myArray = [ {
                "Country" : "USA",
                "Capital" : "WDC",
                "Rank" : "1",
                "UID" : "USA"
            },{
                "Country" : "UK",
                "Capital" : "LONDON",
                "Rank" : "2",
                "UID" : "UK"
            },{
                "Country" : "China",
                "Capital" : "Beijing",
                "Rank" : "3",
                "UID" : "China"
            }]

myArray = myArray.filter(function( obj ) {
        return Obj[obj.UID] !== 'undefined';
    });
    
console.log(myArray);

Upvotes: -1

felixmosh
felixmosh

Reputation: 35613

You can use check if the UID is in the keys of the obj using in operator.

const obj = {
  "USA": {
    "Country": "USA",
    "Capital": "WDC",
    "Rank": "1",
    "UID": "USA"
  },
  "UK": {
    "Country": "UK",
    "Capital": "LONDON",
    "Rank": "2",
    "UID": "UK"
  }
}


const myArray = [{
  "Country": "USA",
  "Capital": "WDC",
  "Rank": "1",
  "UID": "USA"
}, {
  "Country": "UK",
  "Capital": "LONDON",
  "Rank": "2",
  "UID": "UK"
}, {
  "Country": "China",
  "Capital": "Beijing",
  "Rank": "3",
  "UID": "China"
}]

const objKeys = Object.keys(obj);
const result = myArray.filter((item) => item.UID in obj)

console.log(result);

Upvotes: -1

Sajith Edirisinghe
Sajith Edirisinghe

Reputation: 1737

Almost there... Use below

myArray = myArray.filter(function( obj ) {
    return obj.UID in Obj ;
});

Upvotes: 1

Related Questions