How to get value from an array in vue.js

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Data Access: How to Get Value from Nested Arrays in Vue.js

As developers working with modern front-end frameworks like Vue.js, we constantly deal with complex data structures—often nested arrays and objects retrieved from APIs. A very common hurdle is successfully navigating these structures to extract the specific pieces of information we need for rendering.

Let's dive into a practical scenario using your provided example, where you have a post object containing an array of categories, and you want to display the names of those categories.

The Challenge: Navigating Nested Data

Consider the data structure you are working with:

{ "id": 1, "title": "Incidunt facere placeat nulla occaecati voluptatem voluptatem minus.", "categories": [ { "id": 1, "name": "News", ... }, { "id": 2, "name": "Sport", ... } ] }

You correctly noted that simply trying to access post.categories.name results in null. This happens because post.categories is an array, and arrays do not have a direct property named name. To get the names, you must iterate over the array and look inside each object within it.

The Solution: Using Array Methods (.map())

The most idiomatic and powerful way to transform an array into a new array of derived values in Vue (or plain JavaScript) is by using the .map() method. .map() iterates over every element in an array, applies a function to that element, and returns a brand new array containing the results. This is crucial for maintaining Vue's reactivity system efficiently.

Step-by-Step Implementation

To extract just the category names from your structure, you would apply .map() to the categories array:

// Assuming 'post' is your data object in your Vue component state
const post = { /* ... your full object data ... */ };

// 1. Access the categories array
const categoriesArray = post.categories;

// 2. Use .map() to transform the array into an array of names
const categoryNames = categoriesArray.map(category => category.name);

console.log(categoryNames); 
// Output: ["News", "Sport"]

Integrating into Your Vue Component

In your Vue component's script setup or data block, you would perform this transformation when the data is loaded. If you need to display these names in your template, you simply reference the newly created array.

<template>
  <div>
    <h1>{{ post.title }}</h1>
    <h2>Categories:</h2>
    <ul>
      <!-- Iterate over the derived array -->
      <li v-for="categoryName in categoryNames" :key="categoryName">
        {{ categoryName }}
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';

const post = ref({ 
  "id": 1, 
  "title": "Incidunt facere placeat nulla occaecati voluptatem voluptatem minus.", 
  "categories": [ 
     { "id": 1, "name": "News", /* ... */ }, 
     { "id": 2, "name": "Sport", /* ... */ } 
  ] 
});

// Computed property to handle the transformation reactively
const categoryNames = computed(() => {
  return post.value.categories.map(category => category.name);
});
</script>

Best Practices for Data Handling

When dealing with relational data, like the category pivot shown in your example, understanding how these relationships are structured is key. In frameworks that rely on strong data modeling (like Laravel), understanding Eloquent relationships helps simplify this process significantly. For instance, defining proper relationships means you interact with related data through defined methods rather than manually iterating over raw JSON structures.

If you are fetching this type of complex nested data via an API, ensuring your backend provides the data in a structured way is paramount. Think about how you structure your Eloquent models and relations when designing APIs; clean relational design makes front-end data access much smoother. For robust data handling practices, studying frameworks like those used by Laravel can provide excellent context on structuring data efficiently.

Conclusion

To effectively extract values from nested arrays in Vue.js, always shift your focus from direct property access to array iteration methods. The .map() function is your best friend for transforming an array of complex objects into a flat array of simple values, making your component logic clean, readable, and highly reactive. By mastering these fundamental JavaScript techniques, you can handle any level of data complexity thrown at you in your Vue applications.