search

Home  >  Q&A  >  body text

node.js - How to get loop variables in nodejs using for of to traverse Map

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_u1582phpcn_u15822768 days ago1600

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-06-14 10:54:45

    `for(value of map){

    console.log(value[0]);

    }`

    reply
    0
  • 学习ing

    学习ing2017-06-14 10:54:45

    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

    reply
    0
  • Cancelreply