1

I'm creating a catalog of Kingdom Hearts characters and I have a question about how I can filter them

First I created construction classes to make the characters and games

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

Then I created the characters and the games themselves

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)

Then I created arrays to organize each type of character and a general array with all

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]

Now I would like to create a filter that could return all the names of living characters, or human, or male, so I created this test function only with humans that didn't work

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

var humans = heroes.filter(human);

how can I make this function work?

Update

this method works for simple arrays

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

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

How can I make it work on the characters array (an array of arrays)?

1
  • Instead of creating lots of variables, consider objects with properties. This makes it a lot easier to organize and manipulate your data. Commented May 9, 2020 at 1:37

1 Answer 1

3

Array.prototype.filter passes an item to the callback and leaves only those elements that return truthy value. You are looking for the following callback function:

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

If you're interested in names only, you can map your heroes to an array of names:

const humanNames = heroes.filter(human).map(human => human.name);
Sign up to request clarification or add additional context in comments.

5 Comments

this works on single arrays, but if I put the characters which is an array of arrays it doesn't return anything
Of course. If you have an array of arrays, you work with different data structure and need another solution to your needs.
which would it be?
Can you include your case in the question? Also, are you still interested in flat array of heroes or not?
So, do you really have an array of arrays? Or perhaps that't an object with arrays as values?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.