Understanding the application of JSON data can be simplified by following these

I am looking to manipulate dates by either adding or subtracting them, but I am unsure of how to extract the dates from the other data present.

var fetch = require('node-fetch');

fetch('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY')
  .then(response => response.json())
  .then(data => console.log(data));

This is the current output I am receiving:

{
   count:56,
   results:[
      {
         date:'2014-02-04T03:30:01',
         id:'LC8_L1T_TOA/LC81270592014035LGN00'
      },
      {
         date:'2014-02-20T03:29:47',
         id:'LC8_L1T_TOA/LC81270592014051LGN00'
      },
      {
         date:'2014-03-08T03:29:33',
         id:'LC8_L1T_TOA/LC81270592014067LGN00'
      },
      {
         date:'2014-03-24T03:29:20',
         id:'LC8_L1T_TOA/LC81270592014083LGN00'
      }
    ]
}

Answer №1

You can give this a shot!

const fetch = require('node-fetch');

fetch('https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY')
  .then(response => response.json())
  .then(data => {
    const results = data.results;
    for(let i = 0; i < results.length; i++){
      console.log(results[i].date);
    }
  })
  .catch(error => console.error('Error:', error));

Answer №2

JavaScript :

var dateList = new Array();
var jsonData = {
   totalEntries:56,
   entries:[
      {
         entryDate:'2014-02-04T03:30:01',
         entryID:'LC8_L1T_TOA/LC81270592014035LGN00'
      },
      {
         entryDate:'2014-02-20T03:29:47',
         entryID:'LC8_L1T_TOA/LC81270592014051LGN00'
      },
      {
         entryDate:'2014-03-08T03:29:33',
         entryID:'LC8_L1T_TOA/LC81270592014067LGN00'
      },
      {
         entryDate:'2014-03-24T03:29:20',
         entryID:'LC8_L1T_TOA/LC81270592014083LGN00'
      }
    ]
};
var temporaryData = jsonData.entries;
for(var index= 0; index<temporaryData.length;index++){
    dateList.push(temporaryData[index].entryDate);
}
console.log(dateList);

Check out the sample jsfiddle code snippet: http://jsfiddle.net/43hbB/558/

Answer №3

let fetchedResults = data.results;
let dateArray = [];                 //store dates
for(let j = 0; j < fetchedResults.length; j++){
    console.log(fetchedResults[j].date) //output each date
    dateArray.push(fetchedResults[j].date);
}
console.log(dateArray);            //display dateArray

Answer №4

Utilize the array map method to extract the date from the provided data.

var data = obj.results;
var newDates = data.map(function(element) {
return element.date;
})
console.log(newDates);

View working example here : https://jsfiddle.net/eey2s68L/

Answer №5

When performing addition or subtraction on dates, it is necessary to first convert the date received as a string into a Date object.

var allDates = [];
var request = require('request');
request('https://api.example.com/data?lon=100.75&lat=1.5&date=2022-10-15&api_key=API_KEY', function (error, response, body) {
  var data = JSON.parse(body);

  var results = data.results;
  for(var i=0; i<results.length; i++) {
   allDates.push(new Date(results[i].date)); // Storing dates as Date objects in an array
   // Perform your operations here using (new Date(results[i].date))
  }
});

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

The state update is triggering a soft refresh of the page within Next.js

In my Next.js project, I have integrated a modal component using Radix UI that includes two-way bound inputs with state management. The issue arises when any state is updated, triggering a page-wide re-render and refreshing all states. Here is a snippet of ...

Struggling to extract text from within a <p> tag on an Ionic 1 app page on iOS

After developing an ionic-1 application for iOS and Android devices, I encountered an issue with displaying mobile numbers in one of the views. The numbers are contained within <p> tags. While users can click and hold on a number to copy it on Andr ...

"Patience is key when waiting for an AJAX response within a jQuery loop

I've been facing difficulties in making this work using the $.Deferred object. Here is a simplified version of the code setup. g_plans = []; $(function(){ // Need to utilize a custom ajax function that returns a promise object var pageLoadPro ...

Steps for adding an array of JSON objects into a single JSON object

