当前位置: 首页 > 面试题库 >

在JavaScript中模仿集合?

易祯
2023-03-14
问题内容

我正在使用JavaScript。我想存储具有以下属性的 唯一 ,无序字符串值的列表:

  1. 快速询问“列表中是否有A”的方法?
  2. 一种快速的方法(如果列表中存在A,则从列表中删除A)
  3. 一种快速的方法“将A添加到列表(如果尚不存在)”。

我真正想要的是一套。有什么建议以最佳方式模仿JavaScript中的集合吗?

这个问题建议使用Object,其键存储属性,并且所有值都设置为true:这是明智的方法吗?


问题答案:

如果要在支持ES6的环境中编程(例如,node.js,具有ES6功能的特定浏览器或为环境转换ES6代码),则可以使用SetES6中内置的对象。它具有非常好的功能,可以在您的环境中直接使用。

对于ES5环境中的许多简单事物,使用Object效果很好。如果obj是您的对象,并且A是一个具有要在集合中操作的值的变量,则可以执行以下操作:

初始化代码:

// create empty object
var obj = {};

// or create an object with some items already in it
var obj = {"1":true, "2":true, "3":true, "9":true};

问题1:A在列表中:

if (A in obj) {
    // put code here
}

问题2: 如果存在,请从列表中删除“ A”:

delete obj[A];

问题3: 如果尚未在列表中添加“ A”

obj[A] = true;

为了完整起见,使用以下方法测试是否A在列表中会更加安全:

if (Object.prototype.hasOwnProperty.call(obj, A))
    // put code here
}

因为基础对象上的内置方法和/或属性(例如constructor属性)之间可能存在冲突。

ES6上的侧栏: ECMAScript 6 或ES 2015 的当前工作版本具有 内置的Set对象
。现在已在某些浏览器中实现。由于浏览器的可用性随时间变化的,你可以看看线Set在此ES6兼容性表,查看浏览器可用性的当前状态。

内置Set对象的一个​​优点是,它不像Object那样将所有键都强制转换为字符串,因此您可以将5和“
5”作为单独的键。而且,您甚至可以直接在集合中使用对象,而无需进行字符串转换。

我现在为ES6设置对象编写了一个polyfill,因此您现在就可以开始使用它,如果浏览器支持,它将自动遵从内置设置对象。这样做的好处是您正在编写与ES6兼容的代码,这些代码将一直工作到IE7。但是,还有一些缺点。ES6
set接口利用了ES6迭代器,因此您可以做类似的事情for (item of mySet),它将为您自动html" target="_blank">遍历set6。但是,这种语言功能无法通过polyfill实现。您仍然可以在不使用新ES6语言功能的情况下迭代ES6集,但是坦率地说,在没有新语言功能的情况下,它不如我在下面包括的其他集界面那样方便。

查看两者后,您可以决定哪一个最适合您。

仅供参考,在我自己的测试中,我注意到Firefox v29
Set实施不是最新的规范草案。例如,您不能.add()像规范说明和我的polyfill支持那样链接方法调用。这可能是运动规范的问题,因为它尚未最终确定。

预先构建的Set对象:
如果想要一个已经构建的对象,该对象具有可在任何浏览器中使用的对集合进行操作的方法,则可以使用实现不同类型集合的一系列不同的预先构建的对象。有一个miniSet,它是一些小代码,可实现set对象的基础。它还具有功能更丰富的set对象和几个派生对象,包括Dictionary(让您为每个键存储/检索一个值)和ObjectSet(让您保留一组对象-
JS对象或DOM对象,您可以在其中提供为每个键生成唯一键的函数,否则ObjectSet会为您生成键)。

)。

"use strict";
//-------------------------------------------
// Simple implementation of a Set in javascript
//
// Supports any element type that can uniquely be identified
//    with its string conversion (e.g. toString() operator).
// This includes strings, numbers, dates, etc...
// It does not include objects or arrays though
//    one could implement a toString() operator
//    on an object that would uniquely identify
//    the object.
// 
// Uses a javascript object to hold the Set
//
// This is a subset of the Set object designed to be smaller and faster, but
// not as extensible.  This implementation should not be mixed with the Set object
// as in don't pass a miniSet to a Set constructor or vice versa.  Both can exist and be
// used separately in the same project, though if you want the features of the other
// sets, then you should probably just include them and not include miniSet as it's
// really designed for someone who just wants the smallest amount of code to get
// a Set interface.
//
// s.add(key)                      // adds a key to the Set (if it doesn't already exist)
// s.add(key1, key2, key3)         // adds multiple keys
// s.add([key1, key2, key3])       // adds multiple keys
// s.add(otherSet)                 // adds another Set to this Set
// s.add(arrayLikeObject)          // adds anything that a subclass returns true on _isPseudoArray()
// s.remove(key)                   // removes a key from the Set
// s.remove(["a", "b"]);           // removes all keys in the passed in array
// s.remove("a", "b", ["first", "second"]);   // removes all keys specified
// s.has(key)                      // returns true/false if key exists in the Set
// s.isEmpty()                     // returns true/false for whether Set is empty
// s.keys()                        // returns an array of keys in the Set
// s.clear()                       // clears all data from the Set
// s.each(fn)                      // iterate over all items in the Set (return this for method chaining)
//
// All methods return the object for use in chaining except when the point
// of the method is to return a specific value (such as .keys() or .isEmpty())
//-------------------------------------------


// polyfill for Array.isArray
if(!Array.isArray) {
    Array.isArray = function (vArg) {
        return Object.prototype.toString.call(vArg) === "[object Array]";
    };
}

