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_Filter - Fatal编程技术网

Javascript 如何根据数组的值筛选数组

Javascript 如何根据数组的值筛选数组,javascript,arrays,filter,Javascript,Arrays,Filter,我正在创建一个王国之心角色的目录,我有一个关于如何过滤它们的问题 首先,我创建了构造类来制作角色和游戏 class character { constructor(name, gender, alive, race, description,debut, seriesAppearance) { this.name = name; this.gender = gender; this.alive = alive; this.

我正在创建一个王国之心角色的目录,我有一个关于如何过滤它们的问题

首先,我创建了构造类来制作角色和游戏

class character {
    constructor(name, gender, alive,  race, description,debut, seriesAppearance) {
        this.name = name;
        this.gender = gender;
        this.alive = alive;
        this.description = description;
        this.race = race
        this.debut = debut;
        this.seriesAppearance = seriesAppearance;
    }
}

class serie {
    constructor(name, year, chronology) {
        this.name = name;
        this.year = year;
        this.chronology = chronology;
    }
}

然后我创造了角色和游戏本身

let kh1 = new serie('Kingdom Hearts', '2002', '1')
let khcom = new serie('Kingdom Hearts: Chains of Memories', '2004', '2')
let kh2 = new serie('Kingdom Hearts 2', '2005', '3')

let sora = new character('Sora', 'Male', true, 'Human', 'The Keyblade Master and the protagonist', kh1.name, `${kh1.name}, ${khcom.name} and ${kh2.name}`)
let kairi = new character('Kairi', 'Female', true, 'Human', 'Sora and Riku lost friend', kh1.name, `${kh1.name} and ${kh2.name}`)
let riku = new character('Riku', 'Male', true, 'Human', 'Sasuke of Kingdom Hearts', kh1.name, `${kh1.name} and ${khcom.name}`)
let larxene = new character('Larxene', 'Female', true, 'Nobody', 'Blonde girl who has electrical powers', khcom.name, `${khcom.name} and ${kh2.name}`) 
let axel = new character('Axel', 'Male', true, 'Nobody', 'Man with red hair with fire powers', khcom.name, `${khcom.name} and ${kh2.name}`)
let marluxia = new character('Marluxia', 'Male', true, 'Nobody', 'Pink haired man with a scrythe', khcom.name, khcom.name)
let lexaeus = new character('Lexaeus', 'Male', true, 'Nobody', 'Strong guy who has powers to control the land', khcom.name, `${khcom.name} and ${kh2.name}`)
let vexen = new character('Vexen', 'Male', false, 'Nobody', 'Scientist who created the Riku´s replica', khcom.name, khcom.name)
let replicaRiku = new character('Riku Replica', 'Male', true, 'Replica of the riku that only thinks about protecting Namine', khcom.name, khcom.name)
let namine = new character('Namine', 'Female', true, 'Human', ' Blonde girl who controls people´s memories', khcom.name, khcom.name)
let ansem = new character('Ansem', 'Male', true, 'Nobody', 'Enemy who controlled riku´s mind', kh1.name, kh1.name)
然后我创建了数组来组织每种类型的字符和一个包含所有字符的常规数组

let series = [kh1, kh2, khcom]

let heroes = [sora, kairi, namine]
let enemies = [larxene, axel, marluxia, lexaeus, vexen, replicaRiku, ansem]
let ambiguous = [riku]

let characters = [heroes, enemies, ambiguous]
现在我想创建一个过滤器,它可以返回所有活着的角色的名字,或者人类的名字,或者男性的名字,所以我只对不起作用的人类创建了这个测试函数

  function human(race) {
      if (this.race == 'Human') {
        return this.name
      }
  }

var humans = heroes.filter(human);
如何使此功能正常工作

更新

此方法适用于简单数组

function human(hero) {
    return hero.race === 'Human';
}

const humanNames = heroes.filter(human).map(human => human.name);
如何使其在字符数组或数组数组上工作?

array.prototype.filter将一个项传递给回调,并只保留那些返回truthy值的元素。您正在查找以下回调函数:

功能人类英雄{ return hero.race===“人类”; } 如果您只对名称感兴趣,可以将英雄映射到名称数组:

const humanNames=heromes.filterhuman.maphuman=>human.name; Array.prototype.filter将一个项传递给回调,并只保留那些返回truthy值的元素。您正在查找以下回调函数:

功能人类英雄{ return hero.race===“人类”; } 如果您只对名称感兴趣,可以将英雄映射到名称数组:

const humanNames=heromes.filterhuman.maphuman=>human.name;
这对单个数组有效,但是如果我把字符放在数组中,它当然不会返回任何东西。如果你有一个数组,你会使用不同的数据结构,需要另一个解决方案来满足你的需要。你会是哪一个?你能在问题中包括你的案例吗?还有,你还对英雄的平面阵列感兴趣吗?那么,你真的有阵列吗?或者这不是一个以数组作为值的对象?这对单个数组有效,但是如果我把字符放在数组中,它当然不会返回任何东西。如果你有一个数组,你会使用不同的数据结构,需要另一个解决方案来满足你的需要。你会是哪一个?你能在问题中包括你的案例吗?还有,你还对英雄的平面阵列感兴趣吗?那么,你真的有阵列吗?或者,这不是一个数组作为对象的值,而不是创建大量的变量,考虑具有属性的对象。这使得组织和操作数据变得更加容易,而不是创建大量的变量,而是考虑具有属性的对象。这使得组织和操作数据变得更加容易。