I have a JSON array that looks like this: var finalResponse2 = [ {Transaction Amount: {type: "number"}}, {UTR number: {type: "string"}} ] My goal is to convert it into the following format: responses : [ { Transaction Amount: {type: "number"}, UTR numbe ...

Solving required packages in Express server

I am encountering difficulties with resolving dependencies on my express server. Below is the structure of my project: Calculator --dist ----app -------calculator.js -------server.js --node_modules --src ----app --------calculator.js --------server.js -- ...

Managing a database update when server actions involve various form types

My UI dashboard contains multiple forms like "edit title" and "edit description", each with just one input element. I am looking to streamline database updates and server actions in next js 14, utilizing useFormState on the front end. While I have achieve ...

The Everyday Explanation of Same Origin Policy

Could anyone simplify the concept of the Same Origin Policy for me? I've come across various explanations but I'm in search of one that a child can easily understand. I found this link to be quite helpful. Is there anyone who can provide further ...

What is the procedure to incorporate login credentials into the source of an iframe?

Is there a way to pass user auto login in the src URL? <iframe src="https://secure.aws.XXX.com/app/share/28228b0ccf0a987" width="1060px" height="1100px"></iframe> I attempted to achieve this, but it still shows the login screen <ifr ...

Exploring elements in Javascript

I am attempting to retrieve values from various elements when a 'a' element is clicked. Consider the following code: <tr data-id="20"> <div class="row"> <td> <div class="btn-group"> <a data-checked= ...

Choosing bookmarkable views in Angular 5 without using routes

I'm currently working on a unique Angular 5 application that deviates from the standard use of routes. Instead, we have our own custom menu structure for selecting views. However, we still want to be able to provide bookmarkable URLs that open specifi ...

What is the best way to show static files from the backend in a React application?

Currently, my setup involves a React application connected to an Express-Node.js backend through a proxy. Within the backend, there are some static files in a specific directory. When I make requests and embed the response link in the "src" attribute of an ...

Discover the most frequent value in an array by utilizing JavaScript

My array contains repeating values: [0, 1, 6, 0, 1, 0] How can I efficiently determine the highest frequency of a specific value being repeated? For example, in this array, I would like the script to return 3 since the number 0 repeats most frequently a ...

Node accurately handles and displays errors, such as validation errors, in a precise manner

I am currently restructuring our code base to incorporate promises. Below are two blocks of sample code: user.service.js export function updateUserProfileByUsername(req, res) { userController.getUserByUsername(req.params.username) .then((userProfile ...

Having trouble getting the Underscore.js template to function correctly with JSON data

Hello there! I have some PHP code $arr = array("title"=>"sample Title", "body"=>"151200"); echo json_encode($arr); The data output is: {"title":"test Title","body":"151200"} When I attempt to use this JSON output in Underscore, I encounte ...

What is the process for transferring information from a Microsoft Teams personal tab to a Microsoft Teams bot?

Is it feasible to share data such as strings or JSON objects from custom tab browsers to a Teams bot's conversation without utilizing the Graph API by leveraging any SDK functionality? ...

Issue with Vue canvas $ref not being defined on resize causing error, but oddly still seems to function correctly

Within my application, there exists a tabbed interface. One of the tabs contains a canvas element that dynamically adjusts its size to fit the window whenever it is resized. The functionality works seamlessly at first, but upon switching tabs and returning ...

Guide to building a seamless page without refreshes with the help of jquery, express, and handlebars

I need assistance with my express JS project. I am trying to set up two pages using NodeJS, incorporating handlebars as the template engine. My objective is to have the first page rendered using res.render('home'), and for the second page to be l ...

Using Node.js and the Azure DevOps Node API, you can easily retrieve attachments from Azure DevOps work items

Encountering a problem while attempting to download an attachment for a work item in Azure DevOps. Utilizing the node.js 'azure-devops-node-api' client (https://www.npmjs.com/package/azure-devops-node-api) to communicate with ADO API. Retrieving ...

Is it possible to implement BreezeJS without relying on EF?

Earlier, the process involved using Entity Framework with Breeze to connect directly to DbContext, but the object did not exist elsewhere. Some have manually created Metadata (such as through T4). I have access to the SQL server where each Table has its ...

Best method to incorporate JSON array from WebService into a Dataframe

I have a column titled 'code' that I need to send to a web service in order to update two specific fields (dfMRD1['Cache_Ticker'] and dfMRD1['Cache_Product']) with data retrieved from the JSON response, specifically the values ...