英文:
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: '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}
})
Let's see every article.spec:
<li v-for="(value, propertyName, index) in article.specs">
{{ propertyName }}: {{ value }} ({{ index }})
</li>
result:
type: { "text": "description type", "value": "mytype" } (0)
prop1: { "text": "description prop1", "value": "myprop1" } (1)
prop2: { "text": "description prop1", "value": "myprop1" } (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
如果您想使用方括号[]
来访问属性名称,请在方括号内使用字符串["text"]
。
<template>
<li v-for="(value, propertyName, index) in article.specs">
{{ propertyName }}: {{ value["text"] }} ({{ index }})
</li>
</template>
英文:
If you want to use brackets []
to access into a propery name, use an string inside brackets ["text"]
<template>
<li v-for="(value, propertyName, index) in article.specs">
{{ propertyName }}: {{ value["text"] }} ({{ index }})
</li>
</template>
答案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["text"]
Can call property of object by dynamically variable.
const name = "text"
value[name]
Solution # 2
Can use for loop.
<span v-for="propValue of value">{{ propValue }}</span>
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: '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')
<!-- language: 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>Solution # 1</h2>
<ul>
<li v-for="(value, propertyName, index) in article.specs">
<div style="display: flex; gap: 8px;">
<span>{{ propertyName }}:</span>
<!-- can use object.name or object[name] formats -->
<!-- can add dynamically name if use object[name] format -->
<span>{{ value.text }}</span> <!-- or value["text"] -->
<span>{{ value["value"] }}</span> <!-- or value.value -->
<span>{{ index }}</span>
</li>
</ul>
<h2>Solution # 2</h2>
<ul>
<li v-for="(value, propertyName, index) in article.specs">
<div style="display: flex; gap: 8px;">
<span>{{ propertyName }}:</span>
<!-- can use for loop -->
<span v-for="propValue of value">{{ propValue }}</span>
<span>{{ index }}</span>
</div>
</li>
</ul>
</div>
<!-- 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(() => {
let result = '';
Object.keys(article.specs).forEach((item) => {
result += article.specs[item].value + 'todo'
console.log("item", article.specs[item])
})
return result;
})
And template code became:
{{article_specs}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论