JavaScript 调试问答:如何 `console.log()` 映射?
JavaScript Debug Q&A: How do I `console.log()` Map?
有人告诉我 Map
比 JS Array []
更好用。
现在,我正在调试一些正在解析的数据。
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);})
我正在调试的文件非常大。
我怎样才能 console.log
只是一些数据?
(勾选 10 或 100 returns)
P.S。我知道我可以转换回数组,
但是有更好的方法吗?还是我在自欺欺人?
由于 Map 实例是一个迭代器,您可以循环遍历它并跟踪要打印的项目数:
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--
}
更糟糕的想法是简化代码,但将其转换为数组。为什么更糟?因为你需要将整个数组加载到内存中(消除了迭代器的优势),然后才取出它的一部分:
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);
}
对于您的代码,这种遍历地图的循环将发生在 .then
块内:
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);
}
});
有人告诉我 Map
比 JS Array []
更好用。
现在,我正在调试一些正在解析的数据。
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);})
我正在调试的文件非常大。
我怎样才能 console.log
只是一些数据?
(勾选 10 或 100 returns)
P.S。我知道我可以转换回数组,
但是有更好的方法吗?还是我在自欺欺人?
由于 Map 实例是一个迭代器,您可以循环遍历它并跟踪要打印的项目数:
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--
}
更糟糕的想法是简化代码,但将其转换为数组。为什么更糟?因为你需要将整个数组加载到内存中(消除了迭代器的优势),然后才取出它的一部分:
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);
}
对于您的代码,这种遍历地图的循环将发生在 .then
块内:
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);
}
});