简体   繁体   中英

Need to remove specific objects having a specific pattern in keys from an array of objects in javascript

Hi I have an array of Objects of following type:

var person = [{
    "country" : "United States",
    "firstName/per/one" : "John",
    "lastName/per"  : "Doe",
    "age/per"       : 50,
    "eyeColo/per"  : "blue"
},{
    "firstName/per" : "james",
    "lastName.per"  : "bond",
    "age_per"       : 50,
    "eyeColo/per.xyz"  : "blue"
}];

My requirement is to remove all those "key:value" pairs in which the key has any slashes (/) in it. So if we take above array of objects my required output is as follows:

var person = [{
    "country" : "United States"    
},{
    "lastName.per"  : "bond",
    "age_per"       : 50    
}];

In short need to remove elements having a specific pattern in its keys (that patern in above array of objects is "/")

Thanks

The function indexOf(pattern) will tell you if a string contains the pattern provided. Also, in javascript, you can iterate an object using a for loop. So coupling that together we can do the following:


 var person = [{ "country": "United States", "firstName/per/one": "John", "lastName/per": "Doe", "age/per": 50, "eyeColo/per": "blue" }, { "firstName/per": "james", "lastName.per": "bond", "age_per": 50, "eyeColo/per.xyz": "blue" }]; var strippedPerson = []; for (var i = 0; i < person.length; i++) { var newDetails = {}; // iterate the keys of the person for (var key in person[i]) { // see if there is a slash in the key (indexOf returns -1 if there is no occurance of the pattern) if (key.indexOf('/') == -1) { // store the key and value as there is no slash newDetails[key] = person[i][key]; } } strippedPerson.push(newDetails); } // strippedPerson has no keys with slashes in document.write('<pre>person = ' + JSON.stringify(person, null, '\\t') + '</pre>'); document.write('<pre>strippedPerson = ' + JSON.stringify(strippedPerson, null, '\\t') + '</pre>'); 

var person = [{
    "country" : "United States",
    "firstName/per/one" : "John",
    "lastName/per"  : "Doe",
    "age/per"       : 50,
    "eyeColo/per"  : "blue"
},{
    "firstName/per" : "james",
    "lastName.per"  : "bond",
    "age_per"       : 50,
    "eyeColo/per.xyz"  : "blue"
}];

for(var i = 0; i < person.length; i++) {
    for(key in person[i]) {
        if(key.indexOf('/')!=-1) {
            delete person[i][key]
        }
    }
}
console.log(person)

Fiddle Demo

recursion method:

function removeSlash(arr){
var res={};
for(i in arr){
if(typeof arr[i]==='object'){res[i]=removeSlash(arr[i]);continue;}
if(i.indexOf('/')===-1)res[i]=arr[i]
}
return res;
}

var person = [{
"country" : "United States",
"firstName/per/one" : "John",
"lastName/per"  : "Doe",
"age/per"       : 50,
"eyeColo/per"  : "blue"
},{
"firstName/per" : "james",
"lastName.per"  : "bond",
"age_per"       : 50,
"eyeColo/per.xyz"  : "blue"
}];

person=removeSlash(person);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM