0

I have two logs:

console.log(res.shared);
console.log(res.shared[0]);

enter image description here

The only problem is, that the 0th element of the full array log is different from the 0th element logged on the second line of the same array! As seen in results:

enter image description here

I call this "Go home Javascript! You're drunk!"

EDIT: The text stringified as requested by @Zerkms and yes - they are as expected and as passed by PHP. However, the stringified and non-stringified versions are different within the same console.log:

console.log(res.shared, JSON.stringify(res.shared));



"[{"SharedCandidate":{"id":"5","user_id":"145","applied_job_id":"380","comment":"hello!","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"380","mobile":"2342342342","phone":"","email":"em2@mgpepe.com","cover_letter_text":"<p>\r\n\tsdfjasjkf klasfajkhsdhjkfajkhsfjkhajkshdfjhksajkfjkjkdfjka djfhkajs dhjfk ahjksd fjashjkd fashjdfhjasjhkdfjhksdfjasjkf klasfajkhsdhjkfajkhsfjkhajkshdfjhksajkfjkjkdfjka djfhkajs dhjfk ahjksd fjashjkd fashjdfhjasjhkdfjhk</p>\r\n","resume_id":"78","cover_letter_id":null,"first_name":"Petar","last_name":"petrov","modified":"2014-04-02 17:19:30","CoverLetter":[],"Resume":{"id":"78","original_filename":"test","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":true,"dateJSformat":1396469940}},{"SharedCandidate":{"id":"6","user_id":"145","applied_job_id":"430","comment":"asdfsadf","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"7","user_id":"4","applied_job_id":"430","comment":"gasdfas","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"8","user_id":"4","applied_job_id":"430","comment":"hello","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"9","user_id":"4","applied_job_id":"430","comment":"iiii","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"10","user_id":"4","applied_job_id":"430","comment":"3444","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"11","user_id":"150","applied_job_id":"430","comment":"etradfsd","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"12","user_id":"221","applied_job_id":"430","comment":"heco","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}},{"SharedCandidate":{"id":"12","user_id":"221","applied_job_id":"430","comment":"heco","sharedby_user_id":"142"},"UserSharer":{"id":"142","username":"petarpetrov8","email":"pp4@mgpepe.com","first_name":"petar","last_name":"petrov","password":"64a55abeffc6a25be0e984550d9608b2b78f98ab","verification_code":"52ee812c-20b0-4b4e-81d3-098152d40771","status":"1","company":null,"company_prefill_compose":"0","address":"","address_prefill_compose":"0","state":"9","city":"1720","last_search_location":"11201","last_search_term":"24","phone":null,"zipcode":"95052","created":"2013-09-02 09:22:55","modified":"2014-06-20 13:29:40","last_login":null,"term":"1","inbox_page_limit":"5","available_posting":"0","secondary_emails":null,"is_admin":"0","is_jobseeker":"1","is_employer":"0","lat":"50.6133","lng":"28.7679","home_is_jobsbox":"0","confirm_delete_applicant":"1","do_not_show_saved_searches":"0","send_share_candidate_notification":null,"email_confirmation_token":null,"full_name":"petar petrov"},"AppliedJob":{"id":"430","mobile":"2546875786","phone":"","email":"pp4@mgpepe.com","cover_letter_text":"","resume_id":"66","cover_letter_id":"7","first_name":"iasdf","last_name":"ivanov","modified":"2014-05-19 11:07:46","CoverLetter":{"id":"7","original_filename":"Ribbit Capital Btc Piece - October 2013","extension":"pdf"},"Resume":{"id":"66","original_filename":"Bitcoin Assessment BAML 2013-12-05","extension":"pdf"},"LikedCandidate":[],"show_text_submissions":false,"dateJSformat":1400508420}}]"

EDIT 2: If I output the full thing after line 556 again, the result is identical to line 555.

EDIT 3: If I output the element only, both stringified and non-stringified version are the same:

console.log(res.shared[0], JSON.stringify(res.shared[0]));
mgPePe
  • 5,677
  • 12
  • 52
  • 85
  • did something else modify the array? – Daniel A. White Jul 20 '14 at 21:39
  • How could it? There is no code between the two logs... – mgPePe Jul 20 '14 at 21:40
  • the prototype doesn't look right. – Daniel A. White Jul 20 '14 at 21:40
  • Could it be that you have an item which has a name/key of `'0'`? In that case, the element with key `['0']` might be different from the first element `[0]`. – GolezTrol Jul 20 '14 at 21:40
  • 1
    What is `typeof res.shared`? – Chris Hayes Jul 20 '14 at 21:41
  • 5
    Keep in mind that, at least on some browsers, the console's evaluation of objects will be *dynamic*. The object view you expand last will have the most up-to-date information in these conditions. – Frédéric Hamidi Jul 20 '14 at 21:41
  • @ChrisHayes `console.log(typeof(res.shared));` shows `object`. – mgPePe Jul 20 '14 at 21:43
  • @GolezTrol From the image it looks like those are valid numbers, not strings. Strings would have colored red i believe. – mgPePe Jul 20 '14 at 21:44
  • To make it more obvious - output `console.log(res.shared, JSON.stringify(res.shared));`. The former will be lazy, the latter - instant – zerkms Jul 20 '14 at 21:44
  • @DanielA.White can you explain what you said about prototype please? – mgPePe Jul 20 '14 at 21:46
  • can you log ``res.shared`` again after ``console.log(res.shared[0]);`` and verify the values? – Sarbbottam Jul 20 '14 at 21:46
  • @mgPePe I don't think that counts for those indexes. Look at the other keynames/properties, like AppliedJob, FullName and proto. They're all purple. I'm talking about the keys themselves, not the values. – GolezTrol Jul 20 '14 at 21:48
  • "The text stringified as requested in comments" --- so? Is the stringified value expected? – zerkms Jul 20 '14 at 21:49
  • @zerkms Yes, they are as passed by PHP. It's the 556 that is showing the wrong thing. – mgPePe Jul 20 '14 at 21:52
  • "the result is identical to line 555" --- of course it is - it is lazily evaluated on first unfold event – zerkms Jul 20 '14 at 21:52
  • @mgPePe: now you know that you need to check stringified value to get the instant value as it was at that moment. Why don't you just apply it to the line 556? – zerkms Jul 20 '14 at 21:52
  • @zerkms ``fullName`` value is different. – Sarbbottam Jul 20 '14 at 21:53
  • @sarbbottam: OP confirmed (2 comments above) it is as expected. – zerkms Jul 20 '14 at 21:53
  • Wait, @zerkms, I might have mislead you, the stringified value is correct as expected, but the non-stringified value on the same line is wrong. – mgPePe Jul 20 '14 at 21:57
  • 1
    @mgPePe: you've been told **multiple times** here - that the values (references) in console are lazily resolved. Not sure if one needs to tell it once again, but let's try: the value of a reference is resolved with the value as it is on the moment you're expanding an element. – zerkms Jul 20 '14 at 21:58
  • 1
    Anyone voting to close please vote as duplicate as this is a valid question. – slebetman Jul 20 '14 at 21:58
  • Okay, got it. Should I just delete the question? – mgPePe Jul 20 '14 at 22:01

1 Answers1

4

The console itself shows the state of the object not at the time of the log, but at the time of expanding, that is, at the time you click on it to show the inner contents.

So the full array as well as the first element you see in the console are not the state at the time of the log (there, only the reference to the object is remembered), but it shows the state at the time you clicked on it. There was plenty of time to change it in between the click on full array and the click on element.

  • 2
    You should probably add "Use `console.log(JSON.stringify(obj, null, 4));` to print out the values of `obj` instantly". – soktinpk Jul 20 '14 at 21:59
  • 1
    Or just leave your comment here ;-) with the ado that if your object is very deep, use `2` instead of `4`, and if it has cyclical references, you're screwed. –  Jul 20 '14 at 22:01