-4

I'm trying to get a value that's between, next or before of a comma.

For example, my string is: "value1,value2,value3,value4", sent to JS through PHP (ajax)

How do I just get value2 and value4 inside the .js file?

mega6382
  • 9,211
  • 17
  • 48
  • 69
Roberth
  • 95
  • 1
  • 9

3 Answers3

1

    let myString = "value1,value2,value3,value4"
    myString.split(',')
  console.log(myString.split(','))
zabusa
  • 2,520
  • 21
  • 25
0

let szResult = "val1,val2,val3,val4";
let arr = szResult.split(',');
console.log(arr[1]); // val2
console.log(arr[3]); // val4
adeltahir
  • 1,087
  • 2
  • 15
  • 31
0

Use split() function like that:

var arr = yourString.split(",")
// value2
return arr[1]
// value 3
return arr[3]

It's pretty simple, I don't think it is really issue for the question here, sorry.