Retrieve the string value from a JSON object beginning at a designated position

Trying to figure out how to extract a specific value from a JSON object. Here's the JSON example:

{
 "posts":{
   "fields":{
     "date":"2015/01/24",
     "date_end":"2015/01/25",
     "time":"23:00 - 05:00"
   }
 }
}

I need to store the 05:00 part of the time key into a variable. The challenge is that sometimes the time could only have 23:00 without the - 05:00 part. How can I achieve this task?

Answer №1

Give this a shot

let currentTime = data.fields.time[0].split(' - ');
currentTime = currentTime[1] || currentTime[0] || '';

Demo

Answer №2

extractedTime = processTime(fields['time']);
if (extractedTime.length>1)
     return extractedTime[1];
else
     return 'another value';

Answer №3

Here's a solution that can help accomplish the task:

Extracting the last element from 'xxx.time[0]' array:
xxx.time[0].split(" ")[xxx.time[0].split(" ").length - 1];

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

How do I determine which radio button is currently selected within my Angular2 application?

In my HTML, I have the following radio buttons: <div class="col-lg-4" id="radioButtons"> <form action=""> <fieldset id="capacity"> <legend>capacity</legend> <label for="input-ao">< ...

Utilize Json parsing to extract and display precise data

This is a snippet of JSON code and I am attempting to execute it: Sub test() Dim req As New MSXML2.XMLHTTP60 Dim URL As String, ws As Worksheet Dim json As Object, r, r1 As String URL = "https://www.nseindia.com/api/quote-equity?symbol=DIVISLAB" ...

Angular JS Profile Grid: Discover the power of Angular JS

I came across an interesting example that caught my attention: http://www.bootply.com/fzLrwL73pd This particular example utilizes the randomUser API to generate random user data and images. I would like to create something similar, but with manually ente ...

Steps to create a TypeScript function that mimics a JavaScript function

As I look at this javascript code: // find the user User.findOne({ name: req.body.name }, function(err, user) { if (err) throw err; if (!user) { res.json({ success: false, message: 'Authentication failed. User not found.' ...

Combine millions of JSON files into one highly efficient consolidated document

I have a vast collection of JSON files that need to be combined into a single JSON file as a list of JSON dictionaries: Each JSON file contains the following dictionary structure: { "finent": "abc", "findeplabel": "lbl", } They should be merged into ...

Converting an array of 8-bit unsigned integers into a string without the

Currently, I am working on extracting JSON data from an HTML document stored in a Uint8 array. The process involves converting the array to text and then isolating the JSON within a script tag. To achieve this, I initially converted the array into text fo ...

Trouble with Fullcalendar v4: Unable to use addEvent() function in a customized view

Currently, I am utilizing fullcalendar v4 (accessible at https://fullcalendar.io/) and facing an issue where I need to refresh the dropped event in a custom view called 'timeGridSixMonth'. var calendar = new FullCalendar.Calendar(calendarEl, { ...

Is the data set in stone, or is it just the API data that can

Recently, I embarked on the journey of creating a blog using nextjs, MongoDB, and express. Taking advantage of getStaticProps and getStaticPaths, I was able to dynamically generate pages for each blog post and display them individually. However, I encoun ...

How can I make HTML elements smaller to properly fit within a fixed-size div container?

Could anyone provide guidance on how to display HTML elements such as h1, h2, p, block quotes, and lists inside a fixed width and height <div id="preview"></div>? I want the content to be shrunk down so it fits perfectly scaled and proportion ...

Implementing role-based authentication in Next.js using Next-auth and Firebase

Currently, I'm in the process of integrating role-based authentication using NextAuth.js into my Next.js application. Despite following the provided documentation meticulously, an error (in profile snippet and callback snippet which I copied from next ...

The problem arises when the KendoUI update request fails to utilize JSON format, even when explicitly setting the dataType property to

I have searched extensively through the KendoUI documentation regarding the scheduler widget, and have also sought help on the "premium" forums without success. My current objective is to send meeting data to my NodeJS API. In accordance with the document ...

Notify immediately if there is any clicking activity detected within a designated div container

I am looking to trigger an alert when a specific div is clicked. Here is the scenario: <div class="container"> <div class="header"> <h1>Headline<h1> </div> <div class="productbox"></div> </div> I have succ ...

The function Map() in React-Leaflet cannot be executed, despite the presence of data

I am attempting to replicate the React-Leaflet example of a list of markers. I have an array of objects that I am passing to MarkerList to be transformed into Fragments and displayed on the map. However, my mapping function is not functioning as expected ...

Issue encountered while attempting to send an HTML file using express.js

I have been trying to display an HTML file using Express.js with a static __dirname, but the HTML content does not show up after calling localhost/ var express = require('express'); var web = express(); ...

Using jQuery to restrict the number of checked checkboxes based on a selected value - Here's how!

How can I restrict the number of checkboxes that can be checked based on the selected option from a dropdown menu? For example, selecting 'option1' should allow only 1 checkbox to be checked, 'option2' should allow 2 checkboxes, and so ...

Issue encountered in React: Unable to access object value within ComponentDidUpdate method

I'm struggling to retrieve the value from an object key. componentDidUpdate(prevProps) { if (prevProps !== this.props) { console.log("component did update in top menu", this.props.topmenudata[0]) this.setState({ ...

Accessing a specific value based on a selected option in Angular 2+

Examining the Angular 2+ code snippet below, there is an Array defined in TypeScript: models = [{idModele: 1, model: "Bianca", price: 500}, {idModele: 2, model: "etc", price: 600} ] The query here is about setting up a mechanism where selecting a specifi ...

React SlideMenu will not close when a link is clicked

I am facing an issue with my off-canvas menu, which slides in when the variable isOpen is set to true. However, the problem arises when trying to slide it back out upon clicking a Link to navigate to another page. Instead of sliding out, the mobile menu oc ...

Including numerous pins on a map

I am currently working on a project that involves extracting data from PHP records to plot markers on a leaflet map using coordinates. Unfortunately, I encountered an error in the console: Error: Invalid LatLng object: (18.473396, undefined) Here is th ...

Enhance the functionality of JavaScript for multiple images using ASP.NET

I have been searching extensively for a way to achieve the following necessary function. Initially, I successfully replicated this function in an asp.net application using VB for a single image. However, I am now attempting to apply it to multiple images. ...