Searching for a nested object in Javascript using regular expressions

Presenting my data structure:

var foo = {
          "Category1": [
            {"Company1": {"URL": ["DomainName1", "DomainName2"]}},
            ...
          ],
          ...
          }

Typically, I access DomainName1 like this:

foo["Category1"][0]["Company1"]["URL"][0]

Now, I need to search all of foo for a specific DomainName without any additional information. Using multiple nested loops would be too slow. Is there an efficient way to achieve this? I was considering replacing ["Category1"], [0], etc. with a wildcard symbol '*', but I'm unsure how to implement that.

Any suggestions or guidance on this matter would be highly valued.

Answer №1

While some may see my response as biased, I strongly believe that attempting to recreate the wheel with tens of thousands of objects is unnecessary. This situation calls for utilizing a database storage solution, such as SQL or non-SQL options like MongoDB.

Answer №2

If you're looking for a solution to this problem, consider using Jsonpath.

For example, you can employ the following expression to find all URLs:

var out = jsonPath(json, "$..URL[*]").toJSONString() + "\n";
document.write(out);

You can customize an expression to filter for specific domains.

For your scenario, try using this expression:

$..URL[?(@.indexOf('DomainName1') != -1)]

Check out this Jsonpath online tool

Here's a practical example of using Jsonpath from the documentation:

(pre-written code block omitted for brevity)

Just input the desired expression to effortlessly navigate through your JSON data structure.

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

Determine the dimensions of the objects within a JSON array

My JSON response creation looks like this : { "G5LDUHRPEEA6B-39CFBWYA": [], "JMSK0DKOEEA0UXMY750O3W": [], "ISVN8JF1EEAD3W398ZNOSA": [ { "delloData": "1478644629", "ref": "75", "dataType": "String", "somePart": LA, ...

Invoke the parent method within the child application

I have a checkbox on the child app controller. When the user clicks it, I need to call a method from the parent controller: Parent app controller: <div ng-controller="ParentCntr as parentCntr"> <child-app></child-app> </div> C ...

The onload event for windows does not fire

I am currently working on a project that requires the use of tabbing control with flyingbox. To achieve this, I consulted the following link: After referring to the above link, I made some modifications to my project. I am retrieving details from another ...

Storing Data in SQL Server Database with ajax and asp.net

After utilizing ajax and asp.net for inserting a file into my SQL Server 2012 database, I encountered an error message displayed below: my error localhost:6446 says : [object Object] within the realm of HTML and ajax jquery $(document).ready(functi ...

Is there a way to use HTML, JavaScript, and CSS to manipulate an object's opacity when a button is clicked?

I am looking to add a button on my website landing page that can hide the weather section, but I am unsure how to go about it! https://i.sstatic.net/COA82.jpg Currently, I am using a "selector" where I can choose either 0 or 1 from the bottom right corner ...

Guide on Developing a JavaScript Library

After creating several JavaScript functions, I noticed that I tend to use them across multiple projects. This prompted me to take things a step further and develop a small JavaScript Library specifically for my coding needs. Similar to popular libraries l ...

Incorporate JSON into the selection choices

After working with PHP for a while, I decided to start learning how to work with JSON. I managed to successfully create a PHP file that returns JSON data. In my main file, I referenced the PHP file and was able to retrieve the JSON objects. One of the thi ...

Tips for modifying the JSON format within a Spring and Angular project

I am utilizing Spring for the backend and Angular for the frontend. Below is my REST code: @GetMapping(path = "/endpoint") public @ResponseBody Iterable<Relations> getGraphGivenEndpointId(@RequestParam(value = "id") int id) { return ...

Using regular expressions in the route attribute of a .NET Core WebAPI to define

How can I create a route that matches requests with multiple paths and always ends with /end? For instance: domain.com/api/path1/path2/path3/end domain.com/api/path1/path2/path3/path4/end I attempted to use [Route("api/{p:regex(([[\\w-]]*&bso ...

Removing item from Angular service

Within my Angular 2 application, I have created a service called events.service.ts: const EVENTS = { 1512205360: { event: 'foo' }, 1511208360: { event: 'bar' } } @Injectable() export class EventsService { getEvents() ...

Issue with Cloud Code function preventing data from being saved

After successfully testing this code in Angular and getting the correct responses in console.log, I decided to migrate it to cloud code. Since the function manipulates data in the user table, I had to use the master key and implement it in cloud code. Howe ...

What is the best way to change the state of an Object?

I need to dynamically adjust the internalstatus based on the values of externalValues. If valueOne is true, then I want statusOne to be true. Similarly, if valueTwo is true, I want statusTwo to be true and statusOne to be false. const externalValues = { v ...

Is there a way to specifically retrieve HotelDetails from the provided JSON data?

When attempting to display only HotelDetails in a table, the first three rows appear as undefined before displaying the data in the fourth row. Here is the code I have used: $.ajax({ type: "GET", url: 'http://localhost/priya/hote ...

Guidelines for updating state in React following a delay?

Trying my hand at creating a basic roulette wheel using React, I encountered an issue where setting the state spinning to false at the end of the function did not seem to have any effect. import React, { useState } from "react"; const numbers = ...

What is the most efficient method for constructing a JSON string in Java?

This is my first time working with JSON on the server. The object I'm trying to create should resemble something like this: { columnData : { column1 : {"foo": "bar", "value" : 1 }, }, rowData : { row1 : {"type" : "integer", "value" : 1 ...

The alignment of the point on the Highchart line appears to be off

I've encountered a minor issue while using Highstock type Highchart for my statistics. I am utilizing the angular version of highchart. Take a look at the screenshot below: https://i.sstatic.net/HPCkw.png As evident from the image, the point is not ...

Error Encountered in MERN Stack Development: TypeError Thrown Due to Inability to Convert Undefined

Currently, I am following the MERN Stack, Front To Back Course by Traversy Media. I am in the process of setting up the login functionality for the application. Despite ensuring that my code matches his exactly, I am encountering an error: TypeError: Canno ...

Switch the div's class when the parent element is chosen

I have encountered a complex issue involving two problems for which I am unable to find a solution. In the default state without hover or selection, this div displays with a purple border when selected. My concern is: How can I change the class of the ico ...

Click to slide the div down and up when needed

Currently, I am utilizing the code below to implement a slide up/down effect on a div using JavaScript. The slide down action is triggered by a button, while the slide up action is associated with a close text. My goal is to have the button toggle betwee ...

Utilizing JavaScript to iterate through objects retrieved via Ajax calls

Recently, I've been diving into the world of Javascript and delving deep into AJAX. Utilizing Vanilla JS along with simple AJAX, my current task involves fetching a user object from a URL based on the user's input ID. Despite attempting to use .d ...