-7

I have an array of objects that I'm trying to loop through to match for a specific value, if found delete that object else return false. Below is the code:

array: [{
 obj1:{
    id: null,
    name:'test',
    address: '6857346jfhbdjshb'
 },
  obj12:{
    id: 678346,
    name:'test',
    address: '63784hgj'
 },
  obj13:{
    id: null,
    name:'test',
    address: 'tevhgvd'
 },
  obj15:{
    id: 65847,
    name:'test',
    address: 'djhvwe677ghdgv'
   },
  obj18:{
    address: ""
   }
}]

js:

    for (var obj in array){
       if (array[obj].address === "63784hgj" || array[obj].address === "djhvwe677ghdgv" ||array[prop].address === "")
     {
     array[obj].destroy(); //equivalent to array[1].destroy() (destroy, delete,remove,hide, pop none of them work)
     }
  }

I'm not sure which function is the correct one to remove object from array.

user1234
  • 3,000
  • 4
  • 50
  • 102
  • 1
    Hard to believe that you came up with zero results for this in a web search – charlietfl Jun 23 '16 at 03:53
  • 1
    There is only one element in your array, so looping over it is not going to accomplish very much. You want to loop over the keys in the object which is the single element. Once you find a key you want to delete, you can delete it in [the standard way](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete). –  Jun 23 '16 at 03:54
  • *I have an array of objects that im trying to loop through to match for a specific value,* No you don't. You have an array containing a **single** element, which is an object containing multiple properties. –  Jun 23 '16 at 03:55
  • Actually, this is not about removing an array element (and therefore is not a duplicate of questions about that). It's about removing object properties (I think). –  Jun 23 '16 at 03:56

3 Answers3

1

You apparently want to filter out certain properties from the single element of your array, which is an object, containing subobjects, when the address property of the subobject matches one or more string values.

Extract the first element of the array, then loop over its properties with a for...in loop. If the subproperty matches, delete the property with delete.

function filterproc(array) {
  var o = array[0];

  for (var k in o) 
    if (o[k].address === "....")
      delete o[k];
}

To match against multiple possibilities, consider using indexOf, which checks if an array contains some value:

if (['63784hgj', ...].indexOf(o[k]) !== -1) ...
1

If what you want is what toranaburo described in his answer then nothing to do with mine. :( (inform me to delete mine)

If you want a function which returns the new value do this:

var forbiddenAddressList = ['', '63784hgj', 'djhvwe677ghdgv'];
var newArray = myArray.filter(i => forbiddenAddressList.indexof(i.address) === -1);

another way:

var newArray = [];

for (var i = 0; i < myArray.length; i++)
    if(forbiddenAddressList.indexof(i.address) === -1)
        newArray.push(myArray[i]);
Community
  • 1
  • 1
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48
1

So if what you're asking is to loop through an array of objects, and if the value of a property is found then delete the object it belongs to, then this is what I came up with.

var array = [
        {id: null,name:'test',address: '6857346jfhbdjshb'},
        {id: 678346,name:'test',address: '63784hgj'},
        {id: null,name:'test',address: 'tevhgvd'},
        {id: 65847,name:'test',address: 'djhvwe677ghdgv'},
        {address: ""}
    ];

for (var obj in array){
       if (array[obj].address === "63784hgj" || array[obj].address === "djhvwe677ghdgv" || array[obj].address === "")
        {
            array.splice(obj, 1);
        }
    }

I modified your array of objects, but everything except Obj1, Obj2 etc. remains because arrays are already indexed, thus array[0] refers to Obj1. Also in your if statement you had array[prop] which is undefined, so be sure to change it in your code. This worked for what I think you were trying to do, so let me know if it helps.

Bryant R.
  • 21
  • 3