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?
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