How can i combinate push and update event for an array?

i tried with assign but that was not working

$(`#saveall${roomname}`).on('click',function() {
    var tisch = document.getElementById(`${roomname}_tisch`).value;
    item = {};
    item ["tisch"] = tisch;
    itemsnew.push(item);
});

the Problem what i have if i update the value i got a new entry :

    items: Array(2)
0: {tisch: "3"}
1: {tisch: "6"}

i will still update the value from tisch and not new entry in my array

2 Answers

0
WebRookie On

You can do it like so:

$(`#saveall${roomname}`).on('click',function() {
  var tisch = document.getElementById(`${roomname}_tisch`).value;
  itemsnew.push({
    tisch, // => this is equivalent to "tisch": tisch
    /* add more properties if you need to */
  });
});
1
deceze On

{tisch: tisch} creates the same object in one expression; in fact, in ES6 you can simply use the shorthand {tisch}. Given this:

itemsnew.push({tisch});