1

My issue is with SCORES, when I console.log the entire object the scores part is somehow showing an extra set of brackets.

$(".submit").on("click", function() {

  var newRoommate = {
    "firstname": $('#first_name').val().trim(),
    "lastname": $('#last_name').val().trim(),
    "photo": $('#user_photo').val().trim(),
    scores: [
      $('#question_1').val(),
      $('#question_2').val(),
      $('#question_3').val(),
      $('#question_4').val(),
      $('#question_5').val(),
      $('#question_6').val(),
      $('#question_7').val(),
      $('#question_8').val(),
      $('#question_9').val(),
      $('#question_10').val()
    ],
    scoresInput
  };
  console.log(newRoommate); // .... scores[]: Array(10)

Why is it showing scores[]:?

Ideally, I want it to result in something like this when console.log.

{
    firstname:"Ricky",
    lastname:"Bobby",
    photo:"http://www.topcelebsjackets.com/wp-content/uploads/2017/03/Talladega-Nights-Wonder-Bread-Ricky-Bobby-Jacket-6-570x708.jpg",
    scores:[
        5,
        1,
        4,
        4,
        5,
        1,
        2,
        5,
        4,
        1
      ]
  }
fool-dev
  • 7,671
  • 9
  • 40
  • 54
  • 4
    That is just a visualization of arrays when you log them. It does not really *add* anything to the array itself. – str Jan 17 '18 at 08:32
  • You are looking for `console.log(JSON.stringify( newRoommate ) )` maybe? – Icepickle Jan 17 '18 at 08:34
  • Apparantly that is what your browser produces when it logs an array object. Could be the result of the browser first doing scores.toString(), but hard to tell from a distance. – Peter B Jan 17 '18 at 08:35

1 Answers1

1

Chrome, Firefox, NodeJS, etc. will have different result for the same console.log statement.

That is because console.log is not a function delivered by JS, but actually an implementation delivered differently with each environment that JS is running on. If you'd take the raw JS (for example, Chrome V8) you'd notice that console is not even defined (among others like document or window)

In your case, scores[]: Array(10) is actually saying that you have a scores key, which is an array, and has 10 elements.

Related: Throwing custom error doesn't preserve Error formatting in the log

Adelin
  • 7,809
  • 5
  • 37
  • 65