最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object entries returning undefined - Stack Overflow

programmeradmin2浏览0评论

I have a json object returned from a SQL query. I want to filter out the json key before sending back to the front end. If the key is true, return back to the front end is what I am looking for.

In my server file I have this line.

          let returned_data = Object.entries(queried_data[0]).forEach((key, value) => {
            return value === true ? key : null
          })
          res.json(returned_data)

This is an example of my returned json after SQL querying.

[{first_name: 'testing', has_apple: true, has_pear: true, has_beans: false}]

I am expecting the returned_data to have ['has_apple', 'has_pear']. Right now I am getting undefined for returned_data

I have a json object returned from a SQL query. I want to filter out the json key before sending back to the front end. If the key is true, return back to the front end is what I am looking for.

In my server file I have this line.

          let returned_data = Object.entries(queried_data[0]).forEach((key, value) => {
            return value === true ? key : null
          })
          res.json(returned_data)

This is an example of my returned json after SQL querying.

[{first_name: 'testing', has_apple: true, has_pear: true, has_beans: false}]

I am expecting the returned_data to have ['has_apple', 'has_pear']. Right now I am getting undefined for returned_data

Share Improve this question asked Feb 21, 2019 at 19:37 calvertcalvert 72113 silver badges35 bronze badges 5
  • Object.entries() never returns undefined. forEach always does. – Bergi Commented Feb 21, 2019 at 19:38
  • Are you positive you aren't required to perform a JSON operation on queried_data prior to interacting with it with Object? – theaccordance Commented Feb 21, 2019 at 19:39
  • @theaccordance You are right, I can see something is not right from the console.log, after changing from forEach to map I am getting a messed up output. I need to see which JSON operation I will need to do. – calvert Commented Feb 21, 2019 at 19:44
  • Try JSON.parse() – theaccordance Commented Feb 21, 2019 at 21:36
  • I got it working already. Thank you – calvert Commented Feb 21, 2019 at 21:37
Add a ment  | 

3 Answers 3

Reset to default 4

forEach doesn't return anything map does. Also Object.entries returns an array of arrays and hence you need to destructure the value in map function to get key and value. Change your code to

      let returned_data = Object.entries(queried_data[0]).map(([key, value]) => {
        return value === true ? key : null
      })
      res.json(returned_data)

As other answers say forEach() doesn’t return anything so either you have to use map() or create an array and push value to it if true

var new_data=[];
Object.entries(queried_data[0]).forEach((key, value) => {
       if( value===true){new_data.push(key)}
});
console.log(new_data);

I think you are looking for

const returned_data = Object.entries(queried_data[0]).map((key, value) => {
    return value === true ? key : null
}).filter(key => {
    return key !== null
});

or simply

const returned_data = Object.keys(queried_data[0]).filter(key, queried_data[0][key] === true);

Don't use forEach!

发布评论

评论列表(0)

  1. 暂无评论