Looking through a Json file and retrieving data with the help of Javascript

I am currently working on developing a dictionary application for FirefoxOS using JavaScript. The structure of my JSON file is as follows:

[
{"id":"3784","word":"Ajar","type":"adv.","descr":" Slightly turned or opened; as, the door was standing ajar.","track":"a","track_2":"Aj","track_3":"Aja"},
{"id":"3785","word":"Ajar","type":"adv.","descr":" In a state of discord; out of harmony; as, he is ajar with the world.","track":"a","track_2":"Aj","track_3":"Aja"},{"id":"3786","word":"Ajog","type":"adv.","descr":" On the jog.","track":"a","track_2":"Aj","track_3":"Ajo"},
{"id":"3787","word":"Ajutage","type":"n.","descr":" A tube through which water is discharged; an efflux tube; as, the ajutage of a fountain.","track":"a","track_2":"Aj","track_3":"Aju"}                               ]

My current task involves querying this JSON file to find all entries where the word matches "aj" and retrieving the IDs associated with those results. How should I go about achieving this?

Answer №1

TRY THIS CHECK OUT THE UPDATED FIDDLE DEMO

var jsonArrr =[
{"id":"3784","word":"Ajar","type":"adv.","descr":" Slightly turned or opened; as, the door was standing ajar.","track":"a","track_2":"Aj","track_3":"Aja"},
{"id":"3785","word":"Ajar","type":"adv.","descr":" In a state of discord; out of harmony; as, he is ajar with the world.","track":"a","track_2":"Aj","track_3":"Aja"},{"id":"3786","word":"Ajog","type":"adv.","descr":" On the jog.","track":"a","track_2":"Aj","track_3":"Ajo"},
{"id":"3787","word":"Ajutage","type":"n.","descr":" A tube through which water is discharged; an efflux tube; as, the ajutage of a fountain.","track":"a","track_2":"Aj","track_3":"Aju"}                               ];

var matchMe = new RegExp('^' + 'aj', 'i');
    var matches = [];
        for (var i in jsonArrr) {
            if (jsonArrr[i].word.search(matchMe) > -1 ) {

                    matches.push( {'id': i, 'word': jsonArrr[i].word} );

            }
        }

To view the result

 for (var i in matches) { 
                console.log(matches[i].word);
                //your code
                }

Open the console window in Inspect Element to see the outcome

Answer №2

var matchedIndexes = []
for (var i=0; i < array.length; i++) {
    var obj = array[i];
    if (obj.hasOwnProperty("word")) {
        if(obj["word"].match(/aj/i)) {
            console.log("matched");
            matchedIndexes.push(i);     
        }
    }
}

matchedIndexes will store the indexes where the object contains the word "aj" in a case-insensitive manner. The array variable represents your set of arrays.

Answer №3

Iterate over the json array :

let targetObject = null;
    Objects.forEach(function(object, index){
        if(object.word == 'aj'){
            return targetObject = object;
        }
    });
    console.log(targetObject);

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

Trouble with visibility of Angular controller variables on scope

I recently adjusted my code to align with John Papa's Angular style guide, but encountered an issue where my controller is no longer visible in ng-inspector. If I can successfully display vm.message, I believe I can resolve the remaining issues (thoug ...

What is the proper way to enable the Google Analytics cookie only once another consent cookie has been set and is marked as "true"?

I am currently using React/Nextjs on my website along with the react-cookie-consent library. This setup generates a pop-up where users can agree to the cookie policy, and if they do, a CookieConsent with the value "true" is set. In addition to this, I wis ...

What is the process for generating an HTTP response that results in a pipe error being thrown

In my NestJS application, I have created a custom pipe that validates if a given value is a valid URL. If the URL is invalid, an error is thrown. This pipe is utilized in a controller to save an item into the database. Recently, I discovered that the pipe ...

What's the best way to implement a conditional header in React?

I am looking to create a conditional display of the Header based on different pages. Specifically, I want the Header to be hidden on the Home page and shown on other pages like posts page, projects page, etc. I have been brainstorming possible solutions f ...

Mapping API for JavaScript, converting coordinates into precise locations

