Utilizing Vue.js to effectively filter within a nested array

I'm currently facing an issue with filtering a nested array within an array of objects in Vue.js. Take a look at this snippet from my component code:

computed: {
          filteredProducts: function () { // https://codepen.io/arhey/pen/QrbxdX
            return this.products.map(product => {
                return product.filter(p => {
                    return this.powers.includes(p.total_power_lamps);
                });
            });
        }
    },

The data is being filtered successfully, but the changes are not reflecting on the page.

filteredProducts: Array[6]
0: Array[2] <- Filtered!
1: Array[2] <- Filtered!
2: Array[0] <- Remove?
3: Array[0] <- Remove?
4: Array[0] <- Remove?
5: Array[0] <- Remove?

I am unable to update the data on the page as there are empty arrays present. How can I go about deleting these empty arrays?

Answer №1

The map function generates an array with the same length as the original one. My suggestion is to opt for using the filter method instead of map if you only want to return arrays that are not empty:

computed: {
          filteredProducts: function () { 
            return this.products.filter(product => {
                return product.filter(p => {
                    return this.powers.includes(p.total_power_lamps);
                }).length>0;//return only filled arrays
            });
        }
    },

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

Field must have a base type specified to proceed

Currently, I am in the process of developing a discord.js bot. The structure I have set up involves a folder that contains all the commands, and when a command is called, index.js directs it to the appropriate file. However, when attempting to run the bot, ...

Exploring the world of promise testing with Jasmine Node for Javascript

I am exploring promises testing with jasmine node. Despite my test running, it indicates that there are no assertions. I have included my code below - can anyone spot the issue? The 'then' part of the code is functioning correctly, as evidenced b ...

JavaScript function for validating CGPA or GPA fields

I am encountering issues with JavaScript validation in ASP.NET. The validation seems simple, but for some reason, it is not functioning properly. Below is the code that I have tried: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.as ...

Adjust the Appearance of Highcharts Legend Post-Rendering

After a Highchart is rendered, is it possible to modify the display settings without redrawing the chart? For instance, I would like to relocate the legend box from the right to the bottom upon screen resize, as illustrated in this image: --Example Pictur ...

Ways to initiate JavaScript event upon clearing input form field

I'm working on creating a dynamic search feature. As the user types in the search box, JavaScript is triggered to hide the blog posts (#home) and display search results instead (the specific script for this is not shown below). However, when the user ...

jQuery does not have the capability to access the href attribute through DOM manipulation

I've been trying to extract the href attribute from a link in my code and create a new link using that attribute. However, I'm facing an issue where the created link doesn't seem to work properly - it keeps showing a 404 error message like t ...

Generating a primary XML element encompassing multiple namespaces

I am currently working on integrating Restful services with Backbone.js framework. In this project, I need to send XML data and add multiple namespaces to it. Here is the snippet of my current JavaScript code: var mainNamespace = "xmlns='http://serv ...

Can Big Data flow on Mongo DB be effectively simulated?

My idea involves creating a server with routes and models to be hosted on Heroku for processing requests. I plan to make incremental requests to the server until they qualify as Big Data. I suspect there may be limitations on how many consecutive requests ...

Guide on accessing intellisense for mapGetters, mapActions in Vuex using TypeScript without the need for class-style or decorators syntax

I have been using Vue.js and Vuex for a while now, but always with JavaScript. Recently, I decided to try using Vue with TypeScript, specifically with nuxt.js, but without utilizing decorators or style-class-components. I want to stick to the normal Vue s ...

Receiving an alert when the information has been loaded into a computed field

I am currently working on a Vue project that utilizes Vuetify, and I am trying to implement the loading functionality. The items in the project are loaded using a computed field, but now I need to figure out how to manipulate the 'loading' proper ...

Discover the steps to showcasing product images within Shopware 6's user-friendly administration interface

Working on a specialized Shopware 6 Administrative plugin, integrating product images has proven to be quite challenging. After thorough investigation of the Shopware repositories such as 'product_media' and 'media', along with other re ...

Having trouble uploading an image using the Cloudinary API in a Next.js project

I am currently working on a page meant for creating posts. Initially, the page was designed to handle multiple image uploads. However, I made some adjustments so that it can now accept only a single image upload. Unfortunately, after this modification, the ...

Converting a Complex Array of Objects into a CSV File for Download in a React Application

My current data is available for download in xls/csv format, but when using the react-csv npm package, the name and description are being displayed in the same column instead of separate columns. I am seeking assistance on how to display the data with Nam ...

Update the second dropdown list based on the selection made in the first dropdown list in Vue.js

My goal is to dynamically change the options in the second select list based on the value selected in the first one. I initially achieved this by creating two separate Vue instances for each select, but I wanted a more streamlined approach for a cleaner ap ...

Using a boolean checkbox with Spring MVC and implementing ajax

On my HTML page, I have a boolean checkbox that looks like this: <input type="checkbox" id="pnrCheckbox" name="includesPnr" value="true"/> <!-- This field is generated by Spring as a workaround for something --> <input type="hidden" name="_ ...

Crafting a trail of breadcrumbs using AngularJS

Trying out the angular-breadcrumb plugin has proven to be a bit challenging for me. When I attempt to add the dependency using 'ncy-angular-breadcrumb', it results in an error. The module 'ncy-angular-breadcrumb' is not found! Seems ...

Tips for effectively overlaying one container on top of another in a responsive manner

There are a pair of containers. The main container functions as the parent, while the child container acts as a tag to categorize the content. The objective is to position the tag container at the top right corner of the main content container, slightly of ...

Is it possible to include spaces in a JavaScript alert message?

Is it possible to add spaces in an alert message? I have been trying to include spaces in my alert messages, but the alerts do not appear when there are spaces. Example where it works: https://jsfiddle.net/yczrhztg/ Example where it doesn't work: ht ...

Tips for monitoring Google Analytics/ Adwords Conversions within VueJS

I have developed a VueJS application in which users submit a form, and the data is sent to the server using Vue-resource. I want to track this as a conversion for Google Analytics. Google has provided me with a script that needs to be placed on a page lik ...

Rendering multiple data in a single cell using Vue.js Template

Hey there, I need some help figuring out how to display this data using Vue.js. I want to show this set of data in a single variable. Any ideas? I attempted to use notification.data.user_id but it's not showing anything on the screen. <div v-for ...