Locating a particular child element within a JSON object

I'm working on displaying a button only if a JSON object contains the element hidden: true.

The child elements are unpredictable.

draw": { "item": { "aircraft": { "hidden": false} }
draw": { "item": { "mounted": { "hidden": false} }

I am using ng-repeat on this JSON, but since I don't know whether it's aircraft or mounted, I can't do something like this:

<button ng-if="json.draw.item.next.hidden" />

How can I request the next element without knowing its name?

Answer №1

Here is a function that you can use:

function retrieveFirstProperty(obj) {
    var keys = Object.keys(obj);
    if(keys && keys.length > 0) return obj[keys[0]];
}

To implement it, simply do the following:

retrieveFirstProperty(json.draw.item).hidden

I have tested this function with Angular.js and it works fine. You can also check out the jsFiddle example provided.

Check out the code snippet below for a demonstration:

function retrieveFirstProperty(obj) {
        var keys = Object.keys(obj);
        if(keys && keys.length > 0) return obj[keys[0]];
    }
var draw1 = { "item": { "aircraft": { "hidden": false} }};
var draw2 = { "item": { "mounted": { "hidden": false} }};

document.write(retrieveFirstProperty(draw1.item).hidden, '<br>');
document.write(retrieveFirstProperty(draw2.item).hidden, '<br>');

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

GWT error: empty stack

Every time I attempt to perform an operation, I encounter this exception. (TypeError): b.k.g.E is null stack: TBe([object Object]) .... What steps can I take to fix this error? ...

Develop a specialized WordPress widget featuring several unique text areas for enhanced customization

class ad_widget extends WP_Widget { function __construct() { $widget_ops = array( 'classname' => 'ad-widget-container', 'description' => __( ' Ad WIDGET' ) ); parent::__construct( 'ad-widget', ...

Identify distinct prefixes and eliminate them from an array of strings

If you have an array of strings in Javascript, is there a way to identify the common prefix among all the strings and then remove that prefix from each string? For instance: ["05098701", "05012302", "0545621", "0509301"] The common prefix in this case w ...

Creating PDFs from DOCX files using NodeJS and TypeScript

I'm struggling to convert a DOCX file to a PDF in NodeJS using NestJS and TypeScript. I've tried several methods, but they all seem to fail: @nativedocuments/docx-wasm: NativeDocuments is not functioning as expected. word2pdf: This project is ar ...

File type cannot be determined on certain devices

I am currently utilizing the antd Upload component in React to handle file input. My specific scenario only allows certain types of files to be uploaded. To enforce this restriction, I am employing the beforeUpload prop of the Upload component to verify th ...

"Troubleshooting the issue with the jQuery function not being able to trigger a

When I press the enter key in an input field, I want to be able to select options from a searchable dropdown menu. However, this functionality is not working and the alert message I set up is also not functioning as expected. <div class="form-group m-f ...

A second request for Json in a partial view named will not trigger in MVC 3

I am encountering an issue with two views that are both utilizing the same partial view. Initially, when the partial loads for the first time, the JSON fires successfully and everything functions as expected. However, once the user navigates to the second ...

What is the best way to incorporate Bootstrap 5 into a content script without causing any conflicts with the existing CSS of the website? (Chrome Extension)

In the process of integrating the Bootstrap 5 library into a Chrome extension, I have encountered an issue where the CSS and JS files conflict with the main CSS file on certain websites. To address this, I have included the Bootstrap 5 (CSS and JS) files i ...

Discovering dynamic content enclosed by static values in Applescript

Just starting out with applescript and facing a challenge. I need to extract a string from a web page via Safari and assign it to a variable. The target string varies, but the words before and after it remain constant. For instance: Apple 1293.34 USD The ...

Ways to create an asynchronous Node.js server

I am currently running a nodeJS server that retrieves JSON data from three different websites and displays it on my own website in JSON format. The data on these external websites is updated every 10 seconds. How can I ensure that my NodeJS server is const ...

Tips for creating more efficient styled components with repetitive styles

Utilizing Styled Components for styling in my project has resulted in numerous icons being defined. The style file contains the following code: my-component.styles.ts import { ReactComponent as CloseIcon } from 'svg/close.svg'; import { ReactCom ...

Get a jQuery object from an array

I have a challenge where I am storing jQuery elements in an array and need to retrieve them. My current approach is as follows: var arr = []; arr.push( $(someElement) ); When attempting to retrieve the element, I use: arr.indexOf($(someElement)); Unf ...

Randomly reorganize elements in a JavaScript array

I am facing an unusual issue while shuffling an array in JavaScript and I am unable to identify the root cause. Can someone provide assistance? When attempting to shuffle an array, the output I receive is unexpected: [1,2,3,4,5,6,7,8,9,10] Instead of ...

Using checkboxes as a method, you can select from a pair

Here's my question: When using checkboxes, we can only choose one value. Check out this Plunk example <input type="checkbox" ng-true-value="Korman Juriy" ng-false-value="" ng:model="query.name"/> or <input type=" checkbox "ng - true - va ...

Avoiding memory leaks in Reactjs when canceling a subscription in an async promise

My React component features an async request that dispatches an action to the Redux store from within the useEffect hook: const fetchData = async () => { setIsLoading(true); try { await dispatch(dataActions.fetchData(use ...

Troubleshooting: Struggling to get ngShow to work in AngularJS

Here is the code snippet that represents my perspective: <ul class="commentslist cf"> <li class="cf" ng-repeat="(key,comment) in activity.comments"> <div class="comment">{{comment.name}} <div class="butto ...

Promise chain failure in express

I've developed an express.js server for managing REST API requests and extracting data from a MongoDB database. However, I'm encountering an issue with the promise chain when I send a GET request to a specific endpoint ("localhost:8081/api/getUse ...

Reload the text content of a locally hosted webpage within an iframe at regular intervals

I have set up a simple webpage on my local machine to showcase the contents of some text files on a dedicated monitor. However, I am facing an issue where refreshing the entire webpage causes flickering. My goal is to only refresh the iframes and reload t ...

The step-by-step guide to deactivating server-side JavaScript on MongoDB using a Java application

I am working on a Java web application that interacts with a MongoDB Atlas database for CRUD operations. My goal is to disable server-side JavaScript for my Atlas instance directly from the Java web application itself. After researching, I came across th ...

Trouble with jQuery .click() function not functioning as expected

After reviewing several stackoverflow questions regarding issues with the .click function not working, I am still unable to resolve my problem. I have correctly imported the jquery libraries. <script src="https://ajax.googleapis.com/ajax/libs/jquer ...