13

I have a my object as follows

$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 = { 'one' : '1 Year', 'two' : '2 Year', 'three' : '3 Year', 'akta' : '12', 'mars' : '48' }

I can do it in php as follows.

foreach($object_zero as $key => $val){
     echo $object->$key;
}

so how do i do this with vue.js?

I could not start it. my goal is to just write text containing the keys of the first 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
4
  • An array in JS is something looking like x = [1, 2, "nice", true, "okay"]. So, basically your $array and $object are the same in JS, both are objects. Also, what exactly are you trying to achieve as an end result: some i18n with one/ 1 year/one year? Commented Apr 16, 2021 at 15:54
  • I want to print the values in the object on the screen. 1 Year' '2 Year' '3 Year' Commented Apr 16, 2021 at 16:08
  • @SemsiPasa Have you tried vanilla JS Object.keys(your_object)? Commented Apr 16, 2021 at 16:27
  • I guess I can't express myself. We put the object_zero in the for loop and get its keys. but as in php in the form object->$ key When we type object.key, we cannot print the data of the other object on the screen. but when there is an array, we can write to the screen as array [key]. Commented Apr 16, 2021 at 18:15

2 Answers 2

20

You can loop through the properties of an object in a Vue template like this:

<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. Vue.js takes the order of Object.keys(), which might have an output you are not expecting. Usually you would want to use arrays in such a case.

See also the documentation: https://v2.vuejs.org/v2/guide/list.html#v-for-with-an-Object

Sign up to request clarification or add additional context in comments.

5 Comments

Maps are also a good alternative if you want to preserve the order.
I guess I can't express myself. We put the object_zero in the for loop and get its keys. but as in php in the form object->$ key When we type object.key, we cannot print the data of the other object on the screen. but when there is an array, we can write to the screen as array [key].
Is object_zero an array or an object? If it is an object, you can use my example above, if it is an array, you can use the example by @tauzN. The variable key and value in my example are automatically set to the property key and value in each loop. If this does not answer your question, you definitely need to elaborate what the problem is.
@ssc-hrep3 "echo $object->$key;" I can print with php this way. value in another object. I'm doing the same with php. "echo $object->one;" -> 1 Year . On the vuejs side, let's take two arrays as an example. Let array1 enter the for loop and get its index. <div v-for = "(item, index) in array1"> {{ array2 [index] }} </div> this works. but in case of an object, let's say object1 and object2 as an example. Let's put object1 into the for loop and get the key value. <div v-for = "(val, key, index) in object1"> {{ object2.key }} </div> it doesn't work that way.
key is in that case a variable. You are accessing the property "key" of object2 instead of the value behind the variable key. Use object2[key] instead instead of object2.key.
1

Arrays do not have keys in JavaScript.

Consider using an object instead, like this

notAnArray : {
    'one': '1 Year',
    'two': '2 Year',
    'three': '3 Year'
}

See this example with arrays in Vue.

template

  <div v-for="(val, index) in array" :key="index">
    <p>
      {{val}}
    </p>
  </div>

Array

array : [
    "1 Year",
    "2 Year",
    "3 Year"
]

6 Comments

This is definitely not what I am asking. I want to use the key of an array and show the value in the object.
@SemsiPasa Arrays do not have keys in JavaScript. Simple as that.
@SemsiPasa See the updated answer for a solution with an object that might work for you.
I realized I was asking the question wrong, sorry, I updated the question.
@SemsiPasa My solution does exactly what you ask. <div v-for="(val, key, index) in array" :key="index"><p>{{val}}</p></div>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.