Using Lodash to extract and sort information from various parent and child components

My information:

[
   {
      "configData":[
         {
            "repoSettings":[
               {
                  "BaseURL":"45df16eb-55f9-48a9-9df8-428984524d7b",
                  "CurrentBranch":"Publish_1522323990",
                  "CurrentHeadSHA":"85c599a6ab3ecf8bfb469479da",
                  "RepositoryId":1528,
                  "RepositoryName":"45df16eb-55f94524d7b"
               }
            ]
         },
         {
            "pageSettings":[
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"index.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     }
                  ]
               },
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"about.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     },
                     {
                        "Sidebar":"default"
                     }
                  ]
               },
               ,
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"about.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     },
                     {
                        "Sidebar":"default"
                     },
                     {
                        "MenuBar":"default"
                     }
                  ]
               }
            ]
         }
      ]
   }
]

Within this data, my goal is to loop through all objects in pageSettings and within each one, iterate over the partials array to search for specific key-value pairs. For example, if I have a pair with "Header" as the key and "default" as the value, I want to search for this combination across all pageSettings and partials objects. If found, return true for that specific pageSettings index. Is it achievable using Lodash? Alternatively, could you provide a code snippet to accomplish this task?

Answer №1

Here's an interesting method to explore. Try using forEach() on the array and extract keys and values from partials.

let data = [
   {
      "configData":[
         {
            "repoSettings":[
               {
                  "BaseURL":"45df16eb-55f9-48a9-9df8-428984524d7b",
                  "CurrentBranch":"Publish_1522323990",
                  "CurrentHeadSHA":"85c599a6ab3ecf8bfb469479da",
                  "RepositoryId":1528,
                  "RepositoryName":"45df16eb-55f94524d7b"
               }
            ]
         },
         {
            "pageSettings":[
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"index.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     }
                  ]
               },
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"about.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     },
                     {
                        "Sidebar":"default"
                     }
                  ]
               },
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"about.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Header":"default"
                     },
                     {
                        "Footer":"default"
                     },
                     {
                        "Sidebar":"default"
                     },
                     {
                        "MenuBar":"default"
                     }
                  ]
               },
               {
                  "PageCss":[

                  ],
                  "PageExternalCss":[

                  ],
                  "PageExternalJs":[

                  ],
                  "PageLayout":"default",
                  "PageMetaInfo":[

                  ],
                  "PageMetacharset":"UTF-8",
                  "PageName":"about.html",
                  "PageSEODescription":"",
                  "PageSEOKeywords":"",
                  "PageSEOTitle":"",
                  "PageScripts":[

                  ],
                  "PageStyles":[

                  ],
                  "partials":[
                     {
                        "Sidebar":"default"
                     },
                     {
                        "MenuBar":"default"
                     }
                  ]
               }
            ]
         }
      ]
   }
]

function find(key, value) {
 let matchingIndex = [];
 data[0].configData[1].pageSettings.forEach((i, settingIndex) => {
    i.partials.forEach(j => {
      let matching = Object.keys(j).filter(k => k === key && j[k] === value);     
      if(matching && matching.length) {
        matchingIndex.push(settingIndex);
      };
    })
 })
 
 console.log('The values ('+key+','+value+') have been located at setting index: '+ matchingIndex);
}

find('Header','default');
find('Sidebar','default')

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

Python script to convert a CSV file into JSON format

Currently, my goal is to import data from a csvfile, convert it into a list of dictionaries, and then save the output as a JSON in jsonfile. The code below outlines what I have so far. However, when I attempt to open and load the JSON file, I encounter a ...

Exploring the Benefits of Employing PHP's json_encode Function with Arrays

