首页 > 解决方案 > 让我的班级在做 Array.from 时返回数组

问题描述

我有一个 PriorityQueue 类,它有很多方法,例如 add、peak、changePriority 等。所以要实例化我们拥有的类:

let priorityQueue = new PriorityQueue();

要将某些内容添加到 PriorityQueue 实例,我可以执行以下操作:

priorityQueue.add(10, 1);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);

我的问题是我们该怎么做

Array.from(priorityQueue)

所以返回[100, 200, 10]

这是我的完整代码:

class QElement { 
    constructor(element, priority) 
    { 
        this.element = element; 
        this.priority = priority; 
    } 
} 
  
class PriorityQueue extends Array{ 
  
    // An array is used to implement priority 
    constructor() 
    { 
        super()
        this.items = []; 
    } 
    add(value, priority) 
    { 
        // creating object from queue element 
        var qElement = new QElement(value, priority); 
        var contain = false; 
      
        // iterating through the entire 
        // item array to add element at the 
        // correct location of the Queue 
        for (var i = 0; i < this.items.length; i++) { 
            if (this.items[i].priority > qElement.priority) { 
                // Once the correct location is found it is 
                // enqueued 
                this.items.splice(i, 0, qElement); 
                contain = true; 
                break; 
            } 
        } 
      
        // if the element have the highest priority 
        // it is added at the end of the queue 
        if (!contain) { 
            this.items.push(qElement); 
        } 
    }
    
    poll() 
    { 
        // return the dequeued element 
        // and remove it. 
        // if the queue is empty 
        // returns Underflow 
        if (this.isEmpty()) 
            return "Underflow"; 
        return this.items.shift(); 
    } 
    
    peak() 
    { 
        // returns the highest priority element 
        // in the Priority queue without removing it. 
        if (this.isEmpty()) 
            return "No elements in Queue"; 
        return this.items[0]; 
    } 
    
    rear() 
    { 
        // returns the lowest priorty 
        // element of the queue 
        if (this.isEmpty()) 
            return "No elements in Queue"; 
        return this.items[this.items.length - 1]; 
    }
    
    printPQueue() 
    { 
        var str = ""; 
        for (var i = 0; i < this.items.length; i++) 
            str += this.items[i].element + " "; 
        return str; 
    } 
}

var priorityQueue = new PriorityQueue();

priorityQueue.add(10, 1);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
priorityQueue.printPQueue()
let ppp = Array.from(priorityQueue)
console.log(ppp)

这应该返回[100, 200, 10],但它返回 [],一个空数组。

我必须严格使用Array.from. 解决方案是什么?

标签: javascriptclassprototypal-inheritance

解决方案


你可以使用Symbol.iterator.

在下面的代码片段中,我省略了任何优先级队列逻辑,仅用于sort演示如何使用该生成器:

class PriorityQueue {
    constructor() {
        this.arr = [];
    }
    add(data, priority) {
        this.arr.push({ data, priority });
    }
    * [Symbol.iterator]() {
        yield * [...this.arr].sort((a, b) => a.priority - b.priority).map(a => a.data);
    }
};

let priorityQueue = new PriorityQueue();
priorityQueue.add(10, 1);
priorityQueue.add(100, 0);
priorityQueue.add(200, 0);
console.log(Array.from(priorityQueue));

编辑后

您添加到问题的代码显示错误。尽管您进行了扩展Array,但您从不使用实例的 Array 功能。您的代码继续执行,就好像它没有继承这些代码,而是创建了一个名为items. 当您将项目推送到该对象将保持为空数组的items属性时,这是意料之中的。this如果你想使用,extends那么你应该在某个地方有类似this.push(value),this.pop()的调用this.splice(.....)


推荐阅读