ajax i have this snippet:
$("input#search_field").keyup(function(){
var searchText = $("input#search_field").val()
if(searchText.length > 1){
$.ajax({
url: 'search.php',
data: {data: JSON.stringify(searchText)},
type: 'POST',
dataType: "json",
success: function (data) {
if(data.result == 1) {
console.log(data.error);
}
if(data.result == 0) {
console.log(data.error)
}
}
});
}
});
When data.result is = 1, than the returned data.error is an array, in my console:
["string"]
My question is how get every string in my array into a different variable so i can use it later?
Because returned array could be also:
["string","string2","string3"]
Anyone knows?? Greetings!
data.result[0]
,data.result[1]
, anddata.result[2]
. Why do you need to move it to separate variables if you can already access them as-is?.length
to get the number of elements in the array (e.g.array.length
).