Joe Cook
Joe Cook

Reputation: 71

append the count to each row in JavaScript

I have the following code and when I want to add the count to the beginning of the texts const so it will display in the table row.

const getters = [item => item.count, item => formatDate(item.date), item => item.num, item => item.body];
var count = 0;
  for (let row of rows) {
    count = count + 1;
    const texts = getters.map(getter => getter(row));
    //alert(count + texts)
    const rowWithCount = count + texts;
    //alert(rowWithCount);
    resultsBody.appendChild(createRowWithTexts(rowWithCount));
  }
};

what I have does not work; it displays the count but messes up the other columns.

Upvotes: 0

Views: 154

Answers (1)

Nikita Chayka
Nikita Chayka

Reputation: 2137

When you do const rowWithCount = count + texts; you are adding number to array, in javascript it will resolve to a string, is that what you want? Or you looking to push count to your array, in this case you should do texts.push(count); and if you want for count to be first element in array you could do texts = [count].concat(texts)

Regarding number + array = string. Here is the example:

1 + [10,10] // resolves to "110,10"

Upvotes: 1

Related Questions