skip to Main Content
{"Adana, Türkiye", "Mersin, Akdeniz/Mersin, Türkiye", "Yozgat, Yozgat Merkez/Yozgat, Türkiye"}

How can I convert this data format to array?

For example:

["Adana, Türkiye",
 "Mersin, Akdeniz/Mersin, Türkiye",
 "Yozgat, Yozgat Merkez/Yozgat, Türkiye"]

I tried many methods but none worked. I do not think I saw any easy methods to do this.

2

Answers


  1. Chosen as BEST ANSWER

    const middlePoints = '{"Adana, Türkiye","Mersin, Akdeniz/Mersin, Türkiye","Yozgat, Yozgat Merkez/Yozgat, Türkiye"}'
    
    let dataArray = middlePoints.split('","');
    dataArray[0] = dataArray[0].slice(1);
    dataArray[dataArray.length - 1] = dataArray[dataArray.length - 1].slice(0, -1);
    const correctedData = dataArray.map(item => item.replace(/^"/, '').replace(/"$/, ''));
    console.log(correctedData)

    Hi guys convert it. I used this method but it could be hard way. If you know better answer waiting you good news.


  2. If your data is just a simple string and does not contain any other {} than the starting and ending ones, you could simply do

    let data = '{"Adana, Türkiye","Mersin, Akdeniz/Mersin, Türkiye","Yozgat, Yozgat Merkez/Yozgat, Türkiye"}';
    
    let d = JSON.parse(`[${data.replace(/[{}]/g, "")}]`);
    console.log(d);

    Ie, replace the curly braces {} with square brackets [] and do a JSON.parse

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search