如何移除Fortnite API响应中的指定内容
I've written Node.js code to fetch BRStats data using the Fortnite API, and it successfully retrieves the response. Here's my working code:
const FortniteAPI = require("fortnite-api-com"); const config = { apikey: "REDACTED", language: "en", debug: true }; var Fortnite = new FortniteAPI(config); var name = "Ninja" Fortnite.BRStats({name: name, accountType: "epic", timeWindow:"lifetime", image: "none"}) .then(res => { console.log(res); }).catch(err => { console.log(err); });Now I want to remove specific strikethrough-marked sections from the response. How can I achieve this?
Great job getting the API call working! To remove specific unwanted sections from the response, you have a few straightforward options based on your needs:
Option 1: Destructure the response to exclude unwanted fields
If you know exactly which fields you want to remove, use object destructuring to pull out the unwanted fields and keep the rest in a clean object. For example, if you want to remove unwantedField1 and unwantedNestedSection:
Fortnite.BRStats({name: name, accountType: "epic", timeWindow:"lifetime", image: "none"}) .then(res => { // Explicitly exclude unwanted fields, keep the rest const { unwantedField1, stats: { unwantedNestedSection, ...cleanedStats }, ...cleanedResponse } = res; cleanedResponse.stats = cleanedStats; // Reattach the cleaned nested stats console.log(cleanedResponse); }).catch(err => { console.log(err); });
Option 2: Directly delete unwanted properties from the response
If you don't mind modifying the original response object, use the delete operator to strip out unwanted sections. This works for both top-level and nested fields:
Fortnite.BRStats({name: name, accountType: "epic", timeWindow:"lifetime", image: "none"}) .then(res => { // Delete top-level unwanted fields delete res.unwantedField1; // Delete nested unwanted fields delete res.stats.unwantedNestedSection; console.log(res); // Original object now has unwanted sections removed }).catch(err => { console.log(err); });
Option 3: Create a new object with only desired fields
If you have a short list of fields you want to keep, explicitly build a new object with just those properties. This is the safest way to avoid accidentally retaining unwanted data:
Fortnite.BRStats({name: name, accountType: "epic", timeWindow:"lifetime", image: "none"}) .then(res => { const cleanedResponse = { username: res.username, platformInfo: res.platformInfo, lifetimeStats: res.lifetimeStats.filter(stat => !stat.isStrikethrough) // Filter out strikethrough stats if applicable // Add other specific fields you need here }; console.log(cleanedResponse); }).catch(err => { console.log(err); });
Just replace the placeholder field names in the examples with the actual keys of the sections you want to remove. If you're unsure about the response structure, log the full res object first to identify exactly which keys correspond to the strikethrough sections.
内容的提问来源于stack exchange,提问作者Lova

