0

I am trying to create an object and have the name of each object be unique. The objects will have a name, a number and a second number that is null (this I intend to calculate later).

Is it possible to have an object named after a variable of 1 then at the end of the function increase the variable so that the next object is 2?

I am being alerted the value of the id number and it comes out as NaN

In my fiddle, I have a button to append each object in the array to a list so I can inspect them. They come out as [ object Object ].

Should this be an object of objects instead of an array of objects if I later want to use the number field in each object to perform calculations?

The format I have my sample object in is what I believe I want to stick with (unless there is a reason to do it better another way) because it follows the example on w3schools.

What am I doing wrong?

Fiddle here.

HTML:

<input type="text" placeholder="Name" id="name"> <input type="text" placeholder="Number" id="number">
    <br>
    <button id="makeObject">Make Object</button>
    <button id="show">Show Me The Objects</button>
        <ul id="list"></ul>

JavaScript:

/*Sample ideal object
 1 = {
    name: John Doe
    number: 52
    newNumber: null
 }
*/

var arrayOfObjects = [];
var id = 1;

$(document).ready(function(){

    $('#makeObject').on('click', function(){
        var number = (parseInt($('#Number').val()));
        var name = $('#Name').val();
        arrayOfObjects.push(id = {
            number: number, 
            name: name,
            newNumber: null
        });
        id++;
        alert("The id is now: " + id);
        $('#Number').val("");
        $('#Name').val("");
    });

    $('#show').on('click', function(){
        $('#list').html("");
        for (i = 0; i < arrayOfObjects.length; i++) {
          $('#list').append("<li>" + arrayOfObjects[i] + "</li>");  
        };
    });
});
j08691
  • 204,283
  • 31
  • 260
  • 272
adin
  • 783
  • 3
  • 13
  • 27
  • Object keys are strings no matter what you do, even if those strings represent numbers. Variables on the other hand are just arbitrary names used as a convenience to the programmer. What's your question? – tadman Nov 02 '15 at 17:44
  • 1
    Why don´t you use an array of objects? – juvian Nov 02 '15 at 17:44
  • JavaScript object names cannot begin with a number: http://stackoverflow.com/a/9337047/1028949 – jeffjenx Nov 02 '15 at 17:47
  • @Quantastical That's not an "object name" but a "variable name". – tadman Nov 02 '15 at 17:48
  • @tadman, you are correct. Variable names cannot begin with a number. – jeffjenx Nov 02 '15 at 17:49
  • You can use `_1` but not `1` – mplungjan Nov 02 '15 at 17:49
  • that is actually not the issue here. the problem is, that id is reassigned in the push statement to be an object instead of an int. So adding 1 to it makes the alerted outcome NaN. – Stefan Dochow Nov 02 '15 at 17:52
  • Is it possible to do something like OID1 and at the end of the function do something like ++ so that the next entry would be OID2? – adin Nov 02 '15 at 18:35

2 Answers2

1

What you are looking for would be an object key, not its name (which cannot start with a number as Quantastical states)

Anyway, your assignment is a little weird. This way shoud be the way you intended it:

arrayOfObjects[id] = {
                number: number, 
                name: name,
                newNumber: null
            };

have a look at http://jsfiddle.net/ej3z9ncd/3/ to confirm it's working

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Stefan Dochow
  • 1,454
  • 1
  • 10
  • 11
  • When I inspect the object by appending it to the list, two objects come up when I have added only one object and the object is undefined. Is there a way to make the names OID1, OID2, OID3, etc. and push that to the `arrayOfObjects`? As it is right now, I think it's making each object name `arrayOfObject1`, `arrayOfObject2` when really it should be creating unique identifiers and pushing it to a single array called `arrayOfObjects`. – adin Nov 02 '15 at 18:16
  • the undefined comes from the fact that your i starts at 1 instead of 0. – Stefan Dochow Nov 02 '15 at 18:24
  • I do not really get the object naming thing. what do you want to accomplish? If you need a unique ID, just store it as a property of the object and just use push to add another object to your array. – Stefan Dochow Nov 02 '15 at 18:31
0

Object names are strings.

I realized that the unique name doesn't matter for the object itself, I just went with "student." Instead, I put the name of the student, which is unique, as a field within the object.

Fiddle here.

<input type="text" id="name" placeholder="Name">
<input type="text" id="score" placeholder="Score">
<br>
<br>
<button id="push">Push to Array</button>
<button id="show">Show</button>
<button id="doMath">Do Math</button>
<ul id="list"></ul>
<p id="sum"></p>
<p id="mean"></p>

CSS:

input:hover {
    border: 1px solid black;
}

JavaScript:

var myArray = [];
var sumOfScores;
var mean;
$(document).ready(function () {
    $('#push').on('click', function () {
        var name = $('#name').val();
        var score = parseInt($('#score').val());
        myArray.push((student = {
            name: name,
            score: score,
            newScore: null
        }));
        console.log(student);
        $('#name').val("");
        $('#score').val("");
    });
    $('#show').on('click', function () {
        $('#list').html("");
        for (i = 0; i < myArray.length; i++) {
            $('#list').append("<li>" + myArray[i].name + " received a score of " + myArray[i].score + "</li>");
        };
    });
    $('#doMath').on('click', function(){
        sumOfScores = 0;
        for (i = 0; i < myArray.length; i++) {
            sumOfScores += myArray[i].score;
        };
        $('#sum').html("The sum is: " + sumOfScores);
        mean = (sumOfScores / myArray.length);
        $('#mean').html("The mean score is: " + mean);
    });
});
adin
  • 783
  • 3
  • 13
  • 27