Why am I getting undefined getting data from my json? [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
In my angularjs file I stringified my json result
console.log("details... "+JSON.stringify(response));
it is somewhat of this nature
{"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE",
when I try to console for firstname I get a surprising outcome of undefined
console.log("firstname... "+ response.data.data.firstname) ;
please what could be wrong

The response.data.data is an array, that's what the square brackets are []. You must first access the item itself before accessing the firstname attribute on that item.
For example, to get the first item, you would do
console.log(response.data.data[0].firstname)

Instead of:
console.log("firstname... "+ response.data.data.firstname) ;
Do:
console.log("firstname... "+ response.data.data[0].firstname) ;
This happens because firstname is a key to an object which is inside an array named data.

Your final data is an array. You have to specify index on that:
var response = {"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE"}]}};
console.log("firstname... "+ response.data.data[0].firstname)

Related

Unable to retrieve length of an entity of JSON data in JavaScript [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 3 years ago.
I'm supposed to read certain data from a JSON file :
I'm able to print the correct value for :
console.log('data1 ' + data.entry[0].id);
console.log('data2 ' + data.entry[0].content.properties.yyn);
However, I want to find the length of the properties instead of reading each entity inside properties. I tried:
console.log('data2 ' + data.entry[0].content.properties.length);
It did not work!
console.log('data2 ' + Object.keys(data.entry[0].content.properties).length);
If you want to know how many keys your object has, use Object.keys(obj).length
Properties is an object, not an array. You can count the length of its keys:
const numOfProps = Object.keys(data.entry[0].content.properties).length;

Get data from JS array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Probably very easy question, but I couldn't find solution.
How to get data from this object?
That how it looks in consolo.log()
UPDATE:
Thank you for you answers.
That what I used before and it worked, but when I try on this array it returns error.
console.log(array[1].data);
Output picture
UPDATE2:
So I tried to make it a text, but I couldn't.
console.log(tempArray);
console.log("String: " + tempArray.toString());
console.log("Stringify: " + JSON.stringify(tempArray));
Here is output:
Stringify attempt result
Maybe there is something wrong with how I create this array.
let tempArray = [];
And in the loop
tempArray.push({"id": id, "data": data.routes[0].geometry});
Thank you,
Dmitry
That's an array of objects, so you would get elements from it like so:
console.log(obj[i].data)
Where i is the element (numbered 0 through 2) that you want to access.

how to sort json data by date [duplicate]

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Closed 8 years ago.
i want to sort my json response by date ,here is the javascript function
function successCallback(responseObj){
$.each(responseObj.allRecom.recom,function(index){
var data=JSON.stringify(responseObj); //contains json response
alert(responseObj.allRecom.recom[index].recomDate);//it contains date value from response.
var date=new Date(responseObj.allRecom.recom[index].recomDate);
date=date.toLocaleString(); //converted to local date string
});
}
now what i want is whatever the response will be come ,it should be sorted by date.thanks
Put every object in an array and then call these function:
dates.sort(function(a,b){return a-b}); //asc
dates.sort(function(a,b){return b-a}); //desc
just change 'dates' to corresponding name of the array

Basic JQuery API call - how to specify part of the response? [duplicate]

This question already has answers here:
parsing out ajax json results
(3 answers)
Closed 9 years ago.
I'm very, very new to APIs (an hour in), and I'm just trying to get to the point where I can output a single part of an API response into console.log - and work from there.
Here's the working code which grabs all the data (for example, to display the last price in Bitcoin:
$.ajax({
url: "https://api.bitcoinaverage.com/ticker/all",
dataType: 'json',
success: function(results){
var gpbvalue = results;
console.log(gpbvalue);
}
});
And here's the data itself: https://api.bitcoinaverage.com/ticker/all
How would I specify just the 'last' value under GPB, rather than outputting the entire set of data?
Thank you so much for any help!
This is what you want.
console.log(results.GBP.last);
var gbpvalue = results.GBP.last;
or results["GBP"]["last"], both are equivalent.
The response is a JSON where the first key is a country code and the last value is a key under that. If you wanted to pick a specific last value you could access it like this:
console.log(results['AUD']['last']);
Or if you wanted all last keys you could do this:
for(key in results) {
console.log(results[key]['last']);
}
You can use dot notation but one of the keys 24h_avg is an invalid variable name (vars can't start with numbers) so named index notation is a better habit to get into.

Store array in jQuery cookie [duplicate]

This question already has answers here:
Create array in cookie with javascript
(10 answers)
Closed 9 years ago.
I am storing array in cookie through jQuery cookie plugin but each time I get it from cookie, it returns empty
I define array as:
var myArray = [];
myArray[ 0 ] = "hello";
myArray[ 1 ] = "world";
Set it in cookie
$.cookie('cookie-array', myArray);
But getting this cookie and printing, prints empty string
console.log($.cookie('cookie-array'));
EDIT:
Arrays define in other questions are object arrays no like this array I mentioned here. Also I dont want to user JSON library.
Have a look at:
https://code.google.com/p/cookies/
to store an array
$.cookie('COOKIE_NAME', escape(myArray.join(',')), {expires:1234});
to get it back
cookie=unescape($.cookie('COOKIE_NAME'))
myArray=cookie.split(',')

Categories