Vuejs通过数据键迭代复杂对象

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

Vuejs iterate complex object by data key

问题

我正在使用Vuejs3来构建一个简单的文章配置器。

让我们来定义一个复杂的对象:

const article = reactive({
  code: 'code',
  specs: {
    type: { text: 'description type', value: 'mytype' },
    prop1: { text: 'description prop1', value: 'myprop1' },
    prop2: { text: 'description prop1', value: 'myprop1' },
  },
  dimensions: { base: 10, height: 20 }
})

让我们看看每个article.spec

<li v-for="(value, propertyName, index) in article.specs">
  {{ propertyName }}: {{ value.text }} ({{ index }})
</li>

结果:

type: description type (0)
prop1: description prop1 (1)
prop2: description prop1 (2)

但是,如何提取仅属性"value"的内容以获得:

type: description type
prop1: description prop1
prop2: description prop1

我尝试过以下方法:

{{ propertyName }}: {{ value['text'] }} ({{ index }})

但结果是空值:

type: (0)
prop1: (1)
prop2: (2)
英文:

I'm using Vuejs3 to build a simple article configurator.

Let's define a complex object:

const article = reactive({
  code: &#39;code&#39;,
  specs: {
    type: { text: &#39;description type&#39;, value:&#39;mytype&#39;} ,
    prop1: { text: &#39;description prop1&#39;, value: &#39;myprop1&#39; },        
    prop2: { text: &#39;description prop1&#39;, value: &#39;myprop1&#39; },                
  },
  dimensions: { base: 10, height: 20}
})

Let's see every article.spec:

&lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
  {{ propertyName }}:  {{ value }} ({{ index }})
&lt;/li&gt;   

result:

type: { &quot;text&quot;: &quot;description type&quot;, &quot;value&quot;: &quot;mytype&quot; } (0)
prop1: { &quot;text&quot;: &quot;description prop1&quot;, &quot;value&quot;: &quot;myprop1&quot; } (1)
prop2: { &quot;text&quot;: &quot;description prop1&quot;, &quot;value&quot;: &quot;myprop1&quot; } (2)

But how do I extract property "value" only to get.

type: description type
prop1: description prop1
prop2: description prop1

I'v tried with:

  {{ propertyName }}:  {{ value[text] }}  ({{ index }})

but result in empty values:

type: (0)
prop1: (1)
prop2: (2)

答案1

得分: 2

如果您想使用方括号[]来访问属性名称,请在方括号内使用字符串[&quot;text&quot;]

&lt;template&gt;
  &lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
    {{ propertyName }}:  {{ value[&quot;text&quot;] }} ({{ index }})
  &lt;/li&gt;   
&lt;/template&gt;
英文:

If you want to use brackets [] to access into a propery name, use an string inside brackets [&quot;text&quot;]

&lt;template&gt;
  &lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
    {{ propertyName }}:  {{ value[&quot;text&quot;] }} ({{ index }})
  &lt;/li&gt;   
&lt;/template&gt;

答案2

得分: 1

解决方案 #1

可以通过名称调用对象的属性。

value.text

// 或者

value["text"]

可以通过动态变量调用对象的属性。

const name = "text"
value[name]

解决方案 #2

可以使用for循环。

<span v-for="propValue of value">{{ propValue }}</span>

示例

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

const { createApp, reactive } = Vue

const app = createApp({
  setup() {
    const article = reactive({
      code: 'code',
      specs: {
        type: { text: 'description type', value: 'mytype' },
        prop1: { text: 'description prop1', value: 'myprop1' },
        prop2: { text: 'description prop1', value: 'myprop1' },
      },
      dimensions: { base: 10, height: 20 }
    })

    return { article }
  }
}).mount('#app')

<!-- 语言: lang-html -->

<script src="https://unpkg.com/vue@3.3.4/dist/vue.global.prod.js"></script>

<div id="app">
  <h2>Your code</h2>
  <ul>
    <li v-for="(value, propertyName, index) in article.specs">
      {{ propertyName }}: {{ value }} ({{ index }})
    </li>
  </ul>

  <h2>解决方案 #1</h2>
  <ul>
    <li v-for="(value, propertyName, index) in article.specs">
      <div style="display: flex; gap: 8px;">
        <span>{{ propertyName }}:</span>

        <!-- 可以使用对象.name或对象[name]格式 -->
        <!-- 如果使用对象[name]格式,可以动态添加名称 -->
        <span>{{ value.text }}</span> <!-- 或 value["text"] -->
        <span>{{ value["value"] }}</span> <!-- 或 value.value -->

        <span>{{ index }}</span>
      </div>
    </li>
  </ul>

  <h2>解决方案 #2</h2>
  <ul>
    <li v-for="(value, propertyName, index) in article.specs">
      <div style="display: flex; gap: 8px;">
        <span>{{ propertyName }}:</span>

        <!-- 可以使用for循环 -->
        <span v-for="propValue of value">{{ propValue }}</span>

        <span>{{ index }}</span>
      </div>
    </li>
  </ul>
</div>

<!-- 结束代码片段 -->

英文:

Solution # 1

Can call prop of object by name.

value.text

// or

value[&quot;text&quot;]

Can call property of object by dynamically variable.

const name = &quot;text&quot;
value[name]

Solution # 2

Can use for loop.

&lt;span v-for=&quot;propValue of value&quot;&gt;{{ propValue }}&lt;/span&gt;

Example

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const { createApp, reactive } = Vue

const app = createApp({
  setup() {
    const article = reactive({
      code: &#39;code&#39;,
      specs: {
        type: { text: &#39;description type&#39;, value:&#39;mytype&#39;} ,
        prop1: { text: &#39;description prop1&#39;, value: &#39;myprop1&#39; },        
        prop2: { text: &#39;description prop1&#39;, value: &#39;myprop1&#39; },                
      },
      dimensions: { base: 10, height: 20}
    })

    return { article }
  }
}).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@3.3.4/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;
  &lt;h2&gt;Your code&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
      {{ propertyName }}:  {{ value }} ({{ index }})
    &lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;h2&gt;Solution # 1&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
      &lt;div style=&quot;display: flex; gap: 8px;&quot;&gt;
        &lt;span&gt;{{ propertyName }}:&lt;/span&gt;
        
        &lt;!-- can use object.name or object[name] formats --&gt;
        &lt;!-- can add dynamically name if use object[name] format --&gt;
        &lt;span&gt;{{ value.text }}&lt;/span&gt; &lt;!-- or value[&quot;text&quot;] --&gt;
        &lt;span&gt;{{ value[&quot;value&quot;] }}&lt;/span&gt; &lt;!-- or value.value --&gt;
        
        &lt;span&gt;{{ index }}&lt;/span&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;h2&gt;Solution # 2&lt;/h2&gt;
  &lt;ul&gt;
    &lt;li v-for=&quot;(value, propertyName, index) in article.specs&quot;&gt;
      &lt;div style=&quot;display: flex; gap: 8px;&quot;&gt;
        &lt;span&gt;{{ propertyName }}:&lt;/span&gt;

        &lt;!-- can use for loop --&gt;
        &lt;span v-for=&quot;propValue of value&quot;&gt;{{ propValue }}&lt;/span&gt;

        &lt;span&gt;{{ index }}&lt;/span&gt;
      &lt;/div&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

Use correct separator: not [] but . !

{{ propertyName }}: {{ value.text }} ({{ index }})

英文:

Use correct separator: not [] but . !

  {{ propertyName }}:  {{ value.text }} ({{ index }})

答案4

得分: 0

另一种减少模板代码的方法可能是定义一个计算属性

const article_specs = computed(() => {
  let result = '';
  Object.keys(article.specs).forEach((item) => {
    result += article.specs[item].value + 'todo';
    console.log("item", article.specs[item]);
  });
  return result;
});

而模板代码变成了:

{{article_specs}}
英文:

Another approach to minimize template code could be to define a computed:

    const article_specs = computed(() =&gt; {
      let result = &#39;&#39;;
      Object.keys(article.specs).forEach((item) =&gt; {
    
        result +=  article.specs[item].value + &#39;todo&#39;
        console.log(&quot;item&quot;, article.specs[item])
    
      })

  return result;
})

And template code became:

{{article_specs}}

huangapple
  • 本文由 发表于 2023年6月29日 22:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582083.html
匿名

发表评论

匿名网友

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

确定