0

I'd like to manipulate a little bit my data, and specifically, I'd like to remove some keys (array within another array) if its name includes something.

For example, let's say this is my data:

var myArray = [{
    "toDelete_item1": [],
    "name": "Joe"
}, {
    "toDelete_item2": [],
    "name": "Adam"
}, {
    "name": "Peter",
    "toDelete_item3": []
}]

So now I want to exclude from my array all keys which include "toDelete_" in their name.

So I can have something like that:

var myArray = [{
    "name": "Joe"
}, {
    "name": "Adam"
}, {
    "name": "Peter"
}]

Is this possible?

Tushar
  • 85,780
  • 21
  • 159
  • 179
Vassilis Pits
  • 3,788
  • 4
  • 33
  • 48

5 Answers5

2

Here is what you can do to delete all toDelele_... properties. Using the array.prototype.forEach to iterate over the array, then look for a match of toDelete as a property within every obj.

If there is a match, delete the property.

var data =  [
    {
        "toDelete_item1": [],
        "name": "Joe"
    },
    {
        "toDelete_item2": [],
        "name": "Adam"
    },
    {
        "name": "Peter",
        "toDelete_item3": []
    }
];


data.forEach(function(item,index){
    for(var prop in item) { 
        if (prop.match(/toDelete/) !== null) {
            delete item[prop];    
        }
    }
});

console.log(data);

As mentioned in the comments that this solution will only work for the given scenario, lets turn this into a more usable function. Which will only delete properties within an object which is an item of the data array. Not recursively though.

I don't see the need of checking if the property exists, either there is a match or not (pleas correct me if i am wrong). Sticking with match other than indexOf seems to be just a bit more flexible.

var data =  [
    {"toDelete_item1": [], "name": "Joe"},
    {"toDelete_item2": [], "name": "Adam"},
    {"name": "Peter", "toDelete_item3": []},
    ["toDelete_item4", "Foo"],
    "toDelete_item5"
];

function delPropFromObjsInArr (reg, arr) {
    var regex = new RegExp(reg);
    arr.forEach(function(item, index){
        if (item === Object(item)) {
            for(var p in item) { 
                if (p.match(regex) !== null) {
                    delete item[p];    
                }
            }            
        }
    });    
}

delPropFromObjsInArr("toDelete", data);
console.log(data); 
DavidDomain
  • 14,976
  • 4
  • 42
  • 50
0

It's very simple iteration really and then string checking.

For removing properties of objects you can use the delete keyword.

for iterating through an objects properties use for(propname in object) to do that. If you use an indexed array, propname will be an integer with the current index.

If you have an object with named variables you need to run the test object.hasOwnProperty(propname) so you dont work on native objects or values.

var myArray =  [
        {
            "toDelete_item1": [],
            "name": "Joe"
        },
        {
            "toDelete_item2": [],
            "name": "Adam"
        },
        {
            "name": "Peter",
            "toDelete_item3": []
        }
    ]
 for(var i in myArray) {
   for(var key in myArray[i]) {
     if(myArray[i].hasOwnProperty(key)) {
       if(key.indexOf('toDelete_') === 0) {
         delete myArray[i][key]
         }
       }
     }
   }
console.log(myArray);
Tschallacka
  • 27,901
  • 14
  • 88
  • 133
0

You will need two loops to go through all your elements in the array, then go through all the properties of the objects, then to delete the one you don't need.

for (var i in myArray) {

    var element = myArray[i];

    for (var prop in element) {
       if (prop.indexOf('toDelete_') == 0) {
          delete element[prop];
       }
    }

}
Aston
  • 3,654
  • 1
  • 21
  • 18
-1

Use the "delete" keyword in Javascript.

delete myArray["toDelete_item1"];

You might want to perform a search and list the name of keys to be deleted in an array and delete them one by one.

Reference: How do I remove objects from a javascript associative array?

Community
  • 1
  • 1
orb
  • 175
  • 2
  • 13
-1

Like this?

myArray.forEach( function(current){
    for(var p in current){
        if( current[p].constructor === Array){
            delete current[p];
        }
    }
});
AdityaParab
  • 7,024
  • 3
  • 27
  • 40