如何根据其值过滤数组

时间:2020-05-09 01:17:38

标签: javascript arrays filter

我正在创建Kingdom Hearts人物目录,我对如何过滤它们有疑问

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

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);

如何使它在字符数组(数组数组)上工作?

1 个答案:

答案 0 :(得分:3)

Array.prototype.filter将项目传递给回调,仅保留那些返回真实值的元素。您正在寻找以下回调函数:

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

如果仅对名称感兴趣,可以将英雄映射到名称数组:

const humanNames = heroes.filter(human).map(human => human.name);