javascript push returning number instead of object

Rbar :

I'm sure this is just some simple silly mistake that I'm missing, but can anyone tell me why 3 is being returned instead of [{ "method": 'popup', "minutes": ''}, {"method": 'email', "minutes": '10'}, {"method": 'popup', "minutes": '20'}];?

I made a jsfiddle so you can see as well: https://jsfiddle.net/qk10arb0/3/

HTML

<p>Click the button to add a new element to the array.</p>

<button onclick="addNewReminder()">Try it</button>

<p id="demo"></p>

Javascript

function addNewReminder(){
      var newReminder = {
        "method": 'popup',
        "minutes": '20'
      };

      var reminders = [{
                "method": 'popup',
                "minutes": ''
              }, {
                  "method": 'email',
                  "minutes": '10'
              }];

    reminders = reminders.push(newReminder);
    document.getElementById("demo").innerHTML = reminders;
}

Thanks!!!

kind user :

Array#push method works in situ, you don't have to assign it to a new variable. It won't return a new array, but will modify the original one and will return it's length. That's why you are getting 3 as the result.

To get the desired result, just call it, without assigning to any variable:

reminders.push(newReminder);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

JavaScript: function returning an object

From Javascript

Javascript Object push() function

From Java

Thymeleaf returning a String array instead of List<Object>

From Javascript

forEach returning a large number instead of the index

From PHP

Returning a object gets true instead of beeing an object

From Dev

R returning number instead of string

From Dev

Push object in Javascript

From Dev

Promise returning a function instead of an object

From Dev

Procedure returning 0 instead of higher number

From Dev

Why is my formula returning a number instead of a date?

From Dev

Laravel returning "Object" instead of Array?

From Dev

javascript sharedArrayBuffer and bitwise operations returning a 32bit instead of 16bit number

From Dev

Returning Future<Object> instead of Object

From Dev

Returning line number on match() Javascript

From Dev

Android - Integer[] returning a number instead of String value

From Dev

Javascript push into an object

From Dev

protractor expect statement returning number instead of text

From Dev

Javascript value returning not a number (NaN)

From Dev

JavaScript - returning [object Object]

From Dev

Console returning [Object object] instead of object members

From Dev

Javascript function returning undefined instead of number

From Dev

Returning a sequential number instead of timestamps

From Dev

Push array into JavaScript Object

From Dev

Javascript .push method returning an error

From Dev

Problem with Python returning a NoneType object instead of the Object

From Dev

ngrx selector not returning number but returning the entire state object instead

From Dev

Returning the month index/number instead of month name

From Dev

Why is it returning address instead of object?

From Dev

If Statement Returning Number Instead of Variable Name

Related Related

  1. 1

    JavaScript: function returning an object

  2. 2

    Javascript Object push() function

  3. 3

    Thymeleaf returning a String array instead of List<Object>

  4. 4

    forEach returning a large number instead of the index

  5. 5

    Returning a object gets true instead of beeing an object

  6. 6

    R returning number instead of string

  7. 7

    Push object in Javascript

  8. 8

    Promise returning a function instead of an object

  9. 9

    Procedure returning 0 instead of higher number

  10. 10

    Why is my formula returning a number instead of a date?

  11. 11

    Laravel returning "Object" instead of Array?

  12. 12

    javascript sharedArrayBuffer and bitwise operations returning a 32bit instead of 16bit number

  13. 13

    Returning Future<Object> instead of Object

  14. 14

    Returning line number on match() Javascript

  15. 15

    Android - Integer[] returning a number instead of String value

  16. 16

    Javascript push into an object

  17. 17

    protractor expect statement returning number instead of text

  18. 18

    Javascript value returning not a number (NaN)

  19. 19

    JavaScript - returning [object Object]

  20. 20

    Console returning [Object object] instead of object members

  21. 21

    Javascript function returning undefined instead of number

  22. 22

    Returning a sequential number instead of timestamps

  23. 23

    Push array into JavaScript Object

  24. 24

    Javascript .push method returning an error

  25. 25

    Problem with Python returning a NoneType object instead of the Object

  26. 26

    ngrx selector not returning number but returning the entire state object instead

  27. 27

    Returning the month index/number instead of month name

  28. 28

    Why is it returning address instead of object?

  29. 29

    If Statement Returning Number Instead of Variable Name

HotTag

Archive