I have two coordinates: 54.674705589 and 25.289369548. I want to place these coordinates on a map when the button is clicked, similar to this example. However, the example provided is for addresses, not coordinates. Is it possible to modify this example t ...

The .each method is failing to iterate over the next object

Currently, I have been working with JSON data retrieved from the web. After successfully receiving the data, I proceed to create a JavaScript object to work with it. However, there seems to be an issue with retrieving the values of fname and lname from th ...

What is the best way to target an HTML attribute using jQuery?

I have customized a Textbox by adding a special attribute: <asp.TextBox MyCustomAttribute="SomeValue"><asp.TextBox> Now, I want to retrieve this value from within an AJAX success function. Please note that I have excluded irrelevant attribut ...

Modify the AJAX data in Datatables without directly modifying the elements

I am currently working with a Datatable that is being populated through AJAX, and everything is going smoothly. However, I am looking for a way to include some shortcuts to send requests to the server. The issue lies in how I can modify the data being sent ...

Issue encountered: org.json.JSONException - Index 2 exceeds the accepted range

Hello, I am currently dealing with a simple JSON error that says org.json.JSONException: Index 2 out of range. Can someone help me solve this issue? Here is the snippet of my JSON: [ { "cat_id": 593 "title": "Allergy and Immunology", "sub_cat" ...

Creating a Dynamic Canvas Rendered to Fit Image Dimensions

I'm struggling with a piece of code that creates an SVG and then displays it on a canvas. Here is the Jsbin Link for reference: https://jsbin.com/lehajubihu/1/edit?html,output <!DOCTYPE html> <html> <head> <meta charset=&qu ...

The date-fns parse function will retrieve the value from the previous day

When attempting to parse a date using the date-fns library, I am encountering an issue where the resulting date is one day prior. How can this be resolved in order to obtain the correct result? start = '2021-08-16' const parseStart = parse(start, ...

JavaScript Tutorial: Simplifying Character Ranges for Conversion

I am currently working on adapting code I found here to create a virtual keyboard for a foreign alphabet using an online textarea. Below is the modified code: <textarea id="txt"></textarea> <script src="https://ajax.googleapi ...

Eliminate certain elements from a JSON array

Data received in JSON format: { "id": 313, "status": "blocked", "audit_pixels": [ { "audit_pixel": "134307&ord={{random}}", "id": 1470355, ...

Calculating Time Difference in JavaScript Without Utilizing moment.js Library

I have two timestamps that I need to calculate the difference for. var startTimestamp = 1488021704531; var endTimestamp = 1488022516572; I am looking to find the time difference between these two timestamps in hours and minutes using JavaScript, without ...

Inject the JSON data fetched through AJAX into Datatables

I have been successfully using the datatables plugin to populate multiple tables with data. However, I realized that instead of making separate AJAX calls for each table, I could optimize by fetching the data once and storing it in a variable to be used by ...

JavaScript can sometimes present peculiar challenges when it comes to setting style attributes, especially when a DOCTYPE is

It seems like I am encountering an issue when trying to dynamically create and modify a <div> element using JavaScript. The problem arises when I use the XHTML 1 Transitional doctype. This is how I am generating the <div>: var newDiv = docume ...

Importing .js files from the static folder in Nuxt.js: a step-by-step guide

I'm in the process of transitioning my website, which is currently built using html/css/js, to Nuxt.js so that I can automatically update it from an API. To maintain the same website structure, I have broken down my code into components and imported ...

Numerous instances of Codemirror

I have the ability to generate and exhibit multiple dynamic codemirror instances, however, I am having trouble referencing them using the code snippet below. I suspect that the problem lies in creating a dynamic function name (not entirely sure how to ac ...

Which specific page initiates the post request?

Seeking help with my post request that originates from a specific page on my website: reqdata = 'text=' + mytext; $.ajax({ type: "POST", url: "/request.php", data: reqdata, cache: false, success: function(html) { alert(html) } ...

Change the anchor text dynamically with JQuery

The page contains href links with incomplete text. For example, the link text displayed on the page is "link1", but it should actually be "link1 - Module33". Both the page text and actual text start with the same initial text ("link1" in this case). I retr ...