Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript从数组中删除项(如果数组中已存在项)_Javascript - Fatal编程技术网

javascript从数组中删除项(如果数组中已存在项)

javascript从数组中删除项(如果数组中已存在项),javascript,Javascript,以下内容将向数组中添加项: var arrayOptions = []; function AddToFilterOptionList(mode) { arrayOptions.push(mode); } 从阵列中删除项: function RemoveFromFilterOptionList(mode) { var index = arrayOptions.indexOf(mode); if (index !== -1) { arrayOpt

以下内容将向数组中添加项:

var arrayOptions = [];

function AddToFilterOptionList(mode) {
    arrayOptions.push(mode);
    }
从阵列中删除项:

function RemoveFromFilterOptionList(mode) {
    var index = arrayOptions.indexOf(mode);
    if (index !== -1) {
        arrayOptions.splice(index, 1);
    }}
例如,如果我打电话

AddToFilterOptionList('APPLE')
-
APPLE
应添加到数组中

如果我再打电话

AddToFilterOptionList(“苹果+水果”)
-它应该从数组
arrayOptions
中删除项目“APPLE”,并且应该添加
APPLE+水果

任何时候,数组中只能有一个以
APPLE
开头的单词。 如何在javascript中找到类似“APPLE”的单词


我尝试了
Match()
,它返回匹配的单词
IndexOf()
仅当整个单词匹配但不是单词的开头时才返回1。

在数组中循环,然后使用startsWith方法

void AddToFilterOptionList(String mode) {
    for (i=0; i<arrayOptions.length; i++) {
      if (mode.startsWith(arrayOptions[i] == 1)) {
        array[i] = mode;
        return;  // found, so return
      }
    }
    arrayOptions.push(mode); // should only get here if string did not exist.
 }
void AddToFilterOptionList(字符串模式){

对于(i=0;i循环遍历数组,然后使用startsWith方法

void AddToFilterOptionList(String mode) {
    for (i=0; i<arrayOptions.length; i++) {
      if (mode.startsWith(arrayOptions[i] == 1)) {
        array[i] = mode;
        return;  // found, so return
      }
    }
    arrayOptions.push(mode); // should only get here if string did not exist.
 }
void AddToFilterOptionList(字符串模式){

对于(i=0;i,假设
'This+that'
模式是一致的,并且我们只关心起始项,那么这将起作用

var arr=[];
功能移除(项目){
var f=item.split('+')[0];
对于(变量i=0,e=arr.length;i
假设
'This+that'
模式是一致的,并且我们只关心起始项,这将起作用

var arr=[];
功能移除(项目){
var f=item.split('+')[0];
对于(变量i=0,e=arr.length;i
更新:

function add (array, fruits) {
  var firstFruit = fruits.split('+')[0]
  var secondFruit = fruits.split('+')[1]
  var found = false
  var output = []

  output = array.map(function (item) {
    if (item.indexOf(firstFruit) > -1) {
      found = true
      return fruits
    }
    else return item
  })

  if (! found) {
    array.push(fruits)
  }

  return output
}

var fruits = []
add(fruits, 'APPLE')
fruits = add(fruits, 'APPLE+GRAPE')
console.log(fruits[0]) // 'APPLE+GRAPE'
fruits = add(fruits, 'APPLE')
console.log(fruits[0]) // 'APPLE'
更新:

function add (array, fruits) {
  var firstFruit = fruits.split('+')[0]
  var secondFruit = fruits.split('+')[1]
  var found = false
  var output = []

  output = array.map(function (item) {
    if (item.indexOf(firstFruit) > -1) {
      found = true
      return fruits
    }
    else return item
  })

  if (! found) {
    array.push(fruits)
  }

  return output
}

var fruits = []
add(fruits, 'APPLE')
fruits = add(fruits, 'APPLE+GRAPE')
console.log(fruits[0]) // 'APPLE+GRAPE'
fruits = add(fruits, 'APPLE')
console.log(fruits[0]) // 'APPLE'

您需要按
+
字符分割,然后循环生成的数组以添加/删除所有项:

var arrayOptions=[];
函数AddToFilterOptionList(模式){
mode.split(/\+/g).forEach(函数(el){
var指数=arrayOptions.indexOf(el);
如果(索引!=-1){
阵列选项。拼接(索引,1);
}
否则{
阵列选项。推送(el);
}
});
}
函数RemoveFromFilterOptionList(模式){
var指数=arrayOptions.indexOf(模式);
如果(索引!=-1){
阵列选项。拼接(索引,1);
}
}
AddToFilterOptionList(“苹果”);
document.write(“”+arrayOptions);//预期:APPLE
AddToFilterOptionList(“苹果+水果”);
document.write(“”+arrayOptions);//expect:FRUIT
AddToFilterOptionList(“苹果+水果+胡萝卜”);

document.write(“”+arrayOptions);//预期:苹果、胡萝卜
您需要按
+
字符分割,然后循环生成的数组以添加/删除所有项:

var arrayOptions=[];
函数AddToFilterOptionList(模式){
mode.split(/\+/g).forEach(函数(el){
var指数=arrayOptions.indexOf(el);
如果(索引!=-1){
阵列选项。拼接(索引,1);
}
否则{
阵列选项。推送(el);
}
});
}
函数RemoveFromFilterOptionList(模式){
var指数=arrayOptions.indexOf(模式);
如果(索引!=-1){
阵列选项。拼接(索引,1);
}
}
AddToFilterOptionList(“苹果”);
document.write(“”+arrayOptions);//预期:APPLE
AddToFilterOptionList(“苹果+水果”);
document.write(“”+arrayOptions);//expect:FRUIT
AddToFilterOptionList(“苹果+水果+胡萝卜”);

document.write(“”+arrayOptions);//预期:APPLE,CARROT
试试这个,虽然代码没有优化:p

<html>
    <head>
        <script src = "jquery-1.10.2.min.js"></script>
        <script type = "text/javascript">
            var itemList = [];  
            function addItem()
            {
                var item = $('#item').val();
                if(item != '' || item != 'undefined')
                {
                    if(itemList.length == 0)
                        itemList.push(item);
                    else
                    {
                        for(i=0;i<itemList.length;i++)
                        {
                            var splittedInputItems = [];
                            splittedInputItems = item.split("+");
                            var splittedListItems = [];
                            splittedListItems = itemList[i].split("+");
                            if(splittedListItems[0] == splittedInputItems[0])
                            {
                                itemList.splice(i,1);
                                itemList.push(item);
                                return;
                            }
                        }
                        itemList.push(item);
                    }
                }
            }
        </script>
    </head>
    <body>
        <input id="item" type = "text"/>
        <input type = "button" value="Add" onclick="addItem()">
    </body>
</html>

var itemList=[];
函数addItem()
{
var item=$('#item').val();
如果(项目!=''||项目!='未定义')
{
if(itemList.length==0)
itemList.push(项目);
其他的
{

对于(i=0;i试试这个,代码并没有得到优化:p

<html>
    <head>
        <script src = "jquery-1.10.2.min.js"></script>
        <script type = "text/javascript">
            var itemList = [];  
            function addItem()
            {
                var item = $('#item').val();
                if(item != '' || item != 'undefined')
                {
                    if(itemList.length == 0)
                        itemList.push(item);
                    else
                    {
                        for(i=0;i<itemList.length;i++)
                        {
                            var splittedInputItems = [];
                            splittedInputItems = item.split("+");
                            var splittedListItems = [];
                            splittedListItems = itemList[i].split("+");
                            if(splittedListItems[0] == splittedInputItems[0])
                            {
                                itemList.splice(i,1);
                                itemList.push(item);
                                return;
                            }
                        }
                        itemList.push(item);
                    }
                }
            }
        </script>
    </head>
    <body>
        <input id="item" type = "text"/>
        <input type = "button" value="Add" onclick="addItem()">
    </body>
</html>

var itemList=[];
函数addItem()
{
var item=$('#item').val();
如果(项目!=''||项目!='未定义')
{
if(itemList.length==0)
itemList.push(项目);
其他的
{
对于(i=0;i
let items=[1,2,3,2,4,5,2,7];
设项目=2;
for(设i=0;i
如果要从items数组中删除元素“2”,这是一种方法。

让items=[1,2,3,2,4,5,2,7];
设项目=2;
for(设i=0;i

如果要从items数组中删除元素“2”,这是一种方法。

您想知道如何筛选数组以查找包含给定字符串的数组元素吗?因此,搜索
['apple','grape柚子']
对于
'ap'
应该返回
0
1
,而
'ape'
应该只返回
1
?或者它纯粹是一个“以”开头的搜索?我假设你想要数组索引-或者你想要别的什么?只要关心单词开头的苹果,为什么不拆分你的
苹果+水果
字符串首先?如果第一个苹果+水果在数组中,那么当我添加苹果时,它应该删除苹果+水果。问题是当我尝试从数组中删除该项时。当添加
水果时,是否应该删除
苹果+水果
?您想知道如何过滤数组以查找包含给定字符串的数组元素吗?所以搜索
的数组>['apple','grape柚子']
对于
'ap'
应该