Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

javascript - get new data from api

Hello I'm trying to get new data from API (filtering data with JSON file) I want to filter data from API and filter with JSON file and get what new I tried

    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)

I also tried console.log data3 (data from JSON file) and got: result for data3 (console.log(data3) )

Also i did console.log for data1 (data from api) and got: result for data1 (console.log(data1) )

[ if I do:

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

I will get nothing [] ]

Please if you know how to make [ console.log(jsnf == data1) \true

OR

[ console.log(data1.filter((x) => jsnf.includes(x))) ] \x = new items from api ] please let me know how, thanks!

question from:https://stackoverflow.com/questions/65641347/get-new-data-from-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you are reading file data.json from your local device and doing JSON.stringify on the returned file.

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

Know that it is a string now and if you without using JSON.parse(jsnf) start using your filter logic.

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

Know that this will not work as expected.

Also until and unless we know what data is in jsnf we cannot we sure about your logic to filter things.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...