简体   繁体   中英

Remove object from array based from another array

This is the structure of my first array:

var myArray = [1,Firstname,Lastname];

My second array is consist of object with property:

var mySecondArray = [object, object, object];

And when you expanded it, on the first array object it will show something like this:

Id = 1
FirstName = Test
LastName = TestLastName

I need to remove the object if it has the same id from the first array. I tried using this but no luck:

// if condition
myArray.splice(mySecondArray[x].ID, 1);

Any idea?

EDIT

So this is the whole idea. I have array of all my items (objects) and i have array of invalid items (in this case this is the string array)

var originalLength = validRowsArray.length;
for (var x = 0; x < invalidRowsArray.length; x++) {
    for (var y = 0; y < originalLength; y++) {
        if (validRowsArray[x].ID != invalidRowsArray[y][0]) { // 0 is the position of ID
            validRowsArray.splice(validRowsArray[x], 1);
        }
    }
}

you are using a property of the objects in the second array as an argument for which object to splice :

myArray.splice(mySecondArray[x].ID, 1);

but the first argument you should pass to splice is the index of the object you want to remove inside its encompassing array.

in pseudocode, your algorithm should do this :

for each item in array_a
  look for a match in array b
  determine the index of the matching object in array b
  splice one item at that index from array b

myArray.splice(mySecondArray[x].ID, 1); could be any value and has nothing to do with index in array needed for splice()

Assuming you know the index and it is x just use

myArray.splice(x, 1);

It is not clear in question how you defined x

I think you can use a splice, cause arent 2 arrays, is an object and one array so, Well here you have the answer, but when the array is not included, just add it at the end, else just replace rigth?, test this:

var myArray = [1,Firstname,Lastname]; //array user*
var mySecondArray = [object, object, object]; //array with objects(user)

Array.prototype.insertArray = function ( myArray ){
    var addMyArray = true;
    for(var i=0; i<this.length; i++){
        //compare 2 Id's
        if(this[i].Id == myArray[0]){ //if equals -> replace
            this[i].FirstName = myArray[1];
            this[i].LastName = myArray[2];
            addMyArray = false;
        }
    }
    //in case you array is not in your 'secondarray' it should be added right?
    if(addMyArray){
        this.push( {Id:myArray[0], FirstName:myArray[1], LastName:myArray[2]} );
    }
}

//Note: call your function after write prototype always

mySecondArray.insertArray( myArray );
//check right answer :)
console.log(mySecondArray)

use a reverse for loop to make sure you dont miss elements while removing.

Then use some to check if the current element exists in the invalid array.

splice using the index as the first param, then count as the second.

 var invalidRowsArray = [ [2, 'foo2', 'bar2'] ]; var validRowsArray = [{ Id: 1, FirstName: 'foo', LastName: 'bar' }, { Id: 2, FirstName: 'foo2', LastName: 'bar2' }, { Id: 3, FirstName: 'foo3', LastName: 'bar3' }]; for (var i = validRowsArray.length; i--;) { var exist = invalidRowsArray.some(function(element) { return element[0] == validRowsArray[i].Id; }) if (exist) validRowsArray.splice(i, 1) } console.log(validRowsArray); 

As you stated, 'I need to remove the object if it has the same id from the first array' shouldn't the first array consist of only numbers? Anyway, if you want to remove the object from the second array and you have the index, you can do it this way,

delete mySecondArray[x]

As myarray[x] == mySecondArray[x].Id working fiddle

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