Search through a JSON array to find a specific element and retrieve the entire array linked to that element

Recently, I've been working with a json array that dynamically increases based on user input. Here's a snippet of the json code I'm dealing with:

[{"scheduleid":"randomid","datestart":"2020-06-30","dateend":"2020-06-30","timestart":"08:00","timeend":"20:00","recurrences":"dailysett","daily":""},
{"scheduleid":"randomid2","datestart":"2020-06-30","dateend":"2020-06-30","timestart":"08:00","timeend":"20:00","recurrences":"dailysett","daily":""}]

This json array is stored in var Schedulearray. If I need to search for a specific id, such as retrieving randomid from the array, I can use the following line of code:

Schedulearray.scheduleid;

If the result is randomid, I'd like to retrieve all attributes associated with that element, like timestart, timeend, and so on. Is it possible to do this in one go, or do I have to fetch each attribute individually like demonstrated below?

var timestart=Schedulearray.timestart;

Answer №1

To locate a specific item, you can utilize the find method:

let data = [{"id":"abc123","name":"John Doe","age":30},
{"id":"def456","name":"Jane Smith","age":25}]
var result = data.find((item) => item.id === "abc123");
console.log(result);

Answer №2

To narrow down your data, consider utilizing the filter method.

var items = [{"id":"abc123","name":"John"},
{"id":"def456","name":"Jane"}];

var filteredItems = items.filter(item => item.id === "abc123");

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

Error: Attempting to access 'input_date' property of null object resulted in an uncaught type error

I am attempting to implement pagination for a data table using AJAX to refresh the table without having to reload the entire page. However, I am encountering an issue where the input_date is being considered null even though it should not be. Below is the ...

Generate a random weighted value from an array using PHP

I'm working with this script: $domain = ['gmail.com', 'yahoo.com', 'hotmail.com']; $domain = $domain[mt_rand(0, count($domain) - 1)]; Is there a way to assign different percentages to each item for the chances of being ...

I am uncertain about whether it would be better to utilize URL path parameters or query parameters for filtering on the back-end of the application

Suppose I am trying to retrieve all car models from a specific brand. This can be achieved by utilizing URL path parameters or query parameters as shown below: router.get('/modelsByBrand', modelController.getModelsByBrand); To make the GET reque ...

Retrieving the data payload from a 400 response using HttpURLConnection

When making a JSON request to my web service, I return a 400 message code with a detailed JSON error response if the request is bad. But how can I retrieve this payload on the client side using HttpURLConnection? The connection's InputStream is null a ...

Node.js & Express: Bizarre file routes

It's quite strange how my local paths are functioning. Let me show you an example of my directory structure: public > css > bootstrap.css public > js > bootstrap.js templates > layout > page.ejs (default template for any page) tem ...

Why would one utilize window.location?.search?.split?

Could someone explain the purpose of using window.location?.search?.split('=')[1] and why the value of id is set to window.location?.search?.split('=')[1]? Code: function EndScreen() { const [score, setScore] = React.useContext(Score ...

Best practices for utilizing the getTile() method within Google Maps

I have a question about storing markers in a database using tile IDs. The goal is to display all the markers associated with a specific tile when it is displayed on a map. Initially, I created a code that was not working correctly. It only requested one ...

An efficient method for iterating through RestClient requests by providing an identifier and concatenating it to the response

I am facing limitations with the API I'm using to retrieve data. It only allows me to make GET requests by passing the record identifier. The challenge is that I may have multiple IDs for which I need data. Is there an efficient way to loop through th ...

Optimizing Angular for requireJS deletion

We recently developed an Angular directive that utilizes blueimp-fileupload. Everything seems to be working fine until we decided to optimize our code using requireJs. After running the optimizer, we encountered the following error: Error: cannot call m ...

Using Laravel and vue.js to convert values into an array

I am currently utilizing vue.js within my Laravel project. Within the project, I have three tables: Article Location article_location (pivot table) I am looking to convert location articles into JSON format so that I can pass it to vue. How should I ...

Encountering issues with rendering in React JS when utilizing state variables

I've been attempting to display content using the render method in React JS, but for some reason, the onClick code isn't executing. I'm currently enrolled in a course on Udemy that covers this topic. import React, { Component } from 'r ...

What is the best way to incorporate a progress bar animation into my notification?

Seeking assistance to implement an animated progress bar that changes colors gradually over time based on a variable [timer]. Can anyone lend a hand with this? Thank you! https://i.sstatic.net/lhgeF.png $(document).ready(function(){ window.addEvent ...

Converting information from a model into individual variables

I'm a newcomer to typescript and angular, and I've been attempting to retrieve data from firebase using angularfire2. I want to assign this data to variables for use in other functions later on. I am accustomed to accessing object members using d ...

Guide to configuring Ionic Auto Height Sheet modal in Vue 3

Trying to implement an Ionic Auto Height Sheet modal in a Vue 3 project (https://ionicframework.com/docs/api/modal#auto-height-sheet). Below is the code I have written. In ion-tab-button #3, I included id="open-modal". Underneath the ion-tab-but ...

Tips for changing the content of a td element to an input field and removing the displayed value

I am facing an issue with a dynamic table that displays names and input fields. When a name is displayed in a table row, the user has the option to delete that name. I am able to remove the value from a specific table row, but I am struggling to replace th ...

Extraction of JSON information from an API and converting it into a Pandas dataframe

Looking to retrieve data from an API () and import it into pandas. The API provides data in JSON format. df = pd.read_json('new.json' , orient = 'columns') Error: Mixing dicts with non-Series may lead to ambiguous ordering. Data need ...

Decoding a nested array of objects in JSON using Go

As I understand it, you have the ability to decode arbitrary JSON into a map[string]interface{} value, but for my case where the JSON response is consistently structured and defined, I prefer to decode it into nested structs for simplicity. Here's a ...

utilizing javascript once form elements are dynamically inserted

While dynamically constructing form elements, I encountered an issue with generating unique IDs when the form is submitted. Everything works fine except for the JavaScript function responsible for populating the year in a dropdown selection. The issue ari ...

Exploring the use of asynchronous data retrieval with jQuery and JSON within MVC 2.0

Attempting to retrieve server-side data using jQuery's getJSON method has hit a snag. The URL specified in the getJSON call is being reached, but the expected result is not being returned to the browser upon postback. There are suspicions that the iss ...

Retrieving JSON Data from Django Server

UPDATE: I am currently working on a project that involves the following steps: Submit a POST request with a file in the template --> Process the file in the view to generate an ID --> Display the ID in the template I am relatively new to Django and ...