Creating a multi-dimensional map object using JavaScript

Presented here is a data structure:

tree = [
  {
    "name": "Men Section",
    "categories": [
       {
         "name": "Clothings",
         "Subcategories": [
            {
              "name": "Jackets",
              "products": [
                 {
                    "name": "jacket 01",
                    "price": 100
                 },
                 {
                    "name": "jacket 02",
                    "price": 140
                 },
                 // ..and so on
              ]
         ]
       },
       // ..and so on
    ]
  } // ..and so on
]

In javascript (ES5 or ES6), how can one add a new property isSelected: false to the products item object, resulting in:

{
  "name": "jacket 01",
  "price": 100,
  "isSelected": false
}

?

Answer №1

This method has proven to be effective as it meets my needs

Using a tree structure, we iterate through each base, followed by categories and subcategories, then set all products within each subcategory to have isSelected property as false.

Answer №2

It seems like I need to navigate through the object hierarchy using nested foreach loops. Here's the solution I've come up with:

tree.categories.forEach(function (category) {
  category.subcategories.forEach(function (subcategory) {
    subcategory.products.forEach(function (product) {
        product.isSelected = false;
    })
  })
})

Grateful for the insight!

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

React, connecting form inputs

I've encountered an issue with binding the value of an input in one of my app's components. Strangely, I had no trouble doing this in another component, but now I'm only able to get the first letter of the input instead of the entire text. ...

Enhance a query parameter in a Node.js application

When using Node.js / Express, I define a path and pass in a request and response. My goal is to always include a seed URL parameter when this path is accessed. Initially, I set the seed query param to 0 and redirect the URL. Next, I want to randomize tha ...

How can I retrieve JSON keys and values in JQuery and append them to a list (ul)?

Can you please review this jsFiddle link: http://jsfiddle.net/xY7tx/3108/ Whenever I use getJSON in the provided example, why does the fail part always get executed? I am seeking assistance on how to extract the key and value from the JSON file using jQu ...

Passing a class as a parameter in Typescript functions

When working with Angular 2 testing utilities, I usually follow this process: fixture = TestBed.createComponent(EditableValueComponent); The EditableValueComponent is just a standard component class that I use. I am curious about the inner workings: st ...

When additional elements follow, the button ceases to function properly in JavaScript

I am working on creating a text-based idle game that involves multiple buttons and text around them. However, I have encountered an issue where the functionality stops working when I try to add text after the "Work" button. The callback function is no lon ...

Why does Vue.js Vuex dispatch an action when importing module stores into a helper file?

I recently developed an application using Vue.js. The app is divided into several modules, each corresponding to a specific route and containing a main component with numerous sub-components/children. Every module has its own store, actions, mutations, get ...

Are two JavaScript functions conflicting with each other?

Seeking assistance with integrating a Javascript/PHP/AJAX clock into my website to display various timezones (tutorial link: ) The clock implementation is functional, but it conflicts with an existing javascript stopwatch on the page, causing the clock no ...

"Implement a feature that allows for infinite scrolling triggered by the height of

Looking for a unique solution to implement a load more or infinite scroll button that adjusts based on the height of a div. Imagine having a div with a height of 500px and content inside totaling 1000px. How can we display only the initial 500px of the div ...

FNS Date-Timezone Abbreviation

Is there a way to shorten the Australian Eastern Daylight Time abbreviation to just AEDT? When I use it currently, it displays as 11/11/2022 15:29:25 Australian Eastern Daylight Time. I would like it to show as 11/11/2022 15:29:25 AEDT import { formatInT ...

Adjust the HTML 5 range slider to update according to the selected value

Is it possible to create a custom marker on an HTML5 range input element? I'm looking for a way to change the design of the circle marker as it moves along the scale from 1 to 10. Specifically, I want to change the color of the marker based on its po ...

Issue with Responsive Font Size functionality in Tailwind and ReactJS not functioning as expected

I'm really struggling with this issue. I grasp the concept of mobile-first design and have successfully made my website's layout responsive. However, I'm having trouble getting the font size to change when applying breakpoints as the screen ...

Utilizing LocalStorage in conjunction with the redux state management

Currently, I am working on a single-page application using React and Redux. I have encountered the need to store certain data locally and ensure that it remains synchronized with the appState in local storage, even after a page refresh. Despite being new ...

Having trouble fetching JSONP data with $.getJSON

I am attempting to make a JSONP request for data from , but unfortunately, it's not functioning properly. If you visit the provided URL, you will be able to view the JSON data. I am personally converting an ASP.net object to JSON, so if there are any ...

Error encountered while attempting to import the mongoose npm module into a React application

I'm encountering an issue in my React project where I am trying to include an npm module within a component. Here's an example: var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test', {useNewUrlParser ...

The Angular controller that has been injected into the app is not defined

In an effort to enhance my simple Angular app, I decided to modularize it. I separated the controller into its own file within a dedicated directory. By employing dependency injection, I ensured the controller's availability in the app module. Combini ...

Prevent the upward arrow from appearing on the first row and the downward arrow from appearing on the last row when

This is a straightforward question, but I'm unsure how to achieve it. I am currently working on sorting columns using jQuery. $(document).ready(function(){ $(".up,.down,.top,.bottom").click(function(){ var row = $(this).parents("tr:f ...

Incorporate a .php webpage into a div using Ajax and Javascript

(I'm still learning the ropes here.) I've created a page where users can toggle between different sorting options for posts. Depending on their selection, the corresponding content should be displayed. function loadNewContent() { $.ajax({ ...

Easy steps for entering date and time into a Vuetify text input field

Looking to create a Vuetify form that allows users to input both the date and time of an event for both the start and end times. Currently, my code looks like this: <v-form @submit.prevent="addEvent"> <v-text-field v-mod ...

Interact with embedded elements in JavaScript by using the onClick event

Here is a JavaScript code snippet I've been working on: <div> <tr onClick="click1()"> <td> click 1 </td> <td onClick="click2()"> click 2 < ...

What is the best way to pass a websocket instance to Vue.js components and then invoke the send() method on it?

I am attempting to send a message via a web socket to the server when a button is clicked: // HelloApp.vue <template> <div class="hello"> <h1>{{ msg }}</h1> <button v-on:click="sendMessage($event)">Send Message< ...