如何通过引用该对象来删除数组对象

时间:2017-11-27 06:27:38

标签: javascript

我有一个对象和一个包含大部分相同数据的数组。但是数组中的数据很少超过对象。我想从不在对象中的数组中删除那些特定数据。

这是我的对象

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[2]可以是额外的,而不是对象。如何删除它。

我正在尝试的代码是:

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

但这不是一个动态的。我的意思是UID可以是任何东西。

4 个答案:

答案 0 :(得分:2)

假设Obj的密钥并不总是与其值UID 完全相同,请尝试以下解决方案。

您需要首先迭代Obj的密钥并制作一个包含所有允许的UID

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

IE兼容版

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

现在按myarray

以外的值过滤掉allowedUIDs
myArray = myArray.filter(function( obj ) {
    return allowedUIDs.indexOf( obj.UID ) != -1;  //only those which are in allowedIDs are kept
}); 

演示

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

答案 1 :(得分:1)

差不多......在下面使用

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

答案 2 :(得分:0)

假设您的对象键是UID,您可以通过检查对象中是否存在这些键进行过滤,如

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

答案 3 :(得分:-1)

您可以使用UID运算符检查in是否在obj的键中。



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