Please help me with this issue I am facing while trying to split a string using split()
. It's causing duplicate values which is frustrating. Can someone please assist me?
The string I need to split is a comma-separated response from a database, and here is the echo:
echo $Title1 . ',' . $link. ','.$value. ','.'number'.','.'$' .',';
My code snippet is as follows:
function handleResponse(response2){
var str = response2;
var str_array = response2.split(',');
}
I'm seeing duplicates in the console log when I print str_array
:
0: "Butter cauliflower & paneer"
1: "https://link1.com"
2: "4"
3: "friends5"
4: "$"
5: "Butter cauliflower & paneer"
6: "https://link1.com"
7: "4"
8: "friends5"
9: "$"
10: "Butter cauliflower and coconut sambal"
11: "https://link2.com"
12: "3"
13: "friends5"
14: "$"
15: "Butter cauliflower and coconut sambal"
16: "https://link2.com"
17: "3"
18: "friends5"
19: "$"
Here is the actual value of response2:
Butter cauliflower & paneer,https://link1.com,4,friends5,$,Butter cauliflower & paneer,https://link2.com,4,friends5,$,Butter cauliflower and coconut sambal,https://link3.com,3,friends5,$,Butter cauliflower and coconut sambal,https://link4.com,3,friends5,$,Creamy vegetarian pumpkin curry,https://link5.com,5,friends5,$,
Can you help me understand why `split()` is behaving this way?