1

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

1
  • You always need to remove the lines with a "/" or it can be something else ? (another pattern) Commented Apr 10, 2015 at 9:30

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1
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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.