neiza neiza - 4 months ago 7
Javascript Question

Capture a specific value from a json in an alert

I'm working on an angular project with php. I've encode the php results in json and in the json i'm trying to capture a spefic field

(fname)
data from the json but finding it hard to do

$http.get('http://localhost/vivalooks/app_ion/feeds.php').success(function(data){
console.log(data);
$scope.feeds=data;
alert(data.fname)


})


response from console

[{"fname":"Benjamin","lname":"Osei","up_ans":"Changed","exs":"new","total":"1"}]

Answer

so

alert(data[0].fname) 

NOT

alert(data.fname) 

because , data is an array .


Because, when i requested in comments to print the output of alert(typeof data+Array.isArray(data)) , you wrote : objecttrue : it means :

  • object : Object=> can be literal , array, something else
  • true : IS Array

Extra :

Yo loop through all fname , just, do this :

alert(data.map((e)=>e.fname).join(','));