<small id='dWKXE'></small><noframes id='dWKXE'>

    <i id='dWKXE'><tr id='dWKXE'><dt id='dWKXE'><q id='dWKXE'><span id='dWKXE'><b id='dWKXE'><form id='dWKXE'><ins id='dWKXE'></ins><ul id='dWKXE'></ul><sub id='dWKXE'></sub></form><legend id='dWKXE'></legend><bdo id='dWKXE'><pre id='dWKXE'><center id='dWKXE'></center></pre></bdo></b><th id='dWKXE'></th></span></q></dt></tr></i><div id='dWKXE'><tfoot id='dWKXE'></tfoot><dl id='dWKXE'><fieldset id='dWKXE'></fieldset></dl></div>

      <legend id='dWKXE'><style id='dWKXE'><dir id='dWKXE'><q id='dWKXE'></q></dir></style></legend>
    1. <tfoot id='dWKXE'></tfoot>
      • <bdo id='dWKXE'></bdo><ul id='dWKXE'></ul>

    2. Javascript 在集合中搜索对象键

      Javascript search for an object key in a set(Javascript 在集合中搜索对象键)
      <i id='ba1YC'><tr id='ba1YC'><dt id='ba1YC'><q id='ba1YC'><span id='ba1YC'><b id='ba1YC'><form id='ba1YC'><ins id='ba1YC'></ins><ul id='ba1YC'></ul><sub id='ba1YC'></sub></form><legend id='ba1YC'></legend><bdo id='ba1YC'><pre id='ba1YC'><center id='ba1YC'></center></pre></bdo></b><th id='ba1YC'></th></span></q></dt></tr></i><div id='ba1YC'><tfoot id='ba1YC'></tfoot><dl id='ba1YC'><fieldset id='ba1YC'></fieldset></dl></div>

        <tfoot id='ba1YC'></tfoot>
      • <small id='ba1YC'></small><noframes id='ba1YC'>

          • <legend id='ba1YC'><style id='ba1YC'><dir id='ba1YC'><q id='ba1YC'></q></dir></style></legend>
                <tbody id='ba1YC'></tbody>
                <bdo id='ba1YC'></bdo><ul id='ba1YC'></ul>
              • 本文介绍了Javascript 在集合中搜索对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                是否可以使用 javascript "Set" 对象来查找具有特定键的元素?类似的东西:

                Is it possible to use the javascript "Set" object to find an element with a certain key? Something like that:

                let myObjects = [{"name":"a", "value":0}, {"name":"b", "value":1},{"name":"c", "value":2}];
                let mySet = new Set(myObjects);
                console.log(mySet.has({"name":"a"}));
                

                推荐答案

                不是这样,它会寻找你传入的特定对象,它不在集合中.

                Not in that way, that would look for the specific object you're passing in, which isn't in the set.

                如果你的出发点是一个对象数组,你根本不需要Set,只需要Array.prototype.find:

                If your starting point is an array of objects, you don't need a Set at all, just Array.prototype.find:

                let myObjects = [{"name":"a", "value":0}, {"name":"b", "value":1},{"name":"c", "value":2}];
                let found = myObjects.find(e => e.name === "a");
                console.log(found);

                如果你已经有一个 Set 并且想要搜索它的匹配项,你可以使用它的迭代器,或者直接通过 for-of:

                If you already have a Set and want to search it for a match, you can use its iterator, either directly via for-of:

                let myObjects = [{"name":"a", "value":0}, {"name":"b", "value":1},{"name":"c", "value":2}];
                let mySet = new Set(myObjects);
                let found = undefined; // the `= undefined` is just for emphasis; that's the default value it would have without an initializer
                for (const e of mySet) {
                  if (e.name === "a") {
                    found = e;
                    break;
                  }
                }
                console.log(found);

                ...或间接通过Array.from来(重新)创建(the)一个数组,然后使用find:

                ...or indirectly via Array.from to (re)create (the)an array, and then use find:

                let myObjects = [{"name":"a", "value":0}, {"name":"b", "value":1},{"name":"c", "value":2}];
                let mySet = new Set(myObjects);
                let found = Array.from(mySet).find(e => e.name === "a");
                console.log(found);

                如果这是你需要经常做的事情,你可以给自己一个实用函数:

                If it's something you need to do often, you might give yourself a utility function for it:

                const setFind = (set, cb) => {
                  for (const e of set) {
                    if (cb(e)) {
                      return e;
                    }
                  }
                  return undefined; // undefined` just for emphasis, `return;`
                                    // would do effectively th same thing, as
                                    // indeed would just not having a `return`
                                    // at at all
                }
                
                let myObjects = [{"name":"a", "value":0}, {"name":"b", "value":1},{"name":"c", "value":2}];
                let mySet = new Set(myObjects);
                let found = setFind(mySet, e => e.name === "a");
                console.log(found);

                您甚至可以将它放在 Set.prototype 上(确保它是不可枚举的),但要注意与将来添加到 Set 的内容冲突(例如,我会如果 Set.prototype 在某个时候得到了 find 方法,一点也不感到惊讶.

                You could even put that on Set.prototype (making sure it's non-enumerable), but beware of conflicting with future additions to Set (for instance, I wouldn't be at all surprised if Set.prototype got a find method at some point).

                这篇关于Javascript 在集合中搜索对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)

                <legend id='Iw1xx'><style id='Iw1xx'><dir id='Iw1xx'><q id='Iw1xx'></q></dir></style></legend>
                  <tbody id='Iw1xx'></tbody>

                1. <small id='Iw1xx'></small><noframes id='Iw1xx'>

                    <i id='Iw1xx'><tr id='Iw1xx'><dt id='Iw1xx'><q id='Iw1xx'><span id='Iw1xx'><b id='Iw1xx'><form id='Iw1xx'><ins id='Iw1xx'></ins><ul id='Iw1xx'></ul><sub id='Iw1xx'></sub></form><legend id='Iw1xx'></legend><bdo id='Iw1xx'><pre id='Iw1xx'><center id='Iw1xx'></center></pre></bdo></b><th id='Iw1xx'></th></span></q></dt></tr></i><div id='Iw1xx'><tfoot id='Iw1xx'></tfoot><dl id='Iw1xx'><fieldset id='Iw1xx'></fieldset></dl></div>
                          <bdo id='Iw1xx'></bdo><ul id='Iw1xx'></ul>
                        • <tfoot id='Iw1xx'></tfoot>