遍历具有键/值映射的对象

huangapple go评论55阅读模式
英文:

Iterating through object with key/value map

问题

从REST API获取的对象如下所示:

response =  {
    "0": [
        {
            "description": "basket",
            "color": "red"
        },
        {
            "description": "pillow",
            "color": "blue"
        },
        {
            "description": "armchair",
            "color": "white"
        }
    ],
    "1" : [
        {
            "description": "shirt",
            "color": "black"
        },
        {
            "description": "pants",
            "color": "blue"
        }
    ]
}

这是我需要在我的视图中显示的内容。

controller.js中,您可以使用以下代码:

self.catKeys = Object.keys(response);
self.catValue = Object.values(response);

view.html中,您可以使用以下代码:

<div ng-repeat="cat in ctrl.catKeys" class="col-md-3">{{ cat }}</div>

<div ng-repeat="val in ctrl.catValue" class="col-md-9">
    <div ng-repeat="item in val">
        {{ item.description }} {{ item.color }}
    </div>
</div>

希望这对您有所帮助。如果有其他问题,请随时提问。

英文:

From the rest api, I get an object like this:

          response =  {
                        &quot;0&quot;: [
                                    {
                                        &quot;description&quot;: &quot;basket&quot;,
                                        &quot;color&quot;: &quot;red&quot;
                                    },
                                    {
                                        &quot;description&quot;: &quot;pillow&quot;,
                                        &quot;color&quot;: &quot;blue&quot;
                                    },
                                    {
                                        &quot;description&quot;: &quot;armchair&quot;,
                                        &quot;color&quot;: &quot;white&quot;
                                    }
                               ],
                         &quot;1&quot; : [
                                        {
                                            &quot;description&quot;: &quot;shirt&quot;,
                                            &quot;color&quot;: &quot;black&quot;
                                        },
                                        {
                                            &quot;description&quot;: &quot;pants&quot;,
                                            &quot;color&quot;: &quot;blue&quot;
                                        }
                                ]
                        }

This is what I need to display in my view:
遍历具有键/值映射的对象

// controller.js

self.catKeys= Object.keys(response)
self.catValue= Object.values(response)

// view.html

  &lt;div ng-repeat=&quot;cat in ctrl.catKeys&quot; class=&quot;col-md-3&quot;&gt; {{ cat }} &lt;/div&gt;

  &lt;div ng-repeat=&quot;val in ctrl.catValue&quot; class=&quot;col-md-9&quot;&gt;
    {{ val[index].description }} {{ val[index].color}}
  &lt;/div&gt;

I've also tried many other things but I just cannot get it..please help 遍历具有键/值映射的对象

答案1

得分: 3

使用Array#map将每个项目与其颜色连接起来,然后使用join将它们组合成一个字符串。

for (const [k, v] of Object.entries(response)) {
  response[k] = v.map(x => x.color + ' ' + x.description).join(' - ');
}
self.response = response;

然后,显示很简单:

<div ng-repeat="(k, v) in ctrl.response" class="row">
    <div class="col-md-3">{{k}}</div>
    <div class="col-md-9">{{v}}</div>
</div>
英文:

You can use Array#map to concatenate each item with its color, then join it into one string.

for (const [k, v] of Object.entries(response)) {
  response[k] = v.map(x =&gt; x.color + &#39; &#39; + x.description).join(&#39; - &#39;);
}
self.response = response;

Then, displaying is simple:

&lt;div ng-repeat=&quot;(k, v) in ctrl.response&quot; class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-3&quot;&gt;{{k}}&lt;/div&gt;
    &lt;div class=&quot;col-md-9&quot;&gt;{{v}}&lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年3月31日 04:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定