你怎么能找到并删除阵列的部分用字符串的参数? [英] How can you find and delete a section of an array with a parameter of a string?

查看:104
本文介绍了你怎么能找到并删除阵列的部分用字符串的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能找到一个JS数组的一个部分,并删除它吗?我的意思是,能找到一个字符串的函数,着眼于一个数组,认为如果节的一个酷似字符串,并删除部分。下面是我有:

  VAR阵列= ['A1','F4'];
功能findAndDelete(串){
    删除array.find(A1);
}

当我尝试它,什么都不会发生。似乎有一个语法错误。我该如何解决这个问题?


解决方案

  VAR阵列= ['A1','F4'];
功能findAndDelete(串){
    VAR指数= array.indexOf(字符串);
    如果(索引> -1){
      方法Array.splice(指数,1);
    }
}

在这里,我们可以使用拼接()阵列的方法,从中删除特定元素。

How can you find a section of a JS array and delete it? What I mean is a function that can find a string, looks at an array, sees if one of the "sections" are exactly like the string, and deletes that "section". Here's what I have:

var array = ['a1','f4'];
function findAndDelete(string){
    delete array.find('a1');
}

When I try it, nothing happens. It seems that there's a syntax error. How do I fix this?

解决方案

var array = ['a1','f4'];
function findAndDelete(string){
    var index = array.indexOf(string);
    if (index > -1) {
      array.splice(index, 1);
    }
}

Here we can use splice() method of array to delete specific element from it.

这篇关于你怎么能找到并删除阵列的部分用字符串的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