How can I loop through an object with Vue, similar to PHP?

How can I loop through an object with Vue, similar to PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Mapping PHP Object Iteration Logic to Vue.js Templates

Transitioning from procedural PHP logic to reactive Vue templates requires a shift in mindset, specifically regarding how data is accessed and rendered. In your PHP example, you are iterating over $object_zero to retrieve values from a second object, $object, using the keys of the first as indices for the second. This pattern relies on direct property access ($object->$key).

In Vue.js, attempting to replicate this exact logic inside a template often leads to confusion because the template engine handles data differently than server-side rendering. Your current approach uses v-for to iterate over data.object_zero, capturing the value and key as v and k. However, the condition object.k attempts to access a property on the root component instance (or an incorrectly scoped variable) rather than the local loop variable. Furthermore, inside a template, you cannot directly use dot notation (object.k) to access a sibling object's property unless that object is passed down as a prop or exists in the specific scope where v-for is executed.

To achieve the output "1 Year - 2 Year - 3 Year", you need to ensure both data sources are available within the same component instance and then correctly reference them using bracket notation within the template expression.

Correcting Variable Scope and Access

The most common pitfall in your proposed code is the variable name collision. You defined v as the value inside the loop, but then tried to access object.k. In a Vue single-file component (.vue), if you are not passing an explicit object named object, you likely need to reference the data property directly, such as this.data.object.

However, a more idiomatic Vue approach is to pass both objects into the v-for iteration or ensure they are accessible at the parent scope. If your component's data looks like this:

export default {
  data() {
    return {
      object_zero: ['one', 'two', 'three'],
      object: {
        one: '1 Year',
        two: '2 Year',
        three: '3 Year',
        akt a: '12',
        mars: '48'
      }
    };
  }
};

(Note: The source material provided keys as strings in the PHP object, but your Vue object definition implies numeric or string keys. We must assume the keys match to avoid undefined behavior.)

Inside the template, you should access the second object using bracket notation based on the loop key k. This allows dynamic property access:

<div v-for="(val, key) in data.object_zero" :key="key">
  <p class="mb-0">
    {{ data.object[key] }}
  </p>
</div>

Here, data.object_zero iterates over the keys of your first object. The :key attribute is crucial for Vue to track list updates efficiently. Inside the {{ }} interpolation, data.object[key] dynamically fetches the value from the second object using the current key from the loop.

Handling the "Undefined" Pitfall

A critical consideration in this pattern is data integrity. In your PHP example, you iterate through $object_zero and access $object->$key. If a key exists in $object_zero but not in $object, PHP throws a notice but continues. In JavaScript/Vue, accessing data.object[undefined_key] returns undefined.

If your goal is strictly to print values that exist in both objects (similar to checking if the property exists before printing), you must add an existence check. Without this, the UI might display "undefined" for keys missing in the second object.

<div v-for="(_, key) in data.object_zero" :key="key">
  <p v-if="data.object[key] !== undefined" class="mb-0">
    {{ data.object[key] }}
  </p>
</div>

This ensures that only keys present in the second object render content. If you specifically want to filter the loop entirely based on the intersection of both objects, you should handle this logic in your JavaScript data or computed properties rather than inside the template. For instance, creating a computed property that filters object_zero to only include keys found in object keeps your template clean and declarative.

Performance and Best Practices

While iterating over arrays of strings (keys) is generally performant, be mindful of how you structure your data. If object_zero represents years and object contains the actual text for those years, it is often better to store them as a single flat object or an array of objects in Vue:

data() {
  return {
    years: [
      { id: 'one', label: '1 Year' },
      { id: 'two', label: '2 Year' }
    ]
  };
}

This structure eliminates the need for dynamic property access entirely, making your template simpler and less prone to scope errors. However, if you are constrained by legacy data structures similar to your PHP source, the bracket notation approach with explicit existence checks remains the robust solution for mapping keys between two distinct objects in Vue.js.

Our network