Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Fetch an array of strings in javascript?

I get a list of tag names from the server and fill getedTags with data. my problem is data that it is a type of string while it must be an array , data value: "["HTML","CSS"]" but i need ["HTML","CSS"] how can i fetch an array of strings and add to getedTags variable?

var getedTags = [];
$.get(getTagurl,
    function (data) {

        getedTags = data;

    });
like image 691
Soheil Alizadeh Avatar asked Dec 11 '25 17:12

Soheil Alizadeh


2 Answers

You can use JSON.parse() from parse string to object.

var getedTags = [];
$.get(getTagurl,
function (data) {
    getedTags = JSON.parse(data);
});
like image 55
atiq1589 Avatar answered Dec 13 '25 07:12

atiq1589


You should convert the data you get from server into array. I presume the data type you are getting is JSON.

You can do JSON.parse(data) to convert in Object.

like image 22
Anurag Singh Bisht Avatar answered Dec 13 '25 06:12

Anurag Singh Bisht