function MiniSet(initialData) {
    // Usage:
    // new MiniSet()
    // new MiniSet(1,2,3,4,5)
    // new MiniSet(["1", "2", "3", "4", "5"])
    // new MiniSet(otherSet)
    // new MiniSet(otherSet1, otherSet2, ...)
    this.data = {};
    this.add.apply(this, arguments);
}

MiniSet.prototype = {
    // usage:
    // add(key)
    // add([key1, key2, key3])
    // add(otherSet)
    // add(key1, [key2, key3, key4], otherSet)
    // add supports the EXACT same arguments as the constructor
    add: function() {
        var key;
        for (var i = 0; i < arguments.length; i++) {
            key = arguments[i];
            if (Array.isArray(key)) {
                for (var j = 0; j < key.length; j++) {
                    this.data[key[j]] = key[j];
                }
            } else if (key instanceof MiniSet) {
                var self = this;
                key.each(function(val, key) {
                    self.data[key] = val;
                });
            } else {
                // just a key, so add it
                this.data[key] = key;
            }
        }
        return this;
    },
    // private: to remove a single item
    // does not have all the argument flexibility that remove does
    _removeItem: function(key) {
        delete this.data[key];
    },
    // usage:
    // remove(key)
    // remove(key1, key2, key3)
    // remove([key1, key2, key3])
    remove: function(key) {
        // can be one or more args
        // each arg can be a string key or an array of string keys
        var item;
        for (var j = 0; j < arguments.length; j++) {
            item = arguments[j];
            if (Array.isArray(item)) {
                // must be an array of keys
                for (var i = 0; i < item.length; i++) {
                    this._removeItem(item[i]);
                }
            } else {
                this._removeItem(item);
            }
        }
        return this;
    },
    // returns true/false on whether the key exists
    has: function(key) {
        return Object.prototype.hasOwnProperty.call(this.data, key);
    },
    // tells you if the Set is empty or not
    isEmpty: function() {
        for (var key in this.data) {
            if (this.has(key)) {
                return false;
            }
        }
        return true;
    },
    // returns an array of all keys in the Set
    // returns the original key (not the string converted form)
    keys: function() {
        var results = [];
        this.each(function(data) {
            results.push(data);
        });
        return results;
    },
    // clears the Set
    clear: function() {
        this.data = {}; 
        return this;
    },
    // iterate over all elements in the Set until callback returns false
    // myCallback(key) is the callback form
    // If the callback returns false, then the iteration is stopped
    // returns the Set to allow method chaining
    each: function(fn) {
        this.eachReturn(fn);
        return this;
    },
    // iterate all elements until callback returns false
    // myCallback(key) is the callback form
    // returns false if iteration was stopped
    // returns true if iteration completed
    eachReturn: function(fn) {
        for (var key in this.data) {
            if (this.has(key)) {
                if (fn.call(this, this.data[key], key) === false) {
                    return false;
                }
            }
        }
        return true;
    }
};

MiniSet.prototype.constructor = MiniSet;


 类似资料:
  • 本文向大家介绍js模仿java的Map集合详解,包括了js模仿java的Map集合详解的使用技巧和注意事项,需要的朋友参考一下 java.util 中的集合类包含 Java 中某些最常用的类。最常用的集合类是 List 和 Map。List 的具体实现包括 ArrayList 和 Vector,它们是可变大小的列表,比较适合构建、存储和操作任何类型对象元素列表。List 适用于按数值索引访问元素的

  • 问题内容: 我正在寻找一种模式,该模式允许在Kubernetes中在同一容器上运行的两个容器之间共享卷。 我的用例是:我有一个在Docker容器中运行的Ruby on Rails应用程序。docker映像在目录中包含静态资产,我需要从在同一容器中并排运行的nginx容器访问这些资产。 在“ vanilla”泊坞窗中,我将使用flag共享此目录: 阅读此文档后:https : //github.co

  • 我对集成测试有些陌生。我有两个使用Kafka互相传递消息的服务。然而,对于我的集成测试,我不一定希望为了运行我的测试而运行Kafka。有没有模仿Kafka的标准方法?或者这是我需要自己创建的东西,一些MockKafka队列和补丁在应用程序中适当的地方?此外,这是否违反了集成测试应该做的事情?我对此的看法是,我没有测试Kafka的任何功能,为了集成,测试应该被嘲笑。

  • 我有一个关于嘲笑的问题。在kotlin中,当您使用Mockito时,您可以: 或 有没有办法在dart/flutter中做这样的事情?我真的只需要将mock传递给某个函数并忘记,为什么我必须创建对象并使用Mock类扩展它? 有没有其他的图书馆可以帮助解决这个问题?

  • 我是一个相当新开发的新泽西客户,在一些测试中遇到了一些问题。首先,我可能应该提到,我的应用程序都是客户端的,根本没有服务器端的东西。 我的问题是,我想创建实例< code > InboundJaxrsResponse 的< code>Response对象,到目前为止,我一直试图通过使用< code>Mockito和< code > Response builder . build()模拟< cod

  • 问题内容: 我真的很想让一段文字闪烁传统风格,而不使用javascript或文字修饰。 没有过渡,只有 blink , blink , blink ! 编辑 :这不同于该问题,因为我要求不连续转换的_闪烁_ ,而其他问题的OP询问如何 用连续转换 来 代替 闪烁 __ 问题答案: 最初的Netscape 具有80%的占空比。这很接近,尽管实数仅影响文本: