1

I have array which i got as a result from nodeJS code

Original JSON data:

JS code:

setInterval(function() {
  var notify = db.get('users')
    .filter({notify: "true"})
    .value()
    console.log("1");
    console.log(notify);

}, 10 * 1000);

Result:

[ { uid: '177098244407558145',
    pubgUser: 'Jengas',
    pubgServer: 'pc-eu',
    notify: 'true' },
  { uid: '407970368847085578',
    pubgUser: 'Lovec_Pokemonov',
    pubgServer: 'pc-eu',
    notify: 'true' },
  { uid: '4307970368847085578',
    pubgUser: 'Lossvec_Pokemonov',
    pubgServer: 'pc-eu',
    notify: 'true' },
  { uid: '407970368847015578',
    pubgUser: 'SDLovec_Pokemonov',
    pubgServer: 'pc-eu',
    notify: 'true' } ]

I wanted to get all uid values which had "true". But result was giving me "undefined" for console.log(notify.uid);

Expected result: 177098244407558145, 407970368847085578, 4307970368847085578, 407970368847015578

2 Answers 2

3

You can use map method in combination with filter.

For this, you have to pass a callback function for each of the two methods or just use arrow functions which are specific for latest versions of ES.

let data = [ { uid: '177098244407558145', pubgUser: 'Jengas', pubgServer: 'pc-eu', notify: 'true' }, { uid: '407970368847085578', pubgUser: 'Lovec_Pokemonov', pubgServer: 'pc-eu', notify: 'true' }, { uid: '4307970368847085578', pubgUser: 'Lossvec_Pokemonov', pubgServer: 'pc-eu', notify: 'true' }, { uid: '407970368847015578', pubgUser: 'SDLovec_Pokemonov', pubgServer: 'pc-eu', notify: 'true'}  ]
    
uid_array = data.filter(a => a.notify).map(a => a.uid);
console.log(uid_array);

Sign up to request clarification or add additional context in comments.

Comments

1

notify type is string so we need to check with 'true'

let data = [ { uid: '177098244407558145', pubgUser: 'Jengas', pubgServer: 'pc-eu', notify: 'true' }, { uid: '407970368847085578', pubgUser: 'Lovec_Pokemonov', pubgServer: 'pc-eu', notify: 'true' }, { uid: '4307970368847085578', pubgUser: 'Lossvec_Pokemonov', pubgServer: 'pc-eu', notify: 'true' }, { uid: '407970368847015578', pubgUser: 'SDLovec_Pokemonov', pubgServer: 'pc-eu', notify: 'false'}  ]
    
uid_array = data.filter(a => a.notify==='true').map(a => a.uid);
console.log(uid_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.