Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_Object - Fatal编程技术网

Javascript 如何按变量值从数组中删除对象?

Javascript 如何按变量值从数组中删除对象?,javascript,arrays,object,Javascript,Arrays,Object,我正在编写一个口袋妖怪生成器,并尝试在结果中排除某些类型 我已经“尝试”过拼接和过滤功能,但我的理解非常有限,所以我可能只是用错了 我有一个变量,里面有很多对象,比如 var pokemonChoices = [{ name: 'Bulbasaur', type: ["Grass","Poison"] }, { name: 'Charmander', type: ["Fire"] }]; 然后我有一个包含类型的下拉菜单和一个名为excludetype的变

我正在编写一个口袋妖怪生成器,并尝试在结果中排除某些类型

我已经“尝试”过拼接和过滤功能,但我的理解非常有限,所以我可能只是用错了

我有一个变量,里面有很多对象,比如

var pokemonChoices = [{
    name: 'Bulbasaur',
    type: ["Grass","Poison"]
  }, {
    name: 'Charmander',
    type: ["Fire"]
  }];
然后我有一个包含类型的下拉菜单和一个名为excludetype的变量,该变量从中获取其值

如何创建一个新数组,例如,排除类型为“Grass”的所有口袋妖怪,或者排除类型设置为的任何东西

================================

编辑:我想我已经用它工作了

  var Excludinator = pokemonChoices.filter(function( obj ) { return obj.type.includes(excludetype); }); 

  pokemonChoices = pokemonChoices.filter(function(item) {
  return !Excludinator.includes(item); 
  })

我相信这就是你想要实现的

// Array holding your Pokemon Objects
const pokemonChoices = [
  {
    name: 'Bulbasaur',
    type: ['Grass', 'Poison'],
  },
  {
    name: 'Charmander',
    type: ['Fire'],
  },
];

// Function where you pass in the excluded type and it filters out Pokemon that include it.
const searchPokemon = exclude => {
  return pokemonChoices.filter(pokemon => !pokemon.type.includes(exclude));
};

console.log(searchPokemon('Poison'));

您可以在这些链接上找到更多信息。

要获取所有包含
草的
类型的口袋妖怪,您可以使用
过滤器
包含

const pokemonChoices=[{name:'Bulbasaur',type:[“Grass”,“Poison”]},{name:'Charmander',type:[“Fire”]}];
const pokemonByType=t=>pokemonChoices.filter(({type})=>type.includes(t));
log(pokemonByType(“Grass”);
console.log(pokemonByType(“Fire”)

。作为控制台包装{max height:100%!important;top:auto;}
您的输入看起来格式不正确-您是真的在使用逗号运算符,还是打算将对象包围在
[]
数组中?发布的代码无效JavaScript@CertainPerformance-是的,我想用[]包装,现在编辑。我保证它在代码中是正确的,除了所需的过滤之外,其他一切都可以工作。我在这里写这个例子的时候把空格隔开了。所以基本上我在回答中给出了什么