0

I have this object of objects, I am trying to get the items which include a social

const events = {
    'event-1': { id: 'event-1', channel: 'facebook'},
    'event-2': { id: 'event-2', channel: 'twitter'},
    'event-3': { id: 'event-3', channel: 'slack'},
  };

I need to get for example event that has facebook and slack Any ideas how filter works on javascript for objects not array?

Thank you

2
  • Any reason you're using an object instead of an array? Commented Jul 22, 2021 at 13:58
  • I need to filter from events object, I am doing sorting, so I need when I check facebook to get events that have facebook Commented Jul 22, 2021 at 13:58

3 Answers 3

5

You can turn it to entries, filter them, and turn it back to an object:

const events = {
    'event-1': { id: 'event-1', channel: 'facebook'},
    'event-2': { id: 'event-2', channel: 'twitter'},
    'event-3': { id: 'event-3', channel: 'slack'},
  }
  
const result = Object.fromEntries( 
    Object.entries(events).filter( ([key,value]) => value.channel == "facebook" || value.channel == "slack")
);

console.log(result);

You can check a list of channels by putting them in an array and using includes

const events = {
    'event-1': { id: 'event-1', channel: 'facebook'},
    'event-2': { id: 'event-2', channel: 'twitter'},
    'event-3': { id: 'event-3', channel: 'slack'},
  }
  
const channels = ["facebook","slack"];
const result = Object.fromEntries( 
    Object.entries(events).filter( ([key,value]) => channels.includes(value.channel))
);

console.log(result);

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

1 Comment

is there a way instead of value.channel || value.channel to do it something like value.channel = ['facebook', 'twitter']
2

Try this:

const events = {
            'event-1': { id: 'event-1', channel: 'facebook' },
            'event-2': { id: 'event-2', channel: 'twitter' },
            'event-3': { id: 'event-3', channel: 'slack' },
        };
        
var val = Object.values(events).filter(t=>t.channel == 'facebook' || t.channel == 'slack');

console.log(val);

3 Comments

thanks, what if I want to include facebook + slack I tried ['facebook', 'slack'] but no luck
@lilo Just use || operator. you know filter needs Boolean expression, So you can use || or includes. I updated my answer
@lilo if you want to use ['facebook', 'slack'] you can use this way: Object.values(events).filter(t => ['facebook', 'slack'].includes(t.channel));
0

Maintain a separate variable that holds all the social platforms and filter events array based on that...

const socialPlatforms = [
  'facebook',
  'slack'
];

const events = {
  'event-1': { id: 'event-1', channel: 'facebook'},
  'event-2': { id: 'event-2', channel: 'twitter'},
  'event-3': { id: 'event-3', channel: 'slack'},
};

const socialEvents = Object.values(events)
  .filter(event => socialPlatforms.includes(event.channel))
  .map(event => event.id);

console.log(socialEvents);

/*

[ 'event-1', 'event-3' ]

*/

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.