node.js - How to get loop variables in nodejs using for of to traverse Map
phpcn_u1582
phpcn_u1582 2017-06-14 10:52:45
0
2
1531

My problem is to prevent duplicate data from being inserted when adding data to the array, so I thought of map, but I don’t know how to get the index after traversing the Map structure. If I don't use map, is there any other concise way?

phpcn_u1582
phpcn_u1582

reply all(2)
滿天的星座

`for(value of map){

console.log(value[0]);

}`

学习ing

1. The simplest

Every time you insert it, use the unique key to check whether it already exists

2. Use Object method when storing

let data = {
    [id]: {
        ...obj,
        index: Number // 代表原来的索引
    }
}

If you want to sort things in order when calling, use index to sort and then output.

Object.values(data).sort((a, b) => a.index - b.index);

3. Create an index table while saving Array

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!