F
F
Fedor2021-06-30 19:40:35
JavaScript
Fedor, 2021-06-30 19:40:35

How to dynamically add value for id?

Hello! I have input. The data from which is written to an array of objects:

function() {
            this.listValue.push({'title': this.value})
}

I want to add a unique id for each entry in order to manipulate them in the future.

How to add unique value for id key?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2021-06-30
@Mecitan

function() {
  this.listValue.push({
    'title': this.value,
    'id': `Habr-id-${this.listValue.length}`
  })
}

S
szQocks, 2021-06-30
@szQocks

Googled?Math.random().toString(36).slice(2)

A
Andrew Hecc, 2021-06-30
@Hecc

Download the library:
https://www.npmjs.com/package/uniqid
If you want a native solution, you can use timestamp as a relatively unique value.
But it will not work if more than 1 object is generated in 1 ms. ps Well, in general, it is ideal that the id be generated by the backend when creating an entity, if such a creation occurs
new Date().getTime();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question