Exploring Vue.js3: Simplifying Nested Array Filtering

Currently, I am in the process of sifting through an Array that contains nested arrays.

To handle this task, I utilized computed and crafted a function called filteredEgg(). However, I seem to be overlooking something, as I'm only returning the main array after filtering for the egg element within each sub-array. Following that, I created a concise function to fetch the name from the egg array.

HTML

<p v-for="egg in filterEgg(digilist)">{{ egg }}</p>

JS

const app = Vue.createApp({
data() {
return {
digilist: [{
egg: [
{
id: "blue",
eggtype: "blue",
name: "Punimon",
},
...
],
baby: [
...
]
}]
}
},

computed: {
filteredEgg(digilist) {
return this.digilist.filter((egg) => {
return egg.name
})
}
},

})

My end goal is to organize creatures based on their evolutionary stage. The filtering function aims to match all evolutions that correspond to specific creatures. For instance, eggtype:"blue" is linked to name:"Punimon",, providing a basic example of how creatures are connected through evolution.

The complete database I've developed is too extensive to include here, but you can view it by visiting this link - https://github.com/SheldonAldridge/Digimon-world-divivolution-guides/blob/main/js/Database/digimon-digivolution-database.js

https://i.stack.imgur.com/UxvwM.png

Answer №1

Initially, passing params to a computed property is not possible while maintaining reactivity. To work with params, consider using a function instead. More information can be found here.

Another point to note, treat a computed property like a property and not a function by using filterEgg instead of filterEgg().

Lastly and significantly, there is no need to pass anything additional. You already have access to the digilist property through the this instance, so avoid redundant passes.

Here is a demonstration of the working solution-

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="690f06071d295d4711">[email protected]</a>/css/materialdesignicons.min.css"
      rel="stylesheet"
      />
    <link
      href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a0a3b3a2bfb0af96e5f8e6f8e3">[email protected]</a>/dist/vuetify.min.css"
      rel="stylesheet"
      />
  </head>
  <body>
    <div id="app"></div>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e2e1f1e0fdf2edd4a7baa4baa1">[email protected]</a>/dist/vuetify.min.js"></script>
    <script type="text/x-template" id="app-template">
      <v-app>
        <p v-for="egg in filteredEgg">{{ egg }}</p>
      </v-app>
    </script>
    <script>
      const { createApp } = Vue;
      const { createVuetify } = Vuetify;
      
      const vuetify = createVuetify();
      
      const app = createApp({
        template: "#app-template",
        data() {
          return {
            digilist: [{
              egg: [{
                  id: "blue",
                  eggtype: "blue",
                  name: "Punimon",
                },

                {
                  id: "green",
                  eggtype: "green",
                  name: "Botamon",
                },

                {
                  id: "orange",
                  eggtype: "orange",
                  name: "Poyomon",
                },

                {
                  id: "pink",
                  eggtype: "pink",
                  name: "Yuramon",
                },
              ],
            }, ],
          };
        },
        computed: {
          filteredEgg() {
            return this.digilist[0].egg.map((egg) => {
              return egg.eggtype;
            });
          },
        },
      })
        .use(vuetify)
        .mount("#app");
    </script>
  </body>
</html>

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

Troubleshooting Nuxt Js PWA: Issues with metadata and theme_color configurations in nuxt.config.js

Currently, I am working in Nuxt on a website with PWA support that is almost ready for live use. However, I have encountered an issue where the theme color does not change upon app load up and also in other applications like Discord. In addition, the meta ...

Is there a reason for the absence of the Revit category attribute in the JSON response retrieved from the GET request :urn/metadata/:guid/

After receiving the information from the endpoint regarding Revit Models uploaded to my bucket, I noticed that the JSON response contains multiple objects. These objects seem to represent Revit elements, each with all parameters except for the Revit Categ ...

Creating a custom Vue.js component for specific table cells within a table row - here's how!

My challenge is having related and neighboring table columns grouped within the same component in Vue.js. However, I'm facing a limitation with the template system where only one tag can be directly placed inside <template>. Typically, this wrap ...

Inaccurate Feedback on Maquest Direction Route API

Currently, I am in the process of implementing the MapQuest API Direction Routing function on my website. However, upon submitting a request to the API, it is returning inaccurate routing points between two destinations. Here is the request form that I am ...

