Javascript数组对象返回空数组



我尝试过滤jsonplaceholder注释以获得id,但返回空数组

axios.get('https://jsonplaceholder.typicode.com/comments').then((result) => {
// const arr = _.find(result.data, { postID: 1});
// console.log(arr);
console.log(result.data.filter(x => x.postID === 1))
}).catch((err) => {
console.log(err);
});

ChangepostIDbypostId:

console.log(result.data.filter(x => x.postId === 1))

记录结果数据和post id属性类型。这可能是请求中响应的数据缺失也可能是有缺陷的比较因为"==="的

您比较的是postID而不是postId

Codesandbox的工作示例

axios
.get("https://jsonplaceholder.typicode.com/comments")
.then((result) => {
// const arr = _.find(result.data, { postID: 1});
// console.log(arr);
console.log(result.data.filter((x) => x.postId === 1));
})
.catch((err) => {
console.log(err);
});

最新更新