How to add an object literal to an array using a variable containing the name of the array

I am unable to get the new object literal into an array. When I use the name of the array, everything works. But when I switch the code to use a variable containing the name of the array, it does not work.

I have 6 arrays as shown below and they are listed on my page. Clicking saves the name of the array clicked in the variable named whichList.

Here are three arrays:

student0 = [
  {
    status: "completed",
    goal: "go to store",
    duedate: "November 1",
    datecreated: ""
  }, {
    status: "completed",
    goal: "buy beer",
    duedate: "November 2",
    datecreated: ""
  }
];

student1 = [
  {
    status: "completed",
    goal: "go to the beach",
    duedate: "November 7"
  }, {
    status: "completed",
    goal: "swim without drowning",
    duedate: "November 8",
    datecreated: ""
  }
];

student2 = [
  {
    status: "completed",
    goal: "fly a plane",
    duedate: "November 11",
    datecreated: ""
  }, {
    status: "completed",
    goal: "don't crash",
    duedate: "November 12",
    datecreated: ""
  }
];

Here's a working code that directly indicates the name of the array. It shows my updated array in the console after clicking:

$('#savegoal').click(function() {
  datecreated = new Date();
  student0[student0.length] = {
    status: "pending",
    goal: "\"" + $('#thegoal').val() + "\"",
    duedate: "\"" + $('#thedeadline').val() + "\"",
    datecreated: "\"" + datecreated + "\""
  };
  return console.log(student0);
});

The code is not working here. I want to use whichList variable.

console.log, , . . , , , , .

$('#savegoal').click(function() {
  datecreated = new Date();
  whichList[whichList.length] = {
    status: "pending",
    goal: "\"" + $('#thegoal').val() + "\"",
    duedate: "\"" + $('#thedeadline').val() + "\"",
    datecreated: "\"" + datecreated + "\""
  };
  return console.log(whichList);
});
+4
3

window[whichList], /:

var whichList = "student0";
var theList = window[whichList];
theList[theList.length] = { ... };   // consider using instead: theList.push( { ... }) 
+3

:

var wichList = null;
$('#savegoal').click(function() {
  if (wichList == null){
      alert('No list selected');
  }
  // don't forget the "var", otherwise "datecreated"
  // will actually be a global variable
  var datecreated = new Date();
  // ".push()" adds the given value at the end of the array
  wichList.push({
    status: "pending",
    goal: "\"" + $('#thegoal').val() + "\"",
    duedate: "\"" + $('#thedeadline').val() + "\"",
    datecreated: "\"" + datecreated + "\""
  });
  return console.log(wichList);
});

//somewhere else in your code :
if (somecond) {
    wichList = student0;
} elseif (somecond) {
    wichList = student1;
} else {
    wichList = null;
}

: javascript array

var a = [];
var b = a;
// b an a are now references to the same array,
// any modification to a will be "seen" by b, and vice versa :
a.push(1);
b.push(2);
// a == b == [1,2]

, "" , , , :

var students = [];

students[0] = [{
    status: "completed",
    goal: "go to store",
    duedate: "November 1",
    datecreated: ""
  }, {
    status: "completed",
    goal: "buy beer",
    duedate: "November 2",
    datecreated: ""
  }];

students[1] = [ ...

, , :

<select id="student">
    <option value="0">student 0</option>
    <option value="1">student 1</option>
    ...
</select>

click:

$('#savegoal').click(function() {
  var datecreated = new Date();
  var i = $('#student').val();
  students[i].push({
    status: "pending",
    goal: "\"" + $('#thegoal').val() + "\"",
    duedate: "\"" + $('#thedeadline').val() + "\"",
    datecreated: "\"" + datecreated + "\""
  });
  return console.log(students[i]);
});
0

First use an array instead of individual variables:

var student = [
    [{
        status: "completed",
        goal: "go to store",
        duedate: "November 1",
        datecreated: ""
    }, {
        status: "completed",
        goal: "buy beer",
        duedate: "November 2",
        datecreated: ""
    }],
    [{
        status: "completed",
        goal: "go to the beach",
        duedate: "November 7"
    }, {
        status: "completed",
        goal: "swim without drowning",
        duedate: "November 8",
        datecreated: ""
    }],
    [{
        status: "completed",
        goal: "fly a plane",
        duedate: "November 11",
        datecreated: ""
    }, {
        status: "completed",
        goal: "don't crash",
        duedate: "November 12",
        datecreated: ""
    }],
    ...
];

Then it whichListshould be an index in the array (i.e. a number from 0 to 5), not a variable name, and you can do:

$('#savegoal').click(function() {
    datecreated = new Date().toString();
    student[whichList].push({
        status: "pending",
        goal: $('#thegoal').val(),
        duedate: $('#thedeadline').val(),
        datecreated: datecreated
    });
});

You do not need to concatenate all of these "\""- quotation marks are part of the notation when typing string literals, you do not need them to get strings from variables or expressions.

Here you can do this if you want to use student IDs as a key to search for each student:

var student = {};
student[student0] = [
    {
        status: "completed",
        goal: "go to store",
        duedate: "November 1",
        datecreated: ""
    }, {
        status: "completed",
        goal: "buy beer",
        duedate: "November 2",
        datecreated: ""
    }];
student[student1] = [
    {
        status: "completed",
        goal: "go to the beach",
        duedate: "November 7"
    }, {
        status: "completed",
        goal: "swim without drowning",
        duedate: "November 8",
        datecreated: ""
    }];
student[student2] = [
    {
        status: "completed",
        goal: "fly a plane",
        duedate: "November 11",
        datecreated: ""
    }, {
        status: "completed",
        goal: "don't crash",
        duedate: "November 12",
        datecreated: ""
    }];

$('#savegoal').click(function() {
    datecreated = new Date();
    push(student[whichStudentId],{
        status: "pending",
        goal: $('#thegoal').val(),
        duedate: $('#thedeadline').val(),
        datecreated: datecreated
    });
    console.log(whichList);
});
-1
source

All Articles