Creating nested JSON objects with identical keys in JavaScript

{"func" : "sprint", "nest" : {"func" : "walk", "nest": {"func" : "run"}}}

The provided code snippet demonstrates the structure of a nested JSON object.

These nested objects can vary in complexity, from a single object to multiple levels of nesting. The goal is to invoke methods starting from the innermost object and moving outwards.

In this specific example, there are two nested values. How can we retrieve and call them in the following sequence:

func : run

followed by:

func : walk

and finally:

func : sprint

Answer №1

If you want to explore a nested object, consider using a recursive method like a depth-first search.

function depthFirstSearch(object) {
    return [...(object.nest ? depthFirstSearch(object.nest) :[]), object.func];
}

var data = { func: "sprint", nest: { func: "walk", nest: { func: "run" } } };

console.log(depthFirstSearch(data));

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

Library for HTML styling in JavaScript

Is there a reliable JavaScript library that can automatically format an HTML code string? I have a string variable containing unformatted HTML and I'm looking for a simple solution to beautify it with just one function call. I checked npmjs.com but co ...

Utilize Bootstrap accordions nesting inner accordions to create a dynamic user interface with varying shown.bs.c

I am currently dealing with a nested bootstrap accordion issue. The problem arises when I click on the inner accordion - it unexpectedly triggers the 'shown.bs.collapse' event bound to the outer accordion, which is not the intended behavior. Is t ...

What is the proper way to implement React's Link component from an external npm package to avoid the error message stating, "you should not use link outside router"?

A custom NPM package has been developed to display an icon menu that can be used across multiple projects. Users have the flexibility to provide a 'route' prop to each icon, allowing them to act as links to different pages. Despite importing and ...

Error: Mongoose failed to cast DBrefs value to ObjectId

I am facing an issue with my Team Schema and Match Schema setup. I want the home/away teams in the Match Schema to refer to the Team object. However, I am encountering an error while trying to save the Team. I suspect there might be an issue with the Schem ...

Stop React router from changing routes when the back button is pressed and also close any open pop

Within my React application, I've integrated a popup on a specific page. However, when the user opens the popup and then hits the browser's back button, they are directed to the previous page rather than simply closing the popup. I've tried ...

Vue buttons causing table content to vanish

I've encountered an issue with my edit and delete functions where clicking the buttons in the table causes the data to disappear. Below is the full code snippet for reference: <h1>Player Rankings</h1> <!-- More HT ...

New update: Adjusting the chart by using the slider to modify the date values on the x-axis

Update: I have made changes to the question, the issue with values not appearing on the x-axis has been resolved. Now, as the slider is moved, the data on the graph remains constant and I can see changing x values. I am currently working on incorporating ...

Issue: Unable to navigate the dependency graph: Module '@badeball/cypress-cucumber-preprocessor/steps' cannot be located

Encountering an error when running my feature file and unsure of the issue. See screenshots below: https://i.sstatic.net/Ks5Sy.png https://i.sstatic.net/BP4rl.png https://i.sstatic.net/8IoMM.png ...

Tips for triggering a child component function during parent events:

Scenario Vue 2.0 documentation and various sources such as this one emphasize that communication from parent to child components is done through props. Inquiry How can a parent component inform its child component that an event has occurred using props? ...

Different ways to attach or cycle through various background images with text

I have a collection of images stored in a folder, and I am looking to randomly select one of the images to display as a full background image while also showing the text description related to that specific image. Here is the code snippet for achieving t ...

How can I use a mouse scroll wheel to activate the "hover" event using Javascript?

Is there a way to change the default behavior where using the mousewheel to scroll down the page does not trigger any hover events? I want the hover event to be triggered even if the pointer is hovering over a div and regardless of whether the user used th ...

Include vue-videobg in Vuetify using CDN without the need for webpack

I am trying to integrate vue-videobg into vuetify using a CDN without any additional requirements like "magic_videobg_to_install.java.css.js.htm" <!DOCTYPE html> <html> <head> <link href="https://fonts.googleapis.com/css?family=R ...

Employing the map() function or a similar method to transform the array data into the specified format

Today, I decided to have some fun with the map() function in JavaScript. I am working with an array and trying to return the data from the pages property. However, I want the page id to serve as the key, and the index of that page within the pages array to ...

What is the proper method for setting initial values for scope upon loading the view using AngularJS and ngInit?

For the last few weeks, I've been immersing myself in AngularJS, studying large-scale applications to gain insights into real-world development practices. One common pattern I observed is the use of ng-init="init()" when loading a view - essentially c ...

Is it possible to develop an asynchronous function using Javascript?

Take a look at this code snippet: <a href="#" id="link">Link</a> <span>Moving</span> $('#link').click(function () { console.log("Enter"); $('#link').animate({ width: 200 }, 2000, function() { c ...

tips on customizing buttons within form groups

I have set up two separate form-groups: My goal is to click on each button within each group and toggle its style from grey to green, and vice versa. However, when I click on a button, all buttons within the group turn grey except for the one that was cli ...

Simulating a typescript class import with enzyme for testing purposes

I need to conduct a test on this Typescript class using Jest, here is a snippet of the code: import OrderService from '../../../services/api/OrderService'; class InstallationOverview { // using OrderService somewhere // ... } When I use en ...

A step-by-step guide to implementing cron jobs in Blitz.js

Seeking guidance on cron job implementation with Blitz.js I am in need of assistance with setting up a cron job using Blitz.js. I have already carefully reviewed the Blitz.js documentation. Progress so far I have thoroughly checked the Blitz.js document. ...

Guide on filtering an array's keys using another array's numeric keys

I am working with two arrays. The structure of the first array is as follows: items: [ { name: "a", items: [ { name: "jack" }, { name: "jose" }, ] }, { name: "b", ...

Executing AngularJS controller upon view initialization

I am working on an angularJS single page application that loads views per click. My login and Index are combined in one HTML file. I have managed it using ng-show and ng-if directives. The process is as follows: Index.html and IndexController.js: The log ...