Home > OS >  Get the Index Number of a array
Get the Index Number of a array

Time:03-11

So, Hey Stack Overflow community.

I have a question for all the people who know javascript, I have an array that looks like this

[
  'Grandmaster Clue Scroll: 12',
  'Red Dragon Scimitar: 1',
  'Offhand Red Dragon Scimitar: 7',
  'Red Dragon Scales: 66.5k'
]

I want to be able to "sell" these items. How would I one find the index based off of a search like grandmaster clue scroll?

CodePudding user response:

    [
  'Grandmaster Clue Scroll: 12',
  'Red Dragon Scimitar: 1',
  'Offhand Red Dragon Scimitar: 7',
  'Red Dragon Scales: 66.5k'
].filter(item=> item.toLowerCase().contains('grandmaster clue scroll'))

CodePudding user response:

Easiest way to reference a value from a selected element is to use an object literal and have the kay as the element you want to retrieve and the value as the return ed value

const items= {
  grandmasterClueScroll: 12,
  red Dragon Scimitar: 1,
  offhand Red Dragon Scimitar: ',
  redDragonScales: 66.5k
}

then you can access the values directly as eg items.grandmasterClueScroll or dynamically eg items[requestedItem];

console.log(items[grandmasterClueScroll])
  • Related