基于匹配的ID获取对象的多个属性并输出,无需多次减少。

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

Getting multiple properties of an object based on matching ID and outputting them without reducing multiple times

问题

I can provide a translation for the code-related part of your text. Here it is:

我好奇是否有更好的方法从Vue <template>或包装DOM元素中的减小对象中获取属性

例如在我的组件中我有这两个数据对象

```js
player: [{
   active: true,
   id: 0,
   name: "Alfred",
   action: 0
}, {
   active: true,
   id: 1,
   name: "Bruce",
   action: 1
}],
actions: [{
   id: 0,
   label: "Give advice",
   description: "Player advises others"
}, {
   id: 1,
   label: "Fight",
   description: "Player fights enemy"
}]

... 我想循环遍历一个选项列表,并返回所有结果,其中包括每个玩家内的action属性:

------------------------------------------------------------------
Active?   ID    Name     Label         Description
------------------------------------------------------------------
true      0     Alfred   Give Advice   Player advises others
true      1     Bruce    Fight         Player fights enemy

我知道我可以像这样循环遍历玩家:

<table>
  <tr v-for="(player, index) in players" :key="index">
    <td>{{ player.active }}</td>
    <td>{{ player.id }}</td>
    <td>{{ this.getActionProp(player.action, 'label') }}</td>
    <td>{{ this.getActionProp(player.action, 'description') }}</td>
  </tr>
</table>

使用这样的方法:

getActionProp(id, prop){
  return this.actions.reduce((acc, cur) => {
    return cur.id == id ? cur[prop] : acc
  }, {})
}

但似乎对于表格中的每一行都需要减少两次。是否有更好的方法来获取这些属性,或者至少使它们在每行中只减少一次?

类似这样的方法:

<table>
  <tr v-for="(player, index) in players" :key="index">
    <td>{{ player.active }}</td>
    <td>{{ player.id }}</td>
    <template :action="getAction(player.action)">
      <td>{{ action.label }}</td>
      <td>{{ action.description }}</td>
    </template>
  </tr>
</table>

其中 getAction() 是一个减少并返回匹配action对象的方法。

感谢您的时间,我感谢您的指导。


<details>
<summary>英文:</summary>

I&#39;m curious if there&#39;s a better way of getting properties from a reduced object within a Vue &lt;template&gt; or wrapper DOM element.

For example, I have these two data objects in my component:

```js
player: [{
   active: true,
   id: 0,
   name: &quot;Alfred&quot;,
   action: 0
}, {
   active: true,
   id: 1,
   name: &quot;Bruce&quot;,
   action: 1
}],
actions: [{
   id: 0,
   label: &quot;Give advice&quot;,
   description: &quot;Player advises others&quot;
}, {
   id: 1,
   label: &quot;Fight&quot;,
   description: &quot;Player fights enemy&quot;
}]

...and I'd like to loop through a list of options and return all results, with properties of the action within each player:

------------------------------------------------------------------
Active?   ID    Name     Label         Description
------------------------------------------------------------------
true      0     Alfred   Give Advice   Player advises others
true      1     Bruce    Fight         Player fights enemy

I know I can loop through the players like so:

&lt;table&gt;
  &lt;tr v-for=&quot;(player, index) in players&quot; :key=&quot;index&quot;&gt;
    &lt;td&gt;{{ player.active }}&lt;/td&gt;
    &lt;td&gt;{{ player.id }}&lt;/td&gt;
    &lt;td&gt;{{ this.getActionProp(player.action, &#39;label&#39;) }}&lt;/td&gt;
    &lt;td&gt;{{ this.getActionProp(player.action, &#39;description&#39;) }}&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

With a method like this:

getActionProp(id, prop){
  return this.actions.reduce((acc, cur) =&gt; {
    return cur.id == id ? cur[prop] : acc
  }, {})
}

But it seems like that has to reduce twice for each row in the table. Is there a better way to get these props or at least to make it so they only reduce once per row?

Something like:

&lt;table&gt;
  &lt;tr v-for=&quot;(player, index) in players&quot; :key=&quot;index&quot;&gt;
    &lt;td&gt;{{ player.active }}&lt;/td&gt;
    &lt;td&gt;{{ player.id }}&lt;/td&gt;
    &lt;template :action=&quot;getAction(player.action)&quot;&gt;
      &lt;td&gt;{{ action.label }}&lt;/td&gt;
      &lt;td&gt;{{ action.description }}&lt;/td&gt;
    &lt;/template&gt;
  &lt;/tr&gt;
&lt;/table&gt;

with getAction() being a method that reduces and returns the matching action object.

Thank you for your time and I appreciate your guidance.

答案1

得分: 2

你可以只需映射计算中的操作并准备表格行:

Vue SFC Playground

&lt;script setup&gt;
import { computed, reactive } from &#39;vue&#39;

const players = reactive([{
  active: true,
  id: 0,
  name: &quot;Alfred&quot;,
  action: 0
}, {
  active: true,
  id: 1,
  name: &quot;Bruce&quot;,
  action: 1
}])

const actions = reactive([{
  id: 0,
  label: &quot;Give advice&quot;,
  description: &quot;Player advises others&quot;
}, {
  id: 1,
  label: &quot;Fight&quot;,
  description: &quot;Player fights enemy&quot;
}])

const playerTable = computed(() =&gt; players.map(player =&gt; {
  return {
    ...player,
    action: actions.find(action =&gt; action.id === player.action)
  }
}))
&lt;/script&gt;

&lt;template&gt;
  &lt;h1&gt;TEST&lt;/h1&gt;
  &lt;table&gt;
    &lt;tr v-for=&quot;player in playerTable&quot; :key=&quot;player.id&quot;&gt;
      &lt;td&gt;{{ player.active }}&lt;/td&gt;
      &lt;td&gt;{{ player.id }}&lt;/td&gt;
      &lt;td&gt;{{ player.action.label }}&lt;/td&gt;
      &lt;td&gt;{{ player.action.description }}&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/template&gt;
英文:

You can just just map the actions in a computed and prepare the table rows:

Vue SFC Playground

&lt;script setup&gt;
import { computed, reactive } from &#39;vue&#39;

const players = reactive([{
  active: true,
  id: 0,
  name: &quot;Alfred&quot;,
  action: 0
}, {
  active: true,
  id: 1,
  name: &quot;Bruce&quot;,
  action: 1
}])

const actions = reactive([{
  id: 0,
  label: &quot;Give advice&quot;,
  description: &quot;Player advises others&quot;
}, {
  id: 1,
  label: &quot;Fight&quot;,
  description: &quot;Player fights enemy&quot;
}])

const playerTable = computed(() =&gt; players.map(player =&gt; {
  return {
    ...player,
    action: actions.find(action =&gt; action.id === player.action)
  }
}))
&lt;/script&gt;

&lt;template&gt;
  &lt;h1&gt;TEST&lt;/h1&gt;
  &lt;table&gt;
    &lt;tr v-for=&quot;player in playerTable&quot; :key=&quot;player.id&quot;&gt;
      &lt;td&gt;{{ player.active }}&lt;/td&gt;
      &lt;td&gt;{{ player.id }}&lt;/td&gt;
      &lt;td&gt;{{ player.action.label }}&lt;/td&gt;
      &lt;td&gt;{{ player.action.description }}&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/template&gt;

huangapple
  • 本文由 发表于 2023年6月30日 04:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584355.html
匿名

发表评论

匿名网友

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

确定