How to insert serial numbers for every object in JavaScript

I am trying to make a javaScript game of housie/bingo game. I used the library npm tambola-generator which returns auto generated tickets.

The server side process generates an array similar to this one …

[{
  "_entries": [[0,17,0,0,41,53,0,78,86], [4,0,0,35,0,58,67,80,0]/*, [ ... ] */],
}, {
  "_entries": [[0,16,23,0,41,51,0,0,88], [2,20,0,31,43,56,0,0,0]/*, [ ... ] */],
}, {
  "_entries": [[0,0,23,33,0,51,0,73,87], [1,0,0,35,42,58,68,0,0]/*, [ ... ] */],
}, {
  // ... more items like that ...
}]

I want each ticket to feature a serial number which should be equal to a ticket items’s array index but starting with the base of 1 instead of 0.

How does one include such a ticket number so that I can manage tickets for players?

I am just a beginner I couldn’t solve it out. The target structure of the above example code should look like the following one …

[{
  "ticketNum": 1,
  "_entries": [[0,17,0,0,41,53,0,78,86], [4,0,0,35,0,58,67,80,0]/*, [ ... ] */],
}, {
  "ticketNum": 2,
  "_entries": [[0,16,23,0,41,51,0,0,88], [2,20,0,31,43,56,0,0,0]/*, [ ... ] */],
}, {
  "ticketNum": 3,
  "_entries": [[0,0,23,33,0,51,0,73,87], [1,0,0,35,42,58,68,0,0]/*, [ ... ] */],
}, {
  // ... more items like that ...
}]

Answer

Just use Array.prototype.map for achieving this task.

const entries = [{
  "_entries": [
    [0, 17, 0, 0, 41, 53, 0, 78, 86],
    [4, 0, 0, 35, 0, 58, 67, 80, 0]
  ],
}, {
  "_entries": [
    [0, 16, 23, 0, 41, 51, 0, 0, 88],
    [2, 20, 0, 31, 43, 56, 0, 0, 0]
  ],
}, {
  "_entries": [
    [0, 0, 23, 33, 0, 51, 0, 73, 87],
    [1, 0, 0, 35, 42, 58, 68, 0, 0]
  ],
}];

const tcktEntries = entries.map((entry, index) => {
  return {
    ticketNum: index + 1,
    ...entry
  };
});

console.log({
  tcktEntries
});
.as-console-wrapper {
  min-height: 100%!important;
  top: 0;
}