Unable to display the string following a space in the value attribute of an hbs file

<input type="text" class="form-control" id="exampleInputEmail2" name="productName" value={{product.productName}} > When I input 'Smart Phones' into product.produc ...

Is my data secure in Node.js RAM?

I have developed a web application that enables users to create personal pages using Express and Node.JS. Each page is represented as an object, allowing for the creation of new ones with the syntax: new privatePage(name, pswd). As a result, all passwords ...

What is the reason behind the def.reject code in setTimeout not executing when the sendToSocket operation is successful?

What causes the setTimeout function inside the send method to not run if the sendToSocket operation is successful? this.send = function(type, body) { var def = Promise.defer(); try{ sendToSocket({...}); setTimeout(function() { // ...

Dispatch is functioning properly, however the state remains unchanged

I'm currently developing an application that utilizes redux for state management. In a particular scenario, I need to update the state. Here is how my initial state and reducer function are set up: import { createSlice } from '@reduxjs/toolkit&a ...

Instructions for incorporating highcharts sub modules into a React application

I have been utilizing the react-jsx-highcharts library to seamlessly integrate Highcharts into my React application. Everything is functioning perfectly. However, I am now interested in incorporating the boost module. I attempted to add it by simply using ...

PHP code to store the start and end dates of a week in an array

Can someone help me with PHP code to format the start and end date of a week like this? Range: 16-Jan-2018 to 22-Jan-2018 I have the start and end dates, but I'm struggling to store them in an array for multiple weeks under one index. For example: ...

Tips for eliminating unnecessary data in written content

Could anyone provide a recommended method for removing unnecessary symbols from text strings? For instance, transforming "CWC%20-%20Maint%20Eng%20-%20El" into the more readable format of "CWC - Maint Eng - El". ...

Transmit data from an HTML form to PHP using JSON

I am attempting to utilize JavaScript to send POST data. I have the data in an HTML form: <form name="messageact" action=""> <input name="name" type="text" id="username" size="15" /> <input name="massage" type="text" id="usermsg" si ...

Frequently refreshing a page to display the most recent information without needing to reload the entire

I am seeking a solution for updating the comments section on my website live or every 30 seconds. The comments are fetched from a MySQL database using PHP: <?php $link = mysql_connect('localhost', 'root', ''); ...

Express.js applications may encounter issues with static files and scripts not being found when utilizing dynamic endpoints

I've run into a snag with my Express.js application regarding the inability to locate static files and scripts when accessing certain endpoints. Here's what's happening: My Express.js app has multiple routes serving HTML pages along with st ...

What is the best way to utilize Office365 Excel array formulas to find the minimum and maximum values for each individual?

Looking at the test data provided below, how can we design the array formula to achieve the expected outcome? =LET(item,G4,name,A4:A7,data,C4:E15, names, CHOOSECOLS(data,1), items, CHOOSECOLS(data,2), cnt, CHOOSECOLS(data,3), mn, MIN(I ...

Changing a JavaScript string into an array

I have extracted an array of objects from a hidden HTML input field and received the following string: "{"id":"1234","name":"john smith","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dab0a9b7b3aeb29ab8b6bbb2f4b9b5b7" ...

I must create text that is transparent against a colorful gradient background

Hey there! I'm seeking help in figuring out how the text on this site is designed. You can take a look at it here. Essentially, what I'm aiming for is to have the text color match the gradient of the background color from the previous div, while ...

Dealing with models in Vue.js is proving to be quite a challenge

Why isn't myGame showing as 超級馬力歐 initially and changing when the button is pressed? It just displays {{myGame}} instead. I'm not sure how to fix it, thank you! let myApp = new vue({ el:'myApp', data:{ myGame:&a ...

Sending data to server with Backbone (using save() function and php script)

Wondering if you can assist me with an issue I'm facing. I am trying to utilize Backbone's save() method and send data to a php file. However, I encountered a problem right at the beginning. When I execute the following in the browser's co ...

Binding multiple forms in backend ASP.NET MVC using Managed Extensibility Framework (MEF)

We are facing a challenge where we need to send multiple forms in one Ajax (jQuery) Call to an ASP application. The jQuery code we are using is as follows: var formContainer = { Form1 : form1.serialize(), Form2 : form2.serialize() } ...