Working with inline attributes in Vue JS: Iterating over object properties

I am relatively new to working with vue.js, and I have encountered a challenge that I haven't been able to solve on my own.

Here is the basic structure of my template:

<template v-for="page in $props.data.pageInfo" >
  <div class="overviewItem" :key="page.uid" :data-cat=" **I need to loop through the object to extract both cat_id values** ">
    <div v-for="(cat, index) in page.categories" :key="index" >
      <p v-if="index === 0" class="subtitle">
        {{ cat.title }}
      </p>
    </div>
  </div>
</template>

{{ page.categories }} displays these values for the first .overviewItem:

[
{
  "cat_id": 1,
  "title": "Category 1"
},
{
  "cat_id": 2,
  "title": "Category 2"
}
]

The code to retrieve the subtitle is functioning correctly, but I'm struggling to figure out how to iterate over this object to capture both values for the data-cat attribute.

Answer №1

Utilize the .map method to create a fresh array containing only the cat_id, then combine them with a space in between:

<div
   class="categoryItem"
   :key="item.id"
   :data-category="item.categories.map(category => category.cat_id).join(' ')"
>
...
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deciphering the JSON information for SliderBox

https://www.npmjs.com/package/react-native-image-slider-box according to the example, the react-native image slider box uses an array that contains the paths of images. https://i.sstatic.net/9L1HE.jpg Therefore, it would be correct if we had a JSON array t ...

Need to update various form fields at once with jquery?

There is a form with fields for firstname, lastname, email, country, along with edit icon and submit/cancel buttons. When the user clicks on the edit icon in the top right corner, all values will be displayed in textboxes and the country will be shown in ...

Determining the condition of the menu: understanding whether it is open or closed

I'm diving into the world of jQuery and JavaScript, trying to grasp the ins and outs of the mmenu API. Despite my efforts to understand the libraries, the JavaScript code remains a mystery to me. Following the tutorial provided on , I successfully cr ...

Assistance is required for establishing a connection between php and js. The process begins with executing a sql query to extract the necessary data, which is then encoded into JSON format

I have encountered an issue with a project I am working on solo, involving a sidecart in a PHP file with external links to SQL, CSS, and JS. The problem arose when attempting to insert necessary data into JS using JSON encoding. <?php session_start(); ...

Increasing a variable in MongoDB using Meteor.js based on the value of a variable in a separate document

I am facing an issue similar to this: I am struggling to modify multiple documents simultaneously. click: function() { var power = Meteor.user().power; var mult = Meteor.user().mult; Meteor.users.update({ _id: this.use ...

Choose an element at random

I am trying to figure out how to select all div elements with the same class of "dot" under a parent div in JavaScript. Here is an example structure: <div id="dots"> <div class="dot"> . </div> <div class="dot"> . </div> ...

Is there a way to compare the elements of an array with those of another array containing nested arrays in order to identify matching results?

Every user in our database has specific skills assigned to them. We maintain a list of available skills with unique IDs. The objective is to filter users based on the skill we are interested in, like displaying all Janitors. We are utilizing Vue.js and im ...

The initial request is replaced by new information

Trying to fetch data for autocomplete in Laravel. Controller: public function collection_search(Request $request) { $term = $request->search; $serveurapObj = new Serveurap(); $result = $serveurapObj->collectionAutocomplete(); ...

The Vue component fails to display the updated data even after the prop data has been modified

This is the parent component called abc.vue <v-card outlined class="mt-4"> <DetailsTable v-show="toggleClientData" :columnDefs="columnDefs" :rowData="rowData" /> </v-card> methods:{ aggridData() { let self = this; th ...

React Native Issue: The mysterious disappearance of the 'navigation.push' object

I am trying to navigate to List.js after clicking a button in the MainMenu.js file, but I keep encountering this error: TypeError: undefined is not an object (evaluating 'navigation.push') This snippet is from my App.js file import { StatusBar ...

Guide on generating a route using coordinates in a threejs environment

Currently, I am working with an array of coordinates obtained from the Google Maps Navigation API. I have successfully plotted these coordinates on a sphere, however, my objective is to extract the shape of the path by combining all the coordinate points ...

What is the process for uploading a file using the client's file path?

I'm fascinated by the way Sonic Living is able to identify and upload your iTunes library XML file with such ease. Unlike other upload libraries I've explored, it prompts user approval before automatically uploading the XML file based on client O ...

Detecting Android devices

Issue: My website functions properly on desktop but has a problem with the carousel images skewing on iPhones. To address this, I created a separate CSS styling for iPhone devices and used a JavaScript function found online to detect iPhones and iPads. Un ...

Reverse the action and postpone the Ajax call for a specified time or until the next action occurs

How can I implement a setup and undo feature for my ajax calls that allows users to delay the call for a set amount of time and abort it if needed? I also want to be able to stop the delay and proceed with the most recent ajax call if another action is tri ...

React: Unexpected error occurs with invalid element type

I encountered the following issue while attempting to utilize a component Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forg ...

Github Pages is experiencing a bit of a hiccup with 400 errors due to a problem with the manifest.json file

I'm encountering some issues while trying to deploy my Github Pages website at . The errors I keep facing are as follows: - Failed to load resource: the server responded with a status of 400 () - manifest.json:1 Failed to load resource: the server ...

Unable to alter the state of the object

Can anyone assist me in resolving an issue? I have tried numerous solutions, but none seem to work. Status Update class App extends Component { constructor(props) { super(props); this.state = { countId:{quantityId:[]}, }} The CountId ...

Here are the steps to trigger a pop-up window in JavaScript that is filled with data fetched via an AJAX call:

I'm currently working on a java class where I need to insert a link into a table grid. This code is specific to the internal API of our company. /***MyCustomLink.java*********/ customLink.setOnClick( "fetchURL", return false;); addGridItem("cu ...

What are the top picks for enhancing color to be more vibrant in red, green, and other

When adjusting an RGB pixel map of a picture, which values would result in a more pronounced red, green, blue, cyan, magenta, or yellow appearance? In my current JavaScript code, I am using the following RGB change values to enhance colors, but I am curio ...

What is the purpose of using a height selector in jQuery CSS

I'm trying to set equal height for divs with the class name "modelCode" using a custom function. However, when I run the function, it sets the height to 0px in the element style. Previously, I used the .height() method which worked perfectly. Any idea ...