限制javascript获取的获取结果
是否有类似于限制javascript获取的获取结果,javascript,Javascript,是否有类似于q=sort&或q=created:&的函数来限制JavaScript获取的结果数量 fetch('https://jsonplaceholder.typicode.com/posts') .then((res) => res.json()) .then((data) => { } 当然,最好的解决方案是如果https://jsonplaceholder.typicode.com/postsendpoint记录了一个可以发送的限制或筛选参数 假设结果是一个数组或
q=sort&
或q=created:&
的函数来限制JavaScript获取的结果数量
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => { }
当然,最好的解决方案是如果
https://jsonplaceholder.typicode.com/posts
endpoint记录了一个可以发送的限制或筛选参数
假设结果是一个数组或包含一个数组,第二个最佳解决方案是结果(应用标准)和/或结果(仅应用限制):
发件人:
不确定您到底想要什么,因此这里有3种可能性:
希望这能有所帮助。关于
.filter
?这不值得投反对票,不值得提出好的问题,也不值得对它进行好的描述,我也在寻找同样的东西
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => res.json())
.then((data) => {
data = data.filter(entry => entry.created > someValue) // Created after X
.slice(0, 1000); // Limit to 1000
// ...use data...
})
.catch(error => { // <=== Don't forget to handle errors
// Handle error...
});
fetch('https://jsonplaceholder.typicode.com/posts')
.then((res) => { // ***
if (!res.ok) { // ***
throw new Error("HTTP error " + res.status); // ***
} // ***
}) // ***
.then((res) => res.json())
.then((data) => {
data = data.filter(entry => entry.created > someValue)
.slice(0, 1000);
// ...use data...
})
.catch(error => {
// Handle error...
});
postData(`http://example.com/answer`, {answer: 42})
.then(data => console.log(JSON.stringify(data))) // JSON-string from `response.json()` call
.catch(error => console.error(error));
function postData(url = ``, data = {}) {
// Default options are marked with *
return fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, same-origin, *omit
headers: {
"Content-Type": "application/json; charset=utf-8",
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
.then(response => response.json()); // parses response to JSON
}