Create a Vue component that utilizes the v-for directive to iterate through a list of items, passing data from a

Imagine having two arrays with a similar structure like this:

const individuals = [{name: "Jane Doe", id: "0001"},{name: "John Doe", id:"0002"}, {name: "Sam Smith", id: "0003"}, {name: "Joe Bloggs", id:"0004"}];

const additionalData = [{id: "0001", data: "some data"},{id:"0002", data: "some more data"}, {id: "0003", data: "some further data" }, {id:"0004", data: "even more data"}];

You have a loop structured like this:

<componentName v-for="i in individuals" :person="i" :others="" />

The goal is to pass the data attribute from the additionalData array for the corresponding id without using computed values that take parameters. Can you achieve this by running an additionalData.filter within each loop iteration?

Answer №1

Have you thought about consolidating the data before passing it to the Vue component?

const combinedData = people.map(person => ({
   ...person,
   extras: extras.filter(({id}) => id === person.id).data
}));

// By doing this, you will get
[
   {
      id: '001',
      name: "Jane Doe",
      extras: [
         {
            id: '001',
            data: "more data"
         },
         {
            id: '001',
            data: "even more data"
         }
      ]
   }
]

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

When working with Angular 12, the target environment lacks support for dynamic import() syntax. Therefore, utilizing external type 'module' within a script is not feasible

My current issue involves using dynamic import code to bring in a js library during runtime: export class AuthService { constructor() { import('https://apis.google.com/js/platform.js').then(result => { console.log(resul ...

Expanding a class functionality by incorporating a method through .prototype

My goal is to define a class called "User" and then add a method to the class using the "prototype" keyword. I want the method "who_auto" to be accessible to all future instances of "User". When testing this code in JSFiddle, I encountered the error messa ...

Error: Attempts to access the 'avatar' property of a null value result in a TypeError

I've been attempting to showcase both an avatar and the user name, but I keep encountering this error. Despite trying to declare a user variable to troubleshoot, it's not resolving the issue. Error: Cannot read property 'avatar' of n ...

Waiting for state changes in React by using the UseState Hook

I am currently working on a function that manages video playback when clicked, and I need to set consecutive states using the useState hook. However, I want to ensure that the first state is completed before moving on to the next setState without relying ...

Guide to building a personalized autocomplete feature in vue js

Presently, I am using the buefy autocomplete feature, but it is causing a few problems. In the DepartmentDetail.vue file <template slot-scope="props"> <div class="container is-fluid"> <b-loading :is-full-page=" ...

What sets apart v-model from :model-value?

I'm curious because I have trouble grasping the distinction between v-model and :model-value. According to the information in the documentation: v-model is used on a native element: <input v-model="searchText"/> However, when used on ...

JavaScript Asynchronous Functions Not Handling Await Calls Correctly

The two fadeInList functions control the fading animation of a continuous list split into two lines. The typeOutText function displays text and is supposed to execute List1 first, wait for it to finish, and then proceed with List2. However, after adding ke ...

If the v-model is an array, the Vuejs radio buttons will be automatically checked

Is it possible to have a checked radio button in v-for when using an array as the v-model? Here is my data structure: data() { return { persons: { name: [], surname: [], sex: [], } } } ...

In mvc.net 4, Ajax is only compatible with the httpGet method and not with httpPost

There are two methods, httpGet and httpPost, inside the Login action. Currently, when an ajax call is made, it only works with the httpGet method of Login. I would like it to work with the httpPost method of Login. Thank you in advance for your answer! ...

Using JavaScript to choose an option within an optgroup

Is there a way to automatically select the option in the optgroup when I choose a user from the select list? Here is an example of the select code: <select name="city"> <optgroup label="Zone 1"> <option value=" ...

JavaScript keydown event for rotating images

I am experiencing an issue with JavaScript animation. I have incorporated code from this particular link into my keydown function. However, the code is not functioning as expected. While the code from the provided link works fine on its own, within the key ...

Child element casting shadow over parent element

I am currently using box shadow for both the parent (.map) and child (.toggle-button): .map { position: fixed; top: 20px; right: 0; width: 280px; height: 280px; z-index: 9999; box-shadow: 0px 1px 6px 0px rgba(0,0,0,0.3); } .map ...

Leveraging kazupon/vue-i18n within a Vuex module

Utilizing the Vue-i18n Library for Localization The functions Vue.t() || $t() || trans() expect a string as a key to be translated by vue-i18n Hello everyone! I'm currently experimenting with the code below: import Vue from 'vue' expor ...

What is the best way to populate all data in select2 (4.0) upon page load?

I'm currently utilizing the select2 plugin (v.4.0) and have a specific goal in mind: $("#search-input-chains").select2({ placeholder: "Unit", theme: "bootstrap4", ...

Automated task scheduled to execute every minute between the hours of 8am and 4.30pm using Cloudwatch Events

I am facing an issue with my working CRON schedule. It currently runs from 8am to 5pm and I need to change it to end at 4:30pm. Is it possible to set a specific half-hour time interval in CRON? Below is the current setting for my CRON: 0/1 8-17 ? * MON- ...

The div remains unchanged when the button is clicked

My webpage is filled with several large div elements. There's a button on the page that, when clicked, should switch to the next div. Despite my efforts, the code I've written doesn't seem to be working as expected. :( This is the HTML st ...

Node.js user attempting to upload and handle files without any external libraries, solely relying on traditional JavaScript and HTML techniques

Previously, my Node.js code seamlessly integrated with any javascript+HTML project I worked on, leading me to believe there was a direct correlation between Node.js and vanilla Javascript+HTML. However, this misconception was shattered when attempting to u ...

"The value of a variable in jQuery's 'animate' function can be dynamically adjusted

Looking to smoothly animate a variable using jquery. For example: Starting with a variable value of 1, we want it to reach 10 after 5 seconds. The transition should be smooth and increase gradually. I hope this clarifies what I am trying to achieve. Tha ...

Navigate to the editing page with Thymeleaf in the spring framework, where the model attribute is passed

My goal is to redirect the request to the edit page if the server response status is failed. The updated code below provides more clarity with changed variable names and IDs for security reasons. Controller: @Controller @RequestMapping("abc") public clas ...

"Every time an Ajax call is successful, the 'else' clause in

When it comes to using Ajax for user login in the system, I encountered an issue where the Ajax success function would always run the else statement even if the server returned a true Boolean value. This meant that even when the login credentials were vali ...