[英]How can I loop through an object with Vue, similar to PHP?
I have a my object as follows我有一个 object 如下
$object_zero = {
'one' : 'one year',
'two' : 'two year',
'three' : 'there year',
'four' : 'four year',
'five' : 'five year',
'six' : 'six year',
'seven' : 'seven year',
'eight' : 'eight year',
};
I have a my other object as follows我有一个我的其他 object 如下
$object = { 'one' : '1 Year', 'two' : '2 Year', 'three' : '3 Year', 'akta' : '12', 'mars' : '48' }
I can do it in php as follows.我可以在 php 中进行如下操作。
foreach($object_zero as $key => $val){
echo $object->$key;
}
so how do i do this with vue.js?那么我该如何使用 vue.js 做到这一点?
I could not start it.我无法启动它。 my goal is to just write text containing the keys of the first object
我的目标是只编写包含第一个 object 键的文本
<div v-for="(v, k, index) in data.object_zero">
<p v-if="object.k" class="mb-0">
{{ object.k }} -
</p>
</div>
For example, only these should be written on the screen.例如,只有这些应该写在屏幕上。
print: 1 Year - 2 Year - 3 Year
You can loop through the properties of an object in a Vue template like this:您可以像这样在 Vue 模板中遍历 object 的属性:
<div v-for="(value, key) in object" :key="key">
{{ key }}: {{ value }}
</div>
Keep in mind that, strictly speaking, objects do not have a defined order in JavaScript.请记住,严格来说,对象在 JavaScript 中没有定义的顺序。 Vue.js takes the order of
Object.keys()
, which might have an output you are not expecting. Vue.js 采用
Object.keys()
的顺序,它可能有一个 output 您不期望。 Usually you would want to use arrays in such a case.在这种情况下,通常您会想要使用 arrays。
See also the documentation: https://v2.vuejs.org/v2/guide/list.html#v-for-with-an-Object另请参阅文档: https://v2.vuejs.org/v2/guide/list.html#v-for-with-an-Object
Arrays do not have keys in JavaScript. Arrays 在 JavaScript 中没有键。
notAnArray : {
'one': '1 Year',
'two': '2 Year',
'three': '3 Year'
}
See this example with arrays in Vue.请参阅 Vue 中的 arrays 示例。
<div v-for="(val, index) in array" :key="index">
<p>
{{val}}
</p>
</div>
array : [
"1 Year",
"2 Year",
"3 Year"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.