Creating a new JSON object in Vue by filtering and computing values

I have a JSON object coming from my server with the following structure:

    [
   {
      "id":1,
      "tag":"Cooking",
      "weight":null,
      "deleted_at":null,
      "created_at":null,
      "updated_at":null,
      "listings":[
         {
            "id":5,
            "name":"Learn how to make the perfect hotpot with",
            "slug":"learn-how-to-make-the-perfect-hotpot-with",
            "description":"We live in a unique and wonderful time where electric guitars are things that we can rock on. ...awesome rockin’.",
            "booking_details":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore... "25.00",
            "active":1,
            "moderated":1,
            "user_id":3,
            "category_id":null,
            "deleted_at":null,
            "created_at":"2020-06-18T13:41:05.000000Z",
            "updated_at":"2020-06-24T22:50:48.000000Z",
            "tagsList":"Cooking",
            ...
         }
      ]
   },
   ...
]

The objective is to filter out listing objects with a price of "0.00". The current attempt is not yielding desired results, possibly due to navigating through multiple nested objects. Here's the current logic:

filteredListings() {
            return this.tags.filter((listings) => {
                return listings.filter((l) => {
                    console.log(l.cost);
                })

            })
        }

Any assistance or guidance on refining the filtering process would be greatly appreciated.

Answer №1

I attempted to solve the problem using a combination of map and nested filter. Please give this code a try and let me know if it addresses your issue:

givenArray.map(({items, ...others})=>{
    items = items.filter(item=>Number(item.price)!==0);
    return {...others, items};
});

Answer №2

Give this a shot:

filteredListings() {
    return this.tags.map(({ listings, ...rest }) => 
        ({ ...rest, listings: listings.filter(listing => listing.cost === '0.00' })
    )
}

Here's the breakdown: Instead of trying to filter the tags directly, focus on filtering the listings within each tag. By using the map function to iterate over each tag and filter its respective listings, you'll achieve the desired outcome.

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

How come the deleteOne and findById methods in mongoose are able to function properly even after the document has

During my development of an API using nodejs to interact with a MongoDB database, I encountered a peculiar issue after deleting a document. My API consists of various endpoints for retrieving all animals in the database, fetching a specific animal using i ...

A Guide to Displaying HTTP Error Messages on the Angular Login Page

When encountering a form validation failure on my login page, I utilize ngMessage to display an error message. However, I now want this div to be displayed in the event of an HTTP 500 error. While I can retrieve the corresponding error message when an HTTP ...

Guide to converting raw Mysql fields into objects using Node.js

I have written a code to retrieve all the rows from the article table in MySQL, but I would like to represent this data in object and array format so that I can send it to endpoints. app.get('/article' , function(req , res){ var connec ...

Adjusting the transparency of the object's interior

I used the side property to give my object an interior, but it also reflects the exterior: let material = new THREE.MeshStandardMaterial({side: THREE.DoubleSide}); https://i.sstatic.net/oikad.png https://i.sstatic.net/Vb03K.png Is there a way to red ...

Save the values of mongoose fields in a variable, then iterate through them

Recently, I've been delving deeper into the world of node.js and mongo, making significant progress. However, I've hit a roadblock. I have managed to successfully push data from a form into a collection using mongoose. I can also see the data bei ...

Is it possible to alter objects through the use of jQuery.map?

I'm working with a key-value pair that looks like this: var classes = { red: 'Red', green: 'Green' }; Is there a way to add a specific value before each key in the pair? Can jQuery.map help with this task? The desired end result ...

What exactly happens when we use [0] in the expression $("#id")[0]?

I believe this code snippet involves an array index, but I'm unsure of the array it's referencing. It's commonly found in code similar to the following example: HTML: <canvas id = "id"> </canvas> JavaScript/jQuery: var canvas ...

I'm struggling to figure out why my code is throwing an Unexpected token error. What am I missing here?

I keep encountering an Unexpected token error in my code, specifically with a closing parenthesis ). What exactly does this error signify? Experimented by adding and removing parentheses as well as curly brackets. const getUserChoice = userInput => {u ...

Searching for a way to display just a portion of a rendered HTML page using JavaScript

With the JavaScript code window.print(), it is possible to print the current HTML page. If there is a div in an HTML page (such as a page generated from an ASP.NET MVC view), the aim may be to only print that specific div. Is there any jQuery unobtrusive ...

Loading two scripts in sequence, the first utilizing the "src" attribute while the second is an inline script

The HTML code below shows two script elements being added to the body of a webpage. <html> <body> <script> const first = document.createElement("script"); first.setAttribute("src", "./remote.js"); first.async = false; const second = doc ...

Updating cluetip content post webpage loading

I need to update the content within a cluetip after the page has finished loading. Imagine there's a button inside the cluetip and upon clicking it, I want it to disappear. Here is the cluetip setup: $('a.notice_tooltip').cluetip({activa ...

The CSS style of the Div element is not being displayed properly when embedded with JavaScript

Currently, I am working on a simple page for practice purposes. The main issue I am facing is with a div element that has a red border and a blue background. Inside the div, there is a script tag calling an external JavaScript file. Surprisingly, the JavaS ...

Having trouble with object initialization in a jQuery GET request?

Looking to create an HTML button using jQuery that, upon clicking the chart button, will retrieve values from specified text inputs. These values will then be used to construct a JSON object which will subsequently be included in a GET request. $(".chart" ...

Prompt user to confirm action with jQuery before proceeding

I am facing an issue with a checkbox in my form. I would like to prompt the user for confirmation before they check the box. However, currently, the confirmation message appears only after the box is checked. My goal is to allow the user to "cancel", so it ...

What is preventing me from creating a perfect circle using the arc() method in Three.js?

While attempting to create a complex shape in Three.js using extruded arcs, I encountered some unexpected behavior. I am unsure if my understanding of the API is lacking, but wasn't this code supposed to generate a full extruded circle with a radius o ...

Utilizing Mongoose.js to nest a find() query within another find() query

Can I perform a nested search on Node using Mongoose? I need to update a user's email address, but first I have to ensure that the new email is not already associated with another user. This is what I want to achieve: /***************************** ...

Trouble expanding Bootstrap Dropdown menu on mobile devices

I am currently facing an issue with a Bootstrap navbar that includes a dropdown menu for selecting courses. On my desktop, everything works perfectly fine when I click on the courses tab. However, when I try to select a course on mobile, nothing happens. ...

Display the appropriate selection choices based on the knockout condition

In my HTML page, there is a dropdown with certain conditions that determine which options should be rendered in the select element. My attempt at achieving this: <select> <!-- ko if: condition() --> <option value="1">1& ...

achieving outcomes beyond iframe boundaries

I currently have an IFRAME that is responsible for loading my login page. Here is the code: <iframe src="loginForm.html"></iframe> Once the form in the IFRAME is submitted, I am looking for a way to retrieve and display the results on the p ...

Incorporate @aspnet/signalr into your Vue application

I'm encountering an issue while attempting to integrate SignalR into my Vue.js application using webpack for bundling modules. Here's a snippet from my package.json: "dependencies": { "@aspnet/signalr": "^1.1.0" } Within my component's ...