2

I have a Map that I had been told was better to use than a JS Array [].
Now, I'm working on debugging some of the data being parsed through.

const Csv2Map_Promise = (async (csvFilepath) => {
  return new Promise((resolve) => {
    const csvData = new Map();
    createReadStream(csvFilepath)
      .pipe(parse({ delimiter: ',' }))
      .on('data', (csvRow) => { csvData.set(csvRow[0], { 'value 1': 1, 'value 2': 2, 'value 3': 3, 'value 4': 4 }); })
      .on('end', () => { resolve(csvData); });
  });
})

const csvFile1_Map = Csv2Map_Promise('csvFile1.csv');

csvFile1_Map.then(()=>{console.log(csvFile1_Map);})

The files I'm debugging are really big.
How can I console.log just some of the data?
(Check like 10 or 100 returns)

P.S. I know I can convert back to an array,
But is there a better way? Or am I just fooling myself?

0

1 Answer 1

1

Since Map instance is an iterator, you can cycle through it and keep track of how many items you want to print:

let myMap = new Map();

for (let i = 1; i < 10; i++) {
  myMap.set(i, i * 10);
}

console.log("Full list of contacts:")
for (let [key, value] of myMap) {
  console.log(key + ' = ' + value)
};

console.log("3 items of contacts:")
let limit = 3
for (let [key, value] of myMap) {
  if (limit <= 0) {
    break
  }

  console.log(key + ' = ' + value)

  limit--
}

The worse idea is to simplify the code, but convert it to an array. Why is it worse? Because you need to load the whole array into memory (removing the advantage of an iterator), and only then take a slice of it:

let myMap = new Map();

for (let i = 1; i < 10; i++) {
  myMap.set(i, i * 10);
}

console.log("Full list of contacts:");
for (let [key, value] of myMap) {
  console.log(key + " = " + value);
}

console.log("3 items of contacts:");
for (let [key, value] of Array.from(myMap.entries()).slice(0, 3)) {
  console.log(key + " = " + value);
}

In case of your code, this cycling through the map will happen inside of .then block:

const makeMapPromise = (size) =>
  new Promise((resolve) => {
    let myMap = new Map();

    // I am creating a map of numbers as I cannot replicate your code
    // but it does not matter. What metters is that this code
    // populates the map somehow with some values
    for (let i = 1; i < size; i++) {
      myMap.set(i, i * 10);
    }

    resolve(myMap);
  });

const myMapPromise = makeMapPromise(10);

myMapPromise.then((myMap) => {
  console.log("Full list of contacts:");
  for (let [key, value] of myMap) {
    console.log(key + " = " + value);
  }

  console.log("3 items of contacts:");
  const arr = Array.from(myMap.entries()) // converting to an array
  const sliceOf3 = arr.slice(0, 3) // reslicing the array to get only first 3 elements
  for (let [key, value] of sliceOf3) {
    console.log(key + " = " + value);
  }
});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.