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

Flawless Carousel - Flipping the Sequence

I am currently implementing Slick Carousel on a website that I am working on. One challenge I am encountering is trying to get the "slider-nav" to move in the opposite direction than it normally does. For instance, at the moment, the order goes like this ...

Does the syntax for the $.ajax() function appear to be incorrect in this instance?

I am attempting to use .ajax to retrieve the source HTML of a specific URL (in this case, www.wikipedia.org) and insert it into the body of a document. However, the code provided below is not producing the expected outcome. <!DOCTYPE html> < ...

Utilizing jQuery to access data retrieved from ajaxStop

Seeking help on retrieving the json data returned from ajaxStop... I have tried accessing the data property within the event object, but it shows as undefined even though the data is being returned correctly... Cheers, Anthony UPDATE: Upon using Succes ...

Resetting several sticky titles after displaying and hiding elements

Inspired by a popular codepen example, I have successfully implemented sticky titles in my sidebar. However, when integrating these sticky titles with the functionality to show/hide related items on click, I encountered some unexpected issues. The code sni ...

The dynamic Vue.js transitions and effects are like a magic

I am using a v-for to render multiple child components: <div v-for="(pass) in scoringPass" :key="pass.decision"> <Pass :pass="pass"/> </div> Each of these child components contains a transition tag: &l ...

Troubleshooting a problem with dynamic options in Materialize select set

I'm currently facing an issue with my two dependent dropdowns, country and state. When I select a country, I use JavaScript to populate the respective states in the state dropdown. However, even though the options are added correctly when I inspect th ...

Is it true that Safari restricts AJAX Requests following a form submission?

I've developed a JavaScript-based upload progress meter that utilizes the standard multipart submit method instead of submitting files in an iframe. The process involves sending AJAX requests during submission to retrieve the percentage complete of th ...

Tips for achieving a gradual transformation of an element according to the scrolling position

I have been experimenting with using waypoints for two specific purposes. The first objective is to determine whether a user is scrolling up or down and if the container comes into view. However, this functionality is not working as expected. The sec ...

receiving an undefined value from a remote JSON object in Node.js

I have been struggling to extract the specific contents of a JSON api without success. Despite my best efforts, the console always returns undefined. My goal is to retrieve and display only the Question object from the JSON data. After spending several h ...

Retrieving information from a JSON string can be done with or without using the JSONObject

I have a Java String containing the following JSON code: String str = {"posts":[{"key":"key1","value":"x"},{"key":"key2","value":"0"},{"key":"key3","value":"y"}]} Is there a way to extract the values from the string using JSONObject, or should I stick wi ...

Looking for solutions to manage mouseenter, mouseleave events and ensuring content dropdowns stay visible in Vue.js 2?

Hey there, I'm trying to figure out how to keep dropdown content from disappearing when using @mouseenter and @mouseleave. Here's what I have so far: <div class="wrapper"> <div class="link" @mouseenter="show = ...

Achieve top-notch performance with an integrated iFrame feature in Angular

I am trying to find a method to determine if my iframe is causing a bottleneck and switch to a different source if necessary. Is it possible to achieve this using the Performance API? This is what I currently have in my (Angular) Frontend: <app-player ...

Tips for launching a colorbox within a specific div container

I am looking to implement a color box that opens without a popup and only shows/hides inside a specific div. Is it possible to target a div to open the colorbox content? <a href='#myGallery' class='group'>click here</a> &l ...

Navigate within a div using arrow keys to reposition another div

As a newcomer to JavaScript, I am facing some challenges. My goal is to use arrow keys to move a small div inside a larger div. However, the code below is not functioning as expected. Here is the HTML and CSS: <div id="rectangle"> <div id="s ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

getting information from a JSON array using AngularJS

Looking to extract all images from the imagePath array in the following JSON. While retrieving a single image works fine, encountering difficulties when trying to fetch the entire imagePath array. Any help with this issue would be greatly appreciated. Than ...

Bootstrap4 does not support the <button> element

How can I achieve a 'collapse icon' floated to the left followed by Copyright © using Bootstrap 4? I've looked at similar questions on this topic, but none seem to provide a direct answer. Other questions contain different code with ob ...

Ways to halt a script entirely once its tag has been eliminated

I have a script from a page tracker website that runs periodically. Occasionally I need to restart the script from the beginning. To do this, I typically remove the script tag from the DOM and then re-append it. However, because the script utilizes setInt ...

Ways to retrieve a value within a function and update a variable

Fetching data from the firebase database = firebase.database(); var ref = database.ref('urls'); ref.on('value', gotData, errData); function errData(err){ console.log('Error!'); console.log(err); } function gotData(d ...

Steps for implementing a local JSON file in material-table display

I'm new to React and material-table, and I'm looking to display data from a JSON file containing around 1000 objects in a table format. { "a": "1", "b": "2", "c": "3" } While I was able to achieve this with the current code, I'v ...