$cnt = 0; while ($row = $result->fetch_assoc()) { $arre[$cnt]['id'] = $row['idevents']; $arre[$cnt]['title'] = $row['title']; $arre[$cnt]['start'] = "new Date(" . $row['start'] . " ...

Add new data to an existing array in Angular 7 without overwriting it

After clicking a button, I'm retrieving data from the WordPress API: <a (click)="initGetComments()">Get comments</a> This is the code snippet: export class AppComponent { commentsResults: CommentsItem[] = []; ...

The ajax success function is not recognizing the if statement

Looking for help with HTML code <div class="vote"> <div data-post-id="@item.PostId" data-vote-rank="1" class="vote-arrow vote-up glyphicon glyphicon-chevron-up"></div> <div class="total-vote">0</div> <div data- ...

Utilizing Yii2 charts with real-time AJAX updates

I am in the process of developing an application utilizing yii2 and I would like to incorporate the miloschuman chart extension. Currently, I am using: echo Highcharts::widget([ 'scripts' => [ 'modules/exporting', 'the ...

Inspect the JavaScript file for errors and find a solution to overcome them

Our website's select box has been updated to retrieve city options from a JavaScript array file using an ajax call request. This file is now dynamically created on a different server and then transferred to the static server where it is used to popula ...

React Bootstrap hover animation technique

UPDATE: I decided to give react-bootstrap a try for tooltips, hoping it would finally work as expected. Unfortunately, even after implementing the code, I am still facing issues with getting the tooltip effect to show up when hovering over the button. Th ...

add the current item in front of the last one

My goal is to determine if an element is the last one in the container div by checking the length of the next element with the class .item. If this next element exists, it means that I am not currently at the end of the container div. if($(this).next(&apo ...

Differences between Sequelize classMethods and instanceMethods

Embarking on my Node journey, I've decided to explore Sequelize. Initially, my focus was on understanding the following code: 'use strict'; var crypto = require('crypto'); module.exports = function(sequelize, DataTypes) { var U ...

Using jQuery to authenticate Ajax requests in CouchDB

Whenever I try to access a protected CouchDB database on localhost using $.ajax, an alert pops up requesting the username and password. If I cancel the prompt, CouchDB triggers an error and my $.ajax error function is not called. However, if I include auth ...

What occurs when socket.io events are not properly handled?

Is socket.io ignoring or dropping messages? I am asking this because there is a client with multiple states, each having its own set of socket handlers. The server notifies the client of a state change and then sends messages specific to that state. Howeve ...

Javascript Egg Timer

I am currently in the process of developing a unique egg timer application that allows users to input values through various form select tags. However, I have encountered a NaN error when trying to set the timer with the JavaScript code provided below. H ...

What is the best way to identify a specific list item from a dropdown menu and determine its position on

Check out my jsfiddle for reference. My goal is to calculate scores based on different options selected, specifically relating to radiation types. I'm struggling with assigning a score to each type of radiation and integrating the drop-down menu selec ...

Every time I attempt to reuse my components, they keep piling up on top of each other

I'm facing an issue where I need to reuse components I've created multiple times while rendering dynamic content. However, when I attempt to render them, they end up stacking on top of each other in the same position. Each time I render ...

Configuring a Node application for a production server with Babel

I am in the process of preparing my very first node app for a production server. The build and serve scripts I am using are based on those provided by babel "scripts": { "start": "nodemon --exec babel-node server.js --ignore public/", "build": "b ...

Is it possible to utilize Grand Central Dispatch for parsing JSON in the connectionDidFinishLoading method?

While attempting JSON deserialization using JSONTouch, I encountered a problem where the process was taking too long and causing the UI to become unresponsive. To address this issue, I decided to move the serialization task to a background thread using a G ...

Cookies set by ExpressJS Cookie-Parser do not remain across multiple HTTP requests

Currently, I'm utilizing Cookie - Parser in conjunction with express.js. Within my express config file, I have included app.use(cookieParser()). In the main (app.js) file for the server, I am setting the cookie whenever there's a POST request to ...

steps for using the push method to create a new array in JavaScript

Currently, I am attempting to create a new array that is similar to the myCourses array using the push method. However, for some reason it only seems to be logging one string at a time instead of generating a new array that mirrors the myCourses array: ...

Can you restrict the selection of text to only the current element?

<div class="container"> <div class="item1">text text text in div element 1</div> <div class="item2">text text text in div element 2</div> </div> Is there a way using HTML nodes, CSS, or JavaScript to restrict the select ...

Encountered a 404 error while trying to install body-parser

Hey there, I'm new to this and ran into an issue while trying to install the body-parser package. Can you please guide me on how to resolve this problem? D:\Calculator>npm install body-paeser npm ERR! code E404 npm ERR! 404 Not Found - GET htt ...