最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can anyone tell me why the map size is zero? - Stack Overflow

programmeradmin5浏览0评论

I am learning data structures in JS and written some code to build Graph data structure.

But there seems to be an issue that I could not understand why it is happening.

Please look at the ments on getGraph() method. I am just printing the size of the list and the list itself here. list.size returns 0 even if there is data inside list.

I created a separate map, added data and printed it. It works. But in the below case.

class Graph {
    constructor() {
        this.list = new Map();
    }

    addVertex(vertex) {
        if (!this.list[vertex]) {
            this.list[vertex] = [];
            console.log("Added", this.list);
        } else {
            console.log("Vertex already exists!");
        }
    }

    addEdge(vertex, node) {
        if (this.list[vertex]) {
            if (!(this.list[vertex].indexOf(node) > -1)) {
                this.list[vertex].push(node); 
            } else {
                console.log('Node : ' + node + " already added!"); //?
            }

        } else {
            console.log("Vertex " + vertex + " does not exist!")
        }
    }

    getGraph() {
        console.log(this.list);
        console.log(this.list.size); // List size es as zero even if I added some nodes and vertices
    }

}

var graph = new Graph();
graph.addVertex("1");
graph.addVertex("2");
graph.addVertex("3");
graph.addVertex("1");
graph.addVertex("5");
graph.addVertex("5");

graph.addEdge("1", "3");
graph.addEdge("2", "3");
graph.addEdge("2", "3");
graph.addEdge("12", "3");

graph.getGraph();

I am learning data structures in JS and written some code to build Graph data structure.

But there seems to be an issue that I could not understand why it is happening.

Please look at the ments on getGraph() method. I am just printing the size of the list and the list itself here. list.size returns 0 even if there is data inside list.

I created a separate map, added data and printed it. It works. But in the below case.

class Graph {
    constructor() {
        this.list = new Map();
    }

    addVertex(vertex) {
        if (!this.list[vertex]) {
            this.list[vertex] = [];
            console.log("Added", this.list);
        } else {
            console.log("Vertex already exists!");
        }
    }

    addEdge(vertex, node) {
        if (this.list[vertex]) {
            if (!(this.list[vertex].indexOf(node) > -1)) {
                this.list[vertex].push(node); 
            } else {
                console.log('Node : ' + node + " already added!"); //?
            }

        } else {
            console.log("Vertex " + vertex + " does not exist!")
        }
    }

    getGraph() {
        console.log(this.list);
        console.log(this.list.size); // List size es as zero even if I added some nodes and vertices
    }

}

var graph = new Graph();
graph.addVertex("1");
graph.addVertex("2");
graph.addVertex("3");
graph.addVertex("1");
graph.addVertex("5");
graph.addVertex("5");

graph.addEdge("1", "3");
graph.addEdge("2", "3");
graph.addEdge("2", "3");
graph.addEdge("12", "3");

graph.getGraph();
Share Improve this question asked Jul 2, 2019 at 18:59 Praveen AlluriPraveen Alluri 3173 silver badges14 bronze badges 7
  • 2 Maps do not use [] syntax. – SLaks Commented Jul 2, 2019 at 19:00
  • Can you please explain what you are referring to? – Praveen Alluri Commented Jul 2, 2019 at 19:01
  • 1 You have not added any values to the Map (this.list.set(vertex, []);). – loganfsmyth Commented Jul 2, 2019 at 19:03
  • 2 That isn't adding the value to the map itself, that is adding a random property to the Map object. .set is how you add items to a Map. – loganfsmyth Commented Jul 2, 2019 at 19:09
  • 1 The core of it is, console.log logs all of the own enumerable properties of an object, it does not log all of the values stored in a Map. – loganfsmyth Commented Jul 2, 2019 at 19:18
 |  Show 2 more ments

1 Answer 1

Reset to default 8
if (!this.list[vertex]) {
  this.list[vertex] = [];

This isn't the right way to interact with the contents of a map. It's legal javacript, but you're appending arbitrary key/value pairs to the map object, not actually storing things in the map.

Instead, do this:

if (!this.list.has(vertex) {
  this.list.set(vertex, []);
}

Similarly, when you want to get data from the map, don't use the bracket syntax, use this.list.get(vertex)

发布评论

评论列表(0)

  1. 暂无评论