retrieve fresh information from the API

Hello! I am attempting to retrieve new data from an API by filtering it with a JSON file. My goal is to filter the data from the API using the JSON file and extract any new information.

    const jsnf = JSON.stringify(fs.readFileSync("./data.json", "utf8"));
    const res = await axios.get("https://fortnite-api.com/v2/cosmetics/br/new")
    const data1 = res.data.data.items;
    const data2 = JSON.stringify(data1)
    const data3 = JSON.stringify(Object.values(data1), null, 2)

In addition, I attempted to log data3 (data from the JSON file) and received: result for data3 (console.log(data3) )

I also logged data1 (data from the API) and got: result for data1 (console.log(data1) )

[ If I do:

console.log(data1.filter((x) => jsnf.includes(x)))

I will get an empty array [] ]

If you know how to achieve [ console.log(jsnf == data1) \\ true

OR

[ console.log(data1.filter((x) => jsnf.includes(x))) ] \\ x = new items from the API ] please share your insights, thank you!

Answer №1

When you open and read the content of file data.json on your local device, don't forget to convert it into a JSON string using JSON.stringify.

 const jsonFile = JSON.stringify(fs.readFileSync("./data.json", "utf8"));

It's important to remember that the data is now in a string format. If you skip the step of using JSON.parse(jsnf) before applying your filter logic, you may not get the desired results.

console.log(data1.filter((item) => jsonFile.includes(item)));

Keep in mind that understanding the content of jsonFile is crucial for determining the effectiveness of your filtering logic.

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

Trying to understand the strange behavior of HTML parsing with jQuery in Javascript and Firefox

I have been working on a script to parse an HTML page using jQuery. The script runs smoothly in Chrome, IE, and Safari, but I'm facing some unexpected behavior while testing it in Firefox (version 36.0.1). Here's the code snippet: $.ajax({ u ...

How to Change JSON Key Names in Java Using org.json.JSONObject

I have been working on renaming key names within a JSON object. While I've had success renaming keys at the top level, I am facing issues when trying to rename keys nested inside another object or within a List Object. Here is my current approach: ...

Choosing the number of items for each cartItem in KnockoutJS: A comprehensive guide

Greetings! I am new to using knockout and I am attempting to automatically select the quantity for each item in my cart from a dropdown menu. Below is the code I have written: VIEW <div data-bind="foreach: cartItems"> <h3 data-bind="text: ful ...

When utilizing CKEDITOR, the default TEXTAREA is obscured, and CKEDITOR does not appear

I am trying to incorporate CKEDITOR into my project. I have added the ckeditor script in the footer and replaced all instances of it. <script src="<?= site_url('theme/black/assets/plugins/ckeditor/ckeditor.min.js') ?>" type="text/javasc ...

Exploring Vue.js: Navigating Through an Array of Images Nested Within Another Array

I am looking to showcase images stored in an array called "image" within another array named product. Essentially, if a product has an array of 3 images, I want to display all 3 images, and so on. Here is the code snippet: <template> <div c ...

Easily toggle between various image source paths

In my current application, all HTML files have hardcoded relative image paths that point to a specific directory. For example: <img src="projectA/style/images/Preferences.png"/> Now, I am considering switching between two different project modes: & ...

Retrieve information about a parent's data related to child events in Vue.js

I'm working with two components nested inside each other. The parent component has a click event that needs to modify the data value of the child component. <template> <div> ..... ..... <my-component :op ...

How to extract information from JSON arrays in C# without using object property names

When working with an API that provides order data as an array of strings, the goal is to parse it using Json.Net into a specific object format. The sample data looks like this: "orders": [ //[ price, size, order_id ] [ "295.96","0.05088265","3b0f1 ...

Ensure each list item is directly aligned when swiping on a mobile device

Looking to make a simple horizontal list swipeable on mobile devices with tabs that snap from tab to tab, activating the content for the active tab immediately. The desired effect is to swipe directly from one tab to the next without having to click separ ...

Obtain the registration ID for Android to enable push notifications by utilizing PushSharp

I am currently utilizing the PushSharp library and have come across deviceToken in the sample code provided on this link. Could someone kindly assist me on how to obtain this deviceToken? The PushSharp sample code does not clearly explain this. apnsBrok ...

Selecting the parent span element using jQuery

Is there a better way to display text in the parent <div>'s <span>? I'm having trouble with using spans and .parent(), any advice would be appreciated. HTML: <div> <span></span> <!--the span where I need to show ...

Tips for stopping links in iOS web applications from launching in a new Safari tab

I am in the process of developing an HTML application specifically for iPads. To enhance user experience, I have added the web app to the homescreen using the "favorite" option. However, I encountered an issue where every internal URL opens in a new Safari ...

"Enhance Your Website with Bootstrap Modal and Jazz up Your

I am encountering an issue with modal-bootatrap and bootstrap-datepicker. Within the datatable, there is a button provided for editing. Upon clicking the edit button, a modal-bootstrap will be displayed. The modal-bootstrap contains a form for editing d ...

Is AJAX HTML element swapping a breeze with Rails controllers?

It would be great to have an ERB partial set up like this: <ul id='things-index'> <% @things.each do |t| %> <li><%= t.name %></li> <% end %> </ul> And have the ability to update it in the contro ...

Is it feasible to include a variable interpolation in the package.json file for a Node Package?

I have a static website where I compile Sass using the node-sass library. Currently, I am using Grunt to watch the files, but I feel like it's unnecessary because I could just use their built-in command line interface (CLI). So, I decided to add thi ...

Using JQuery's $.ajax() method to send a post request and pass data to invoke a specific method in

I'm currently in the process of learning JQuery and encountering some challenges with utilizing the $.ajax() function. My goal is to send data to a controller for processing. During my debugging of the JQuery, it seems that the ajax call isn't b ...

Function execution in React component is not happening

Trying to master React and next.js with Firebase as the database has been an interesting journey. I recently encountered an issue where a function in my component is not being called. Upon trying to debug using console.logs(), it appears that the function ...

AddRegions does not function as expected

Basic code to initialize an App define(['marionette'],function (Marionette) { var MyApp = new Backbone.Marionette.Application(); MyApp.addInitializer(function(options) { // Add initialization logic here ...

Looking to alter the CSS of an ID element when hovering over a link on your website?

Irrespective of the positioning of the links in the html, a simple hover effect can trigger changes like switching images or altering backgrounds anywhere on the website. The ideal solution would involve a straightforward method without the need for Javas ...

Switching from map view to satellite view on Google Maps allows you to see detailed aerial

Is there a way to switch from map view to satellite view on a Google Map using JavaScript after zooming in 100%? If so, how can it be achieved within the following JavaScript code? DEMO:http://jsfiddle.net/keL4L2h0/ // Load Google Map ///////////////// ...