简体   繁体   English

如何通过使用for循环从javascript中的变量中删除数组对象?

[英]How to remove array object from a variable in javascript by using for loop?

If the array position is not fixed, then how we remove the array ? 如果数组位置不固定,那么我们如何删除数组? I mean not using index/position. 我的意思是不使用索引/位置。 May be by using for loop. 可以通过使用for循环。 Position of the array is not fixed. 数组的位置不固定。

This sounds like a duplicate, if you want to remove an item from an array, in the loop you need to do check on if say the BankBranchId == 6, if it is the you want to remove the object from the array. 这听起来像是重复的,如果要从数组中删除项目,则在循环中需要检查是否说BankBranchId == 6,如果是,则要从数组中删除对象。

Which is shown here - 此处显示-

Remove Object from Array using JavaScript 使用JavaScript从数组中删除对象

Use splice to remove elements from an array, how to determine what is to be deleted. 使用拼接从数组中删除元素,以及如何确定要删除的元素。 I think your BankBranchId is unique, so here it goes. 我认为您的BankBranchId是唯一的,就这样。

var removeBBID = 6;
for(key in bankBranchReponse) {
  if(bankBranchReponse[key].BankBranchId == removeBBID) {
    bankBranchReponse.splice(key, 1);
    break;
  }
}

I agree with the link what @JessicPartridge has given as her answer and what and @KBN has mentioned in his answer but there checking is happening on only one value of array!! 我同意@JessicPartridge作为答案给出的链接,以及@KBN在其答案中提到的链接,但是仅对数组的一个值进行检查! Below code checks all the value and then removes array object from list! 下面的代码检查所有值,然后从列表中删除数组对象!

for(var i=0;i<bankBranchReponse.length;i++)
{
    if(bankBranchReponse[i].BankBranchId === removeVariable.BankBranchId && 
       bankBranchReponse[i].BankBranchName===removeVariable.BankBranchName &&
       bankBranchReponse[i].isPaymentMade===removeVariable.isPaymentMade)
    {
                        bankBranchReponse.pop(bankBranchReponse[i]);
    }
}

DEMO HERE 此处演示

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

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