<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 問題 Problem 你有一個對象數組,例如: You have an Array of Objects, such as: ~~~ cats = [ { name: "Bubbles" age: 1 }, { name: "Sparkle" favoriteFood: "tuna" } ] ~~~ 但是你想像放字典一樣訪問它,例如`cat["Bubbles"]`。 But you want to access it as a dictionary by key, like`cats["Bubbles"]`. ## 方法 Solution 你需要把數組轉化成一個對象,使用reduce來實現。 You need to convert your array into an Object. Use reduce for this. ~~~ # key = The key by which to index the dictionary Array::toDict = (key) -> @reduce ((dict, obj) -> dict[ obj[key] ] = obj if obj[key]?; return dict), {} ~~~ 用法: To use this: ~~~ catsDict = cats.toDict('name') catsDict["Bubbles"] # => { age: 1, name: "Bubbles" } ~~~ ## Discussion Alternatively, you can use an Array comprehension: ~~~ Array::toDict = (key) -> dict = {} dict[obj[key]] = obj for obj in this when obj[key]? dict ~~~ 如果你使用Underscore.js,你可以創建一個mixin: If you use Underscore.js, you can create a mixin: ~~~ _.mixin toDict: (arr, key) -> throw new Error('_.toDict takes an Array') unless _.isArray arr _.reduce arr, ((dict, obj) -> dict[ obj[key] ] = obj if obj[key]?; return dict), {} catsDict = _.toDict(cats, 'name') catsDict["Sparkle"] # => { favoriteFood: "tuna", name: "Sparkle" } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看