What is the best way to retrieve all objects from this data model?

I am looking to collect all the Model Objects from the data structure provided below.

const PRODUCTS = [
    {
        brand: 'Audi',
        allSeries: {
            serie: 'A3',
            allModels: [
                { model: 'A3 1.5 Sportback' },
                { model: 'A3 2.0 TDI' }
            ]
        }
    },
    {
        brand: 'Volkswagen',
        allSeries: {
            serie: 'Golf',
            allModels: [
                { model: 'Golf 1.5' },
                { model: 'Golf 2.0' }
            ]
        }
    }
]

My goal is to combine all the model objects into one array structured like

[{model:'Golf 1.5}, {...}, {...}]
.

Answer №1

One way to achieve this is by utilizing the array's map() method. Give this a try and let me know if it solves your problem:

const PRODUCTS = [{ brand: 'Audi', allSeries: { serie: 'A3', allModels: [ { model: 'A3 1.5 Sportback' }, { model: 'A3 2.0 TDI' } ] } },{ brand: 'Volkswagen', allSeries: { serie: 'Golf', allModels: [ { model: 'Golf 1.5' }, { model: 'Golf 2.0' } ] } }];

const models = PRODUCTS.flatMap(product => product.allSeries.allModels);

console.log(models);

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

The Challenge of Refreshing Static Site Generation in NextJS Version 13

I've encountered a problem with updating data on a basic data page. The situation is simple: there's a page that shows category data and another page that allows editing of the same data. After making edits and returning to the list page, I expec ...

What is the process for obtaining the public email address of a Facebook Business Page through the Graph API?

At the moment, I am utilizing the Graph API and the PHP SDK from Facebook to collect data and recent wall posts from a Business Page. This information includes details like name, address, and phone number. However, even though the public email address is ...

Click the button to access the provided link

I need to add a link for redirection to some buttons. Here is an example of the button code: <Tooltip title="Open New Ticket"> <IconButton aria-label="filter list"> <AddTwoToneIcon /> </IconButton> </T ...

Receiving unexpected errors with the JSON response, troubleshooting the issue

When using the App, I am required to log in by providing a username and password which will be authenticated against the database. My current approach involved checking the PHP response without utilizing JSON, but now I want to retrieve all data using JSON ...

Utilize JavaScript to submit the FORM and initiate the 'submit' Event

Hey there! Here's the code I've been working on: HTML : <html> <body> <form enctype="multipart/form-data" method="post" name="image"> <input onchange="test();" ...

Styling the scrollbar for the PDF element on an HTML page

I have a div that contains an object "linked" to a .pdf file. Is it possible to customize the scrollbar style using CSS or JavaScript/jQuery? <div id="container"> <object data="document.pdf" type="application/pdf" ...

Converting JSON data from a PHP variable into a jQuery array: What you need to know

I attempted to retrieve addresses using the postcode and applied the getaddress.io site API with PHP. This resulted in a JSON dataset stored in the PHP variable $file. Now, I am tasked with converting this JSON result into a jQuery array. { "Latitude":-0. ...

Blip Scripts: Converting JSON to JavaScript - Dealing with Undefined Arrays

I am currently working on a project to develop a bot on Blip. There are certain parts where I need to send a request to an API and then use a JavaScript script to extract elements from the JSON response. The JSON response I received from the API is stored ...

Maintain the functionality of an object even when it is created using the "bind" method

I'm working with a function that has the following structure: var tempFun = function() { return 'something'; } tempFun.priority = 100; My goal is to store this function in an array and bind another object to it simultaneously, like so ...

How can I efficiently extract data from JSON by incorporating variables?

Currently, I am utilizing C# to parse JSON data. The following code functions perfectly: var json = webClient.DownloadString("API KEY"); Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json); Consol ...

Invoke a method from a related component within the same hierarchy

Imagine this scenario: You're working on a reusable item carousel. The slide track component and the slide navigation component need to be independent and in a sibling relationship so you can position the buttons wherever you want. But how do you trig ...

The average duration for each API request is consistently recorded at 21 seconds

It's taking 21 seconds per request for snippet.json and images, causing my widget to load in 42 seconds consistently. That just doesn't seem right. Check out this code snippet below: <script type="text/javascript"> function fetchJSONFil ...

Escape key does not close the modal dialogue box

I’ve customized the codrops slide & push menu (http://tympanus.net/codrops/2013/04/17/slide-and-push-menus/) to create an overlay on a webpage. Although it functions as intended, I’m struggling to implement a way to close it by pressing the escape ...

removing an item from a nested array through the use of the filter() method

I have been struggling to remove an element with a specific ID from a nested array. Are there any suggestions on how to effectively utilize the filter() method with nested arrays? The goal is to only eliminate the object with {id: 111,name: "A"}. Below ...

Implementing a delete functionality within a loop on a JavaScript object array

I have a JavaScript object with the following structure: var partner = { p_name: { value: partner_name, label: "Name" }, p_id: { value: partner_ID, label: "ID" }, p_status: { value: partner_status, label: "Status" }, p_email: { value: partner_emai ...

The instance cannot be accessed by ES6 class methods

Having trouble accessing the this instance in one of my methods within a class that I created. In my router, I am calling the method like this: import Emails from '../controllers/emails' import router from 'express' .... route.post(&a ...

Tips for transferring information from a JSON file to a PHP function

[registration] => Array ( [first_name] => test [location] => Array ( [name] => Santa Ana [id] => 1.08081209215E+14 ) [gender] => female [password] ...

leveraging ajax callbacks, the significance of http status codes and essential validation concepts

Recently, I've been working on a server side AJAX class designed to return a JSON string in response to an AJAX request. One question that has been on my mind is when it would be appropriate to return HTTP status codes based on the server's respo ...

Unravel the encoded string to enable JSON parsing

Here is an example of my JSON string structure [{&#034;id&#034;:0,&#034;nextCallMills&#034;:0,&#034;delay&#034;:0,&#034;start&#034;:&#034;... I am facing an issue with JSON.parseString() Even after trying unescape() a ...

Steps to refresh a .ejs page with updated information following an ajax request

I am in need of re-rendering my homepage.ejs with new data on the server side following an ajax request. Although I am aware that you can somehow re-render the elements in the ajax callback, I am interested in finding out if it is possible to simply re-ren ...