函数从数组中删除单个项目或数组 - javascript [英] function to remove single items or arrays from within an array - javascript

查看:134
本文介绍了函数从数组中删除单个项目或数组 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单个项目和一些数组的数组:

  var groceries = [toothpaste,[李子,桃子,菠萝,胡萝卜,玉米,青豆],橙汁,巧克力,冰淇淋,纸巾 ]; 

我想编写一个函数,在调用时删除其中的一些项目或数组。



对于单个项目,我写了这个函数:

  function removeItems(arr, item){
for(var i = 0; i< item.length; i ++){
var index = arr.indexOf(item [i]);
if(index> -1){
arr.splice(index,1);
}
}
};

然后我可以打电话:

  removeItems(groceries,[fish,orange juice]); 

这个工作正如我所希望的那样。然而,我无法弄清楚如何使这个函数能够从更大的阵列中移除数组(例如[巧克力,冰淇淋))。我会怎么做呢?

解决方案

使用Array#reduce创建密钥字典。如果该项目是一个数组,则加入所有值以创建密钥。之后,通过生成相同的键过滤所有项目,并根据字典对其进行检查。



注意 - Array#过滤器会创建一个新数组,并且不会更改原始数组。



var groceries = [toothpaste,[plums,桃子,菠萝,胡萝卜,玉米,青豆],橙汁,巧克力,冰淇淋,纸巾,鱼];功能removeItems(arr,toRemove){function createKey(item){return Array.isArray(item)? item.join(' - '):item; } var removeDict = toRemove.reduce(function(d,item){var key = createKey(item); d [key] = true; return d;},{});返回arr.filter(函数(item){var key = createKey(item); return!removeDict [key];});} var result = removeItems(groceries,[fish,orange juice,[chocolate ,ice cream]]); console.log(result);

I have an array that contains some single items and some arrays:

var groceries = ["toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish"];

I want to write a function to remove some of these items or arrays when called.

For the single items, I wrote this function:

function removeItems(arr, item) {
  for ( var i = 0; i < item.length; i++ ) {
    var index = arr.indexOf(item[i]);
    if (index > -1) {
      arr.splice(index, 1);
    }
  }
};

So then I can call:

removeItems(groceries, ["fish", "orange juice"]);

And this works exactly as I want it to. However, I can't figure out how to make the function also be able to remove the arrays (e.g. ["chocolate", "ice cream"]) from within the larger array. How would I go about that?

解决方案

Use Array#reduce to create a dictionary of keys. If the item is an array, join all values to create the key. Afterwards filter all items by generating the same keys, and checking them against the dictionary.

Note - Array#filter creates a new array, and doesn't change the original array.

var groceries = ["toothpaste", ["plums", "peaches", "pineapples"], ["carrots", "corn", "green beans"], "orange juice", ["chocolate", "ice cream"], "paper towels", "fish"];

function removeItems(arr, toRemove) {
  function createKey(item) {
    return Array.isArray(item) ? item.join('-') : item;
  }

  var removeDict = toRemove.reduce(function(d, item) {
    var key = createKey(item);
    
    d[key] = true;
    
    return d;
  }, {});

  return arr.filter(function(item) {
    var key = createKey(item);
    
    return !removeDict[key];
  });
}

var result = removeItems(groceries, ["fish", "orange juice", ["chocolate", "ice cream"]]);

console.log(result);

这篇关于函数从数组中删除单个项目或数组 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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