I have indexed my array(posts) through the code below. The problem is that I need to index them in a reverse manner (from larger to lower) since my data is displayed from the newest to oldest so I want the newest post get the last index no. in the array which now is the opposite(newest get no 0). Also, how can I start indexing from no. 1 not 0 any ideas?
this.posts.forEach((post, index) => {post.index = index})
Now what I understood as per the above comments, You want to index the newest post with last index and oldest with the first index. If Yes, You can do this by using Array.map() :
const posts = ['newest', 'old', 'oldest'];
const res = posts.map((post, index) => {
return {
name: post,
index: posts.length - index
}
});
console.log(posts); // original array without mutate
console.log(res); // New array of objects with index properties