Search code examples
javascripttwitterarray-filter

Getting the same Object back when using the array.filter with a valid condition


I am trying to get the value of the 'text' key in a Json object that I get from a GET request to twitter via the twit library.

Im trying to use the filter function on the array because i wanna do functional programming.

I can use the json object inside a callback function on the T.get function:

const gotData = function(err, data, response) {
 console.log(data)
}

T.get('search/tweets', params , gotData)

Then I want to filter only the value on the text key on each of the statuses, which is an array of objects

So i tried this:

let results = data.statuses.filter(
    function(result){ return result.hasOwnProperty('text') 
}) 

But I get back the same data.statuses back... ?

I want to get only the value of the text keys on the objects, what im I doing wrong ?

this is my whole code:

const Twit = require('twit')
const config = require('./config')
const T = new Twit(config)

let params  = { q: 'drum', count: 2 }

const gotData = function(err, data, response) {
  let results = data.statuses.filter(function(result){ return result.hasOwnProperty('text')})
  console.log(results)
}

T.get('search/tweets', params , gotData)

And this a sample of the Json response i get, which corresponds to the data on the gotData function:

{ statuses:
   [ { created_at: 'Sun May 07 11:35:16 +0000 2017',
       id: 861182642888343600,
       id_str: '861182642888343552',
       text: '@was_going \nEvery hunk bore seven drums,\nEvery drum owned seven drums…',
       truncated: false,
       entities: [Object],
       metadata: [Object],
       source: '<a href="http://cheapbotsdonequick.com" rel="nofollow">Cheap Bots, Done Quick!</a>',
       in_reply_to_status_id: 861181362954489900,
       in_reply_to_status_id_str: '861181362954489856',
       in_reply_to_user_id: 795028044758949900,
       in_reply_to_user_id_str: '795028044758949888',
       in_reply_to_screen_name: 'was_going',
       user: [Object],
       geo: null,
       coordinates: null,
       place: null,
       contributors: null,
       is_quote_status: false,
       retweet_count: 0,
       favorite_count: 0,
       favorited: false,
       retweeted: false,
       lang: 'en' },
     { created_at: 'Sun May 07 11:35:09 +0000 2017',
       id: 861182610596405200,
       id_str: '861182610596405248',
       text: 'Would you rather have Data storage or A pedal assembly for a bass drum or high hat cymbals?',
       truncated: false,
       entities: [Object],
       metadata: [Object],
       source: '<a href="http://cheapbotsdonequick.com" rel="nofollow">Cheap Bots, Done Quick!</a>',
       in_reply_to_status_id: null,
       in_reply_to_status_id_str: null,
       in_reply_to_user_id: null,
       in_reply_to_user_id_str: null,
       in_reply_to_screen_name: null,
       user: [Object],
       geo: null,
       coordinates: null,
       place: null,
       contributors: null,
       is_quote_status: false,
       retweet_count: 0,
       favorite_count: 0,
       favorited: false,
       retweeted: false,
       lang: 'en' } ],
  search_metadata:
   { completed_in: 0.034,
     max_id: 861182642888343600,
     max_id_str: '861182642888343552',
     next_results: '?max_id=861182610596405247&q=drum&count=2&include_entities=1',
     query: 'drum',
     refresh_url: '?since_id=861182642888343552&q=drum&include_entities=1',
     count: 2,
     since_id: 0,
     since_id_str: '0' } }

Solution

  • You just check if theres a text property. You might also want to map it to that property:

    let results = data.statuses.filter(
       result=>result.hasOwnProperty('text') 
    ).map(result=>result.text); 
    

    http://jsbin.com/niwezocuhu/edit?console

    Or if you take care on performance ( O(n) instead of O(n+n*r)):

    let results= data.statuses.reduce((arr,result)=>(result.text&&arr.push(result.text)&&false)||arr,[]);