简体   繁体   English

从另一个数组中删除基于数组的对象

[英]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. 我需要从第一个数组中删除具有相同id的对象。 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. 但是您应该传递给splice的第一个参数是要在其包含数组中删除的对象的索引

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() 可以是任何值,并且与splice()所需的数组索引无关

Assuming you know the index and it is x just use 假设您知道索引,并且它是x只需使用

myArray.splice(x, 1);

It is not clear in question how you defined x 目前尚不清楚您如何定义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: 我认为您可以使用一个接头,导致2个数组,是一个对象和一个数组,所以,在这里您可以找到答案,但是当不包括数组时,只需在最后添加它,否则只需替换rigth?这个:

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. 使用反向for循环可确保删除时不会错过任何元素。

Then use some to check if the current element exists in the invalid array. 然后使用some命令检查当前元素是否存在于无效数组中。

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? 如您所说,“如果第一个数组具有相同的ID,则需要删除该对象”,第一个数组不应该仅由数字组成吗? 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 作为myarray [x] == mySecondArray [x] .Id 工作提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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