0

Hello guys i have some static content on my website and want to make some dynamic content so I have 5 different news and want to give them unique ID for example while u prees on read more u must got

id:2 title: President-Elections

so I try to make some array

const posts = [
 {id: 1, title: "some title", content: "some content"},
 {id: 2, title: "some title2", content: "some content2"},
 {id: 3, title: "some title3", content: "some content3"},
 {id: 4, title: "some title4", content: "some content4"},
 {id: 4, title: "some title5", content: "some content5"},
];

Now I want to assing each post unique id but have not idea how to do.

Danny
  • 31
  • 1
  • 7

2 Answers2

2

You can use an uuid generator. https://www.npmjs.com/package/uuid

Errorname
  • 2,228
  • 13
  • 23
0

function generateUUID() { // Public Domain/MIT
    var d = new Date().getTime();
    if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
        d += performance.now(); //use high-precision timer if available
    }
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
}
Anthony Sobo
  • 377
  • 3
  • 4