Exploring Various JSON Arrays

There are 3 arrays provided below which I have no control over:

groups_array = [ {
    "group" : "Mobile Test Region",
    "id" : "251"
}, {
    "group" : "Mobile Demo Region",
    "id" : "252"
} ]
locations_array = [ {
    "location" : "M Testing",
    "id" : "1376"
}, {
    "location" : "Trade Show Machine",
    "id" : "1403"
}, {
    "location" : "M Trailer",
    "id" : "1471"
}, {
    "location" : "Test Los Angeles",
    "id" : "1475"
} ]
pairs_array = [ {
    "location_id" : "1376",
    "group_id" : "251"
}, {
    "location_id" : "1475",
    "group_id" : "251"
}, {
    "location_id" : "1403",
    "group_id" : "252"
}, {
    "location_id" : "1471",
    "group_id" : "252"
} ]

The following code snippet is used to loop through the pairs_array and fetch the location_id's related to the group id. Upon running this code, it outputs 2 location ID's based on the given groupid stored in e.rowData.groupid.

for (var s = 0; s < pairs_array.length; s++) {
    if (e.rowData.groupid === pairs_array[s].group_id) {
        Ti.API.info(pairs_array[s].location_id); 
    }
}

I intend to compare the strings and retrieve the location names by using the location_id's obtained from the IF statement. Would it be beneficial to push the results into an array and then loop through both the location_array and the results for comparison purposes? If so, could you provide a useful code example since my previous attempts did not yield the desired output.

Answer №1

for (let index = 0; index < pairs_array.length; index++) {
    if (e.rowData.groupid === pairs_array[index].group_id) {
        console.log(pairs_array[index].location_id); 

        // Find the location name
        for(let idx = 0; idx < locations_array.length; idx++)
        {
            if(locations_array[idx].id == pairs_array[index].location_id)
            {
                location_name = locations_array[idx].location;                        
            }
        }
    }
}

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

Break down the string and iterate through using AngularJS

When looking at a string in the format "Something for example $takeThis $takeThisAlso", I need to parse it and extract all substrings that are enclosed within $ ($xxx). This means I want to achieve something like this: foreach($xxx in list) { var field ...

Issue with document.Form.submit not working in combination with window.location in Chrome and some versions of IE 7 and 8

Hey there, I'm currently working on a project that involves conducting surveys. Each page of the survey presents a new question for the user to answer, and upon submission, the user is redirected to the next question. The javascript code below allows ...

Using Sencha Touch for asynchronous HTTP request to Model-View-Controller

Ext.util.JSONP.request({ url: '/Home/GetMessagesMobile', callbackKey: 'callback', params: { lat: geoip_latitude(), lng: geoip_longitude(), ...

Utilizing Typescript in tandem with an external library through es6 modules

Is there a recommended method for incorporating Typescript with non-module libraries like PixiJS and SortableJS without using webpacker? I'm looking to utilize es6 modules but want to avoid cumbersome solutions. What would be the best approach in this ...

Launching Angular Application on GitHub Pages

I am encountering an issue with my GitHub Pages website that is connected to a custom domain. My domain is hosting a web application created with Angular, but I am having trouble loading the .js and .css files when I visit the site. In the DevTools/Networ ...

My function handler is not being triggered when using React's onClientClick

I'm facing an issue with a button component I have, let's refer to it as < MyButton >. <MyButton onClick={this.props.calculate} onClientClick={this.changeColor} > Calculate </MyButton> Whenever I click the button, my cal ...

Collect data entered into the input box and store them in an array for the purpose of

I need assistance with a code that involves input boxes for users to enter numerical values, which are then stored in an array. My goal is to add these values together and display the sum using an alert when a button is clicked. However, I am struggling to ...

Troubleshooting intersecting objects in THREE.js

Having trouble detecting intersections of extruded objects in THREE.js. The objects are created from a 2D geometry as shown below: var geoShape = new THREE.Shape(vertexes); var geometry = new THREE.ExtrudeGeometry(geoShape, { bevelEnabled: false, amount: ...

What is the best way to access a specific attribute of an HTML element?

Within my ng-repeat loop, I have set a custom attribute like this: <div ng-repeat="item in itemsList" stepType="{{item.stepType}}"> {{item.itemValue}} </div> The possible values for item.stepType are 'task' or 'action ...

Dynamically load select options using Ajax

I'm a newcomer to javascript and ajax, and I'm working on implementing Ajax to display the current category on my HTML select in a CodeIgniter project. Below is the code snippet: HTML: <select class="form-control" name="category" id="categor ...

The parameter of type 'never' cannot be assigned with the argument of type 'number | boolean | undefined'

In my project, I am creating a validation input using TypeScript in Next.js. interface InputRules { required?: boolean min?: number max?: number minLength?: number maxLength?: number } I have defined an object that contains methods to handle val ...

Encountering a frustrating Npm error while trying to install a package, which persists in throwing

Encountering an error message while trying to run npm install npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\ node_modules\&bsol ...

The ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

The attempt to cast to a number for the value of "[object Object]" at the specified path failed in mongoose

My schema is structured like this: module.exports = mongoose.model('Buyer',{ username: String, password: String, email: String, url: String, id: String, firstName: String, lastName: String, credit: Number, }); Wh ...

Export web application content to PDF through rendering on the server side

Our interactive web application, which includes multiple d3 charts, is built with vue. Currently, I am able to export our webpage to a PDF file by utilizing canvg and html2canvas to convert the content into PNG format. The PNG file is then transmitted to t ...

Convert several arrays into a PHP object

I've been struggling for a few hours trying to figure out how to accomplish this task. I currently have an array structured like so: array (size=2) 0 => array (size=14) 'NR COMANDA' => string '251729' (length=6) ...

Executing multiple JQuery post requests simultaneously

I am currently working with three functions, each of which posts to a specific PHP page to retrieve data. However, since each PHP script requires some processing time, there is a delay in fetching the data. function nb1() { $.post("p1.php", { ...

Find a way to avoid Google's site-blocking measures

Currently developing a website. I am looking into restricting access to only logged-in users on the site. How can I parse the pages in a way that Google does not block the site? ...

Dynamic components in Angular and the error of ExpressionChangedAfterItHasBeenChecked

Seeking to create a versatile component that can dynamically contain various components. The objective is to compose an article with the flexibility of including either paragraphs or tweets. The following code defines DynamicArticleComponent: @Directive( ...

Is there a simple method to refresh a JSP or Spring MVC page using AJAX?

I'm tackling a seemingly basic question in Java web development here, but I could use some guidance. How can I refresh data on a JSP page? I understand the fundamentals (utilize jQuery for AJAX, Spring MVC for the "Controller" & handle data reque ...