-3

I need to iterate over strings that are inside an array, to get a sub-string in each string.

Substrings are between "()"

Something like this..

let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];

//return
//'cat','red','apple'

How I could do that?

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Spartan88
  • 3
  • 1
  • 5
    Show us what code you have already written and where the problem you have comes in. Or are you just asking someone here to write it for you? – Matt Runion Sep 05 '18 at 19:16
  • 1
    Welcome to Stack Overflow! Here, you can learn [How to Ask](http://stackoverflow.com/help/how-to-ask) properly before you do so. When asking a question, be sure you are [on topic](http://stackoverflow.com/help/on-topic) and always try to provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) when possible. This way, it's more likely volunteers on SO can help you. – lealceldeiro Sep 05 '18 at 19:18
  • What have you tried? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Methods_2 might contain something which would help you. Or maybe regular expressions would be better. Either way, this is not a free write-my-code service. We'll _help_ you, we won't _do it for you_. Thanks. – ADyson Sep 05 '18 at 19:23

1 Answers1

0

You can do this using substring and lastIndexOf functions.

let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];

myArray.forEach(function(e){
    console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})
michaelitoh
  • 2,317
  • 14
  • 26