删除数组元素的JavaScript

删除数组元素的JavaScript

本文介绍了删除数组元素的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许按标题似乎是一个简单的问题,但我didn't知道该怎么做最短的标题我的问题。结果
我想删除的JavaScript数组中的元素,是的,但我所寻找的究竟是来自于JavaScript与其他数组的数组元素不匹配(也许它可能是冠军,但过大)删除。结果
例如:

 阵列A => [A B C D]
数组b => [B,D]
阵C = deleteMismatchedElements(A,B)
阵C(功能之后) - > [B,D]

我想,使用嵌套foreach循环也可能是可能的,但我不知道是否有更好的办法,一些作为原生实现的方法可以被称为或类似...

非常感谢你。


解决方案

 变种C = [];
对于(VAR I = 0; I< b.length个;我++){
    如果(A.indexOf(B〔Ⅰ〕)-1个){
        C.push(B [I]);
    }
}

这样做是

  1. 创建数组 C
  2. 的每个项目,运行在 B
  3. 如果 B [I] A ,将其添加到 C

Maybe by title seems a easy question, but I didn´t know how to do the shortest title for my question.
I want to delete elements from array on javascript, yes, but what I am looking for exactly is delete from array mismatched elements with other array on javascript (maybe it could be title, but too large).
For example:

Array A=> [a, b, c, d]
Array B=> [b,d]
Array C = deleteMismatchedElements(A,B)
Array C (after function)-> [b,d]

I suppose that using a nested foreach loop it could be possible, but I wonder if there is a better way, something as a "native" implemented method that could be called, or similar...

Thank you very much.

解决方案
var C = [];
for(var i = 0; i < B.length; i ++){
    if(A.indexOf(B[i]) > -1){
        C.push(B[i]);
    }
}

What this does is

  1. Creates array C
  2. Runs through each item in B
  3. if B[i] is in A, add it to C

这篇关于删除数组元素的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:22