简体   繁体   中英

How can i find specific new object of Class that has a constructor argument equal to input

I have a class called Country with 4 constructor parameters.

I then create some new countries from that class with specified values.

My question is, how can i create a method that can find and return the object with a this.value equal to the input in the method?

class Country {

  constructor(name, area, population, topdomain) {
    this.name = name;
    this.area = area;
    this.population = population;
    this.topdomain = topdomain;
  }

  static findCountry = domain => {
    /*Here is where the magic should happen. 
      If domain is in any of the countries below, then it should return the country name.
     */
  }
}

norway = new Country("Norway", 323802, 5320045, ".no");
sweden = new Country("Sweden", 450295, 9960487, ".se");
russia = new Country("Russia", 17098242, 142257519, ".ru");
china = new Country("China", 9596960, 1379302771, ".cn");

This function here should return "Norway":

Country.findCountry(".no");

For that to work, the class has to keep a list of all the created instances. As JS has no weak references, this will mean that none of the instances can ever be garbage collected (so be careful):

 static instances = [];

 constructor(/*...*/) {
   /*...*/
   Country.instances.push(this);
 }

 static findCountry = domain => {
  return this.instances.find(country => country.domain === domain);
 }

Don't come here to ask people to write your code ;)

class Country {
    constructor(name, area, population, topdomain) {
        this.name = name;
        this.area = area;
        this.population = population;
        this.topdomain = topdomain;
        Country._ALL.push(this);
    }
    static findBy(key, value) {
        let output = [];
        for ( let i in Country._ALL) {
            let c = Country._ALL[i];
            if (c.hasOwnProperty(key) && c[key] === value)
                output.push(c);
        }
        return output;
    }
}
Country._ALL = [];

WARNING! ES6 classes do not support static variables like static variable = []; If you want static class variables in ES6 you have to use ClassName.variable = []; after the class declaration.

Your class doesn't know about the 4 objects you instantiated somewhere. You need to put them in a collection (eg an array), and then explicitly reference that collection in your search method:

class Country {
  constructor(name, area, population, topdomain) {
    this.name = name;
    this.area = area;
    this.population = population;
    this.topdomain = topdomain;
  }

  static findCountry(domain) {
    return (knownCountries.find(country => country.topdomain == domain) || {}).name;
//          ^^^^^^^^^^^^^^
  }
}

const norway = new Country("Norway", 323802, 5320045, ".no");
const sweden = new Country("Sweden", 450295, 9960487, ".se");
const russia = new Country("Russia", 17098242, 142257519, ".ru");
const china = new Country("China", 9596960, 1379302771, ".cn");

const knownCountries = [norway, sweden, russia, china];
//    ^^^^^^^^^^^^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM