Exploring JSON with JavaScript

[
{"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"},
{"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"}
]

The data above represents a JSON object.

var searchBarInput = TextInput.value;

for (i in recentPatientsList.length) {
 alert(recentPatientsList[i].lastName
}

I am successfully receiving alerts for the last names. Now, I have a text input where users can search for a specific value within the JSON data. In this case, they are searching for the last name value.

How can I extract the input value and use it to search within my JSON data?

Answer №1

Here is an example of how to properly iterate over an array:

var searchBarInput = TextInput.value;

for (var i = 0; i < recentPatientsList.length; ++i) {
 alert(recentPatientsList[i].lastName);
}

It's important to use a for loop with an index variable when iterating over arrays, rather than using the "for ... in" mechanism.

To make a comparison between the text input and the "lastName" field of list entries, you would do something like this:

for (var i = 0; i < recentPatientsList.length; ++i) {
 if (searchBarInput === recentPatientsList[i].lastName) {
   alert("Found at index " + i);
 }
}

Answer №2

Avoid using for..in to loop through arrays; opt for a traditional for loop instead. If you need to retrieve objects with a specific last name, consider filtering the array.

var filteredObjects = patientsArray.filter(function(object) {
    return object.lastName === searchInput;
});

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

Building on the Vuejs3 and Vuex4 framework, create a conditional rendering feature that triggers upon

I have a unique setup where custom objects are used to hold child objects full of data. The child objects start with null values for all properties, allowing them to be filled with data from remote sources when referenced. This results in a lazy-loading sy ...

What is the functionality of this.$eval in Vue.js?

During the process of updating an unfamiliar old script from version 0.11 to 2.5.4, an alert popped up stating: To address the warning message saying 'Replace this.$eval('reportData | reportFilter false') with a solution using normal Java ...

Error: The name property is not defined and cannot be read in the Constructor.render function

Having trouble building a contact form and encountering an error when trying to access the values. I've checked for bugs in the console multiple times, but it seems like something is missing. Can anyone provide assistance? var fieldValues = { ...

What is the best way to choose CSS class attributes using Mootools and the getStyle()

Seeking to duplicate an object, I am trying to figure out how to retrieve class CSS attributes from Mootools. css: .card { width: 109px; height: 145px; } html: <div id="cards"> <div class="card" id="c0"> <div class="face fron ...

Error: "$$" not defined in this context

I am currently working on generating a dynamic pie chart programmatically with the intention of transforming it into a reusable React Component. The main idea is to have an interactive pie chart where each slice expands into a full pie when clicked. I came ...

Problem with converting geopoint to JSON string

Utilizing Gson, I am converting a class with two GeoPoints into a JSON string. The snippet of code below is found within my implementation of the LocationListener interface... lat = (float) loc.getLatitude(); lng = (float) loc.getLongitude(); alt = (float ...

Navigating the website with curtain.js and anchor tags

Currently, I am working on a website located at www.TheOneCraft.co.uk. I have incorporated the curtain.js jQuery plugin to create animated slide/pages as users scroll down. However, I have been unsuccessful in making the navigation bar follow this animati ...

DataGrid Component: Implementing a Button for Deleting Rows

I am currently developing a React component with MaterialUI, incorporating a Datagrid to manage a queue of files. The layout resembles this: One functionality I'm focusing on is when the user clicks on the three dots at the end of each row, a Menu (c ...

Creating a new attribute within a JavaScript object by utilizing the properties of its sibling elements

Imagine a scenario where you have a complex array of objects structured like this: [ { "title": "Fundamentals", "order": 1, "lessonRef": "fundamentals", "children": [ { "title": "History", "order": 1, "lesso ...

Integrate a button following the first paragraph exclusively when there are two or more paragraphs present

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> jQuery(document).ready(function($) { if ( $('p').length < 1 ) { $('p:last-child').after('<div id="toggle" class="btn"> ...

Attempting to design a form that will trigger a print dialog box displaying all the information entered in the form

I am currently working on developing a form that will allow users to input information such as Name, Age, Gender, Hobbies, Contact details, and Photo in order to create a resume. My goal is to build a simple local HTML-based application that generates a RE ...

Finding the total number of nodes within a JSON string is a straightforward process that involves analyzing the

Can the number of nodes in a JSON be calculated using SQL? { "File":[ { "ID":1, "Fragment":"Frag1" }, { "ID":2, "Fragment":"Frag2" }, { "ID":3, "Fragment":"Frag3" }] } Is it possible to determine the ...

Angular: Leveraging Nested Callbacks for Efficient HTTP Queries

I'm currently facing an issue with structured English. GET HTTP Resource FOR every data item received do GET another HTTP Resource Alter the original data from the outer loop with data from the inner GET RETURN altered data How can ...

AngularJS Cascading Dropdowns for Enhanced User Experience

There is a merchant with multiple branches. When I select a merchant, I want another dropdown list to display the data from merchant.branches. The following code does not seem to be fixing the issue: <label>Merchant:</label> <select ng-if= ...

"The changes made to the list are not being accurately displayed by Angular's ng

I am working on a directive that injects dynamic templates during ng-repeat. While I can successfully add items to the list, they do not appear in the view for some reason. It seems like I am missing a crucial piece to make it work correctly. You can find ...

Trouble with Chakra UI loading Images from local sources

I'm running into issues when trying to load local images using the Chakra UI library in combination with Next.js. <Image src="./homepage/footer/Black.svg" /> Here is the current folder structure: When checking my console, I see the f ...

Is there a substitute for System.Web.Helpers.Json.Encode() in .net core 3.1?

I'm in the process of converting an MVC application built on the .Net framework to .Net Core 3.1, and I've come across a situation where the System.Web.Helpers.Json.Encode() methods are being used. Since System.Web.Helpers is not compatible with ...

The ajax success response transforms when using @html.raw

In my Razor viewpage, I have the following jQuery code: $(document).ready(function () { var listValues = @Html.Raw(Json.Encode(Session["list"])); $("#nsline").click(function () { alert(listValues) $.ajax({ type: "P ...

Encountered an issue while trying to read properties of undefined (specifically 'meta') within a Vue 3 single-spa application

I've been working on a micro-frontend project, utilizing vue 3 and single-spa 5.9.3. Recently, I attempted to update one of the npm packages for a Micro-frontend to the latest release. The build process went smoothly, but it resulted in the following ...

Leverage the Power of Multiple Markers on Google Maps with API Integration

My project involves creating a WordPress site that displays one marker on a map and has a list of additional locations below it. I aim to remove the description under the map and replace it with a simple list of locations alongside markers on the map. ...