Home > Software design >  Javascript array object return empty array [closed]
Javascript array object return empty array [closed]

Time:09-22

I try to filter jsonplaceholder comments to get by id but return empty array

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);
});

CodePudding user response:

Change postID by postId:

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

CodePudding user response:

You're comparing postID instead of postId.

Working example at 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);
  });

CodePudding user response:

Log both the result data and the post id property type. So it could either be an absence of data from the response in the request or it could be a flawed comparison because of the "===" operandi

  • Related