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.
CodePudding user response:
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])