0

I am trying to get a classes name through the name of a constructor in a class similar to what they have as the answer for this ask How to get a JavaScript object's class?

What I want to do, is find the item with the id of 0 and get the second constructor of name

class item {
    constructor(id, name, price) {
        this.id = id
        this.name = name
        this.price = price
    }
const Bar = new item(0, "Candy bar", "$1") 
const Gum = new item(1, "Gum, "$.5")

I need to be able to grab, any id such in this case 1, and use it to find the item's name

So I want to do something close to

var thisItem = item.with.id = 1
console.log(thisItem.name)
  • Why would it be `0.constructor.name`? What's `0`? It's not clear what you're trying to do – evolutionxbox Apr 30 '21 at 16:19
  • `0` is the ID of what? – evolutionxbox Apr 30 '21 at 16:19
  • I edidted it to add a new item, it would be the id of "Candy Bar" – Zain Wilson - WCH Student Apr 30 '21 at 16:20
  • The string `"Candy Bar"` does not have an `id`. It's unclear what problem you're trying to solve--the `id` of that *instance* of `item` would be `someItemInstance.id`. The question you link to discusses ways to get the class of that `item` instance--but again, it's not clear what problem you're trying to solve or what exactly you want. – Dave Newton Apr 30 '21 at 16:21
  • Assign the new item to a variable? `let bar = new item(...)` then you can access any property you like `console.log(bar.id, bar.name, ...)` – evolutionxbox Apr 30 '21 at 16:21
  • Now what I want to use this for is to create a bunch of items, and then be able to sort through all the items, and make a random array using all of these items id, so I can call them later. I will edit my question for that – Zain Wilson - WCH Student Apr 30 '21 at 16:24
  • I'm confused, do you want to get the name of the class of the object? – First dev Apr 30 '21 at 16:26
  • If you have an array of `item`s you can get an array of their `id` like `listOfItems.map(i => i.id)`. It might be a good idea to take a step back and check out some JS tutorials and docs; the basics of JS OOP are well-documented, and not particularly suitable for SO questions. I don't see how getting the name of an instance's class is related to this at all. – Dave Newton Apr 30 '21 at 16:26
  • Why can't you do exactly what you've written? `thisItem.name` is the name property of that item. – Dave Newton Apr 30 '21 at 16:28
  • I need to find the item, sorry I had it in there last edit, but I had a type with the ``` – Zain Wilson - WCH Student Apr 30 '21 at 16:30
  • 1
    If you want to keep a collection of `item` instances then you need to do that, either as an array (relatively slow searching), an object (e.g., property name is the `id`, property value is the instance), a `Map`, etc. Seriously, though--I would approach a problem like this via a tutorial or two first. There is no magic pile of instances to pull from unless you create it. – Dave Newton Apr 30 '21 at 16:31
  • There, I have created it, sorry I just hadn't of thought of putting it through an array, if you want to comment on my answer below to see how I could improve it – Zain Wilson - WCH Student Apr 30 '21 at 16:33

2 Answers2

1

You could just do that using Map it allows you to save the data with key and value like this

and it has a lot of functionality that may help like has, get and so on

const data = new Map();

data.set(1, {name: 'john', price: 145});
data.set(2, {name: 'taylor', price: 20});

if(data.has(1)){
 console.log(data.get(1).name);
}


data.forEach((el) => {
   console.log(el);
});
Joseph
  • 5,644
  • 3
  • 18
  • 44
  • 1
    Thank you for this, but quick question what does the ```el``` mean in ```data.forEach((el) =>``` – Zain Wilson - WCH Student Apr 30 '21 at 16:50
  • it just a variable name refer to each element while looping through the map you could change it as you want – Joseph Apr 30 '21 at 16:51
  • Ah i get it, so it just respresents each element in the ```data``` function – Zain Wilson - WCH Student Apr 30 '21 at 16:52
  • Also, I was trying to create a store like thing, but the fact that you are putting a price on people is funny to me – Zain Wilson - WCH Student Apr 30 '21 at 16:53
  • you could customize the value of each element as you want i just create a silly object as a value just for testing purposes – Joseph Apr 30 '21 at 16:54
  • 1
    @ZainWilson-WCHStudent Questions like "what does this `forEach` mean" reinforce my encouragement to seek out some tutorials or look at documentation, e.g., the [`forEach` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). All of this information is readily available, and makes for a better first step than SO. – Dave Newton Apr 30 '21 at 17:06
  • @DaveNewton maybe he just not familiar with arrow function syntax – Joseph Apr 30 '21 at 17:09
  • 1
    Then the [arrow function docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) might also be useful. – Dave Newton Apr 30 '21 at 17:11
  • 1
    @DaveNewton Yes, I am trying to learn more, asking questions like that also give me the resources, and motivation to find more documentation. On this question, I had tried to do my own research but was unable to find many good resources. I have been checking out everything you have given me, but sometimes it is just easier and faster just to ask. – Zain Wilson - WCH Student Apr 30 '21 at 17:24
0

Sorry for all the confusion, @DaveNewton helped me figure it out

class item {
    constructor(id, name, price) {
        this.id = id
        this.name = name
        this.price = price
        items.push([id, name, price])
    }
}

i have this .push in the item class, and I can use the items[] now to sort through

  • 3
    I would recommend not doing the push inside the constructor. Instead have something else which decides when an instance is put in a collection? – evolutionxbox Apr 30 '21 at 16:34
  • Why do you not suggest putting it inside the constructor, is it because of efficiency, or other things similar to efficency – Zain Wilson - WCH Student Apr 30 '21 at 16:38
  • 2
    Accessing a (presumably) global variable gives `item` too much information about the environment in which it lives. All an `item` should be aware of is itself. There are multiple ways this could be handled; which makes the most sense depends on context. – Dave Newton Apr 30 '21 at 16:38