Extract specific nested elements

Looking for assistance with extracting specific nested objects from a series structured like so:

data = {"12345":{"value":{"1":"2","3":"4"}},
   {"12346":{"value":{"5":"6","7":"8"}},
   {"12347":{"value":{"9":"0","11":"22"}}

In need of creating a function that targets and retrieves particular objects within this hierarchy. For instance...

grabObject(12345);

will result in:

{"value":{"1":"2","3":"4"}}

Your guidance on this matter would be highly appreciated.

Answer №1

You can achieve the desired result with just this simple code:

function getObject(id) {
    return database[id];
}

Once you correct your syntax errors, you'll have a functioning jsFiddle example: http://jsfiddle.net/exampleuser/abc123/

Answer №2

var info = [
{
    "12345": {
        "value": {
            "1": "2",
            "3": "4"
        }
    }
},
{
    "12346": {
        "value": {
            "5": "6",
            "7": "8"
        }
    }
},
{
    "12347": {
        "value": {
            "9": "0",
            "11": "22"
        }
    }
}
];


function findData(id) {
   var result;
   for (i = 0; i < info.length; i++) {
       for (var k in info[i]){
         if(k == id) {
           result = info[i][k];
         }
       }
   }
   return result;
}
console.log(findData('12345'));

I ran this code to verify, please confirm it.

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 can I modify the base color of a cone in Three.js?

My cone in the jsfiddle link is currently all blue, but I want to change the color of the square base to red. To do that, I need to alter the color of the two faces that make up the square base. How can this be achieved? View my cone on JsFiddle: http://j ...

Unable to define an object within the *ngFor loop in Angular

In order to iterate through custom controls, I am using the following code. These controls require certain information such as index and position in the structure, so I am passing a config object to keep everything organized. <div *ngFor="let thing of ...

The element is being offset by SVG animation that incorporates transform properties

I'm working on creating a halo effect by rotating an SVG circular element using the transform rotate function. I've been using getBox to find the center point of the element, but when the rotation occurs, the overall image is getting misaligned w ...

Is it possible to ensure only one value is set as true in useReducer without manually setting the rest to false

I am seeking a more efficient method to ensure that only one value is set to true while setting the rest to false I came across this Python question and answer recommending an enum (I am not very familiar with that concept) Currently, I have the followin ...

Encountered an issue with formControlName being undefined during the render process in Angular 2

I encountered an issue while implementing Reactive form validation following the official Angular documentation. The error message "username not defined" is causing trouble in my project. Here's the detailed error: Error: Uncaught (in promise): Error: ...

Looking for a JavaScript code to create a text link that says "submit"?

Here is the code I have, where I want to utilize a hyperlink to submit my form: <form name="form_signup" id="form_signup" method="post" action="/" enctype="multipart/form-data"> ... <input type="submit" value="Go to Step 2" name="completed" /> ...

Initiate an Ajax request solely for the elements currently visible on the screen

I am currently facing an issue that requires a solution. Within our template, there are multiple divs generated with the same classes. Each div contains a hidden input field with the ID of the linked target site. My task is to utilize this ID to make aja ...

Update the innerHTML content dynamically every 5 seconds with Vue.js

I'm working on a new website and I'd like to spice things up by changing the header text with a random word every 5 seconds. Here's the starting point: const header = document.querySelector('.header') const needs = ['jacket& ...

To effectively execute a JQuery / Javascript function, it is essential to incorporate both $(document).ready and $(document).ajaxSucces

I have a JavaScript function that is used for basic UI functionality across my site. Some elements affected by this function are injected via Ajax, while others are static HTML. Currently, I have duplicated the function and applied it to both $(document). ...

Angular 8's array verification feature lacks the ability to recognize preexisting elements

I've been trying to add and delete items in an array when a user selects or deselects the same item. However, it appears that either my array is not working properly or there is a bug in my code causing it to fail. <div class="grp-input"> ...

When setting the form action attribute in jQuery, the parameter being passed may be null

My goal is to redirect my form action to another page with a URL parameter. Here's the code I'm using: ui.setFormActionToTargetPage = function () { $('form').get(0).setAttribute('action', 'myTargetPage.html?id=' ...

Customize the position of the Datetimepicker

Is there a way to customize the position of the datetimepicker, ensuring that it stays within the visual boundaries without getting cut off? I need help with this. ...

Encountering the issue of "TypeError: Cannot read property 'file' of undefined" when trying to upload a video with Multer

The objective is to select a video file from the desktop and upload it to the platform. The uploaded video file will then be automatically posted to a specified Youtube channel using GCD. An error message I encountered is:"react-dom.development.js:40 ...

Importing the .css file within a React component for dynamic styling

In my component, I am trying to dynamically load a .css file based on the result of an AJAX call in componentDidMount(). I have attempted adding a <link> element in the render return (which did not work), and also tried injecting the tag directly int ...

Establishing the module exports for the NextJS configuration file

I have explored different solutions for adding multiple plugins to the next.js config file, with composition being the suggested method. However, I am encountering issues with this approach. const Dotenv = require('dotenv-webpack'); const withSt ...

angular ensuring seamless synchronization of objects across the application

This question pertains to both angular and javascript. In our angular app, we have numerous objects from the backend that need to remain synchronized. I am facing challenges in establishing efficient data bindings to ensure this synchronization throughout ...

Executing an http.get request in Angular2 without using RxJS

Is there a method to retrieve data in Angular 2 without using Observable and Response dependencies within the service? I believe it's unnecessary for just one straightforward request. ...

Leveraging Angular 6: Implementing custom scripts on a component basis and verifying their presence

I need some help with a script that I want to run on a specific component only. I've managed to add the script to the component, but there are a few issues that I'm unsure how to fix. When I go to the component, the script is added to the DOM b ...

Horizontal rule located within a table but spanning the entire width

I wrote the following code: <table> {item.awards.map((obj,i) => <tbody> <tr> <td>Name</td> <td>:</td> <td>{obj.title}</td> </tr> ...

React Router integration problem with Semantic UI React

Just diving into ReactJS and encountering a problem with using "Menu.Item" (from Semantic UI React) and React Router. I won't include my imports here, but rest assured they are all set up correctly. The constructor in my "App.jsx" looks like this: ...