Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the values from array with object comma seperated

Hi i am having a array where i am trying to get the value from it, And not able to get the value in object below:

let array= [
      "from 05:04 - to 09:41, train 342 SEK, wifi",
      "Restid 4:37 tim, 0 byten",
      "från",
      "fr.",
      "342",
      "SEK",
      "Företagspris",
      "2 klass, 2 klass Lugn, 1 klass",
      "Snabbtåg, X 2000, Tåg 522"
    ]

From above array i am trying to access like array[0][0] to get first value, but not getting the value, Is there any way to access the values like this, If i use array[0] i am getting 0th like this from 05:04 – to 09:41, train 342 SEK, wifi Please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Arrays are zero-indexed, so you can access the first element in your array with array[0].

Then you can use String.prototype.split() to divide the string into an array of substrings.

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

let array = [
  "from 05:04 - to 09:41, train 342 SEK, wifi",
  "Restid 4:37 tim, 0 byten",
  "från",
  "fr.",
  "342",
  "SEK",
  "Företagspris",
  "2 klass, 2 klass Lugn, 1 klass",
  "Snabbtåg, X 2000, Tåg 522"
]

console.log(array[0].split(',')[0])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading