2
let project = {
    title: "some title",
    participants: [{
        _id: "12345678",
        name: "John"
    }, {
        _id: "44332211",
        name: "Jackson"
    }, {
        _id: "55667788",
        name: "Steve"
    }]
}

let users = [{
    _id: "12345678",
    name: "John"
}, {
    _id: "44332211",
    name: "Jackson"
}, {
    _id: "09876543",
    name: "Aaron"
}, {
    _id: "55667788",
    name: "Steve"
}, {
    _id: "22334455",
    name: "Xavier"
}]

How do I list out all the users that are NOT part of the project participants? includes does not work because project is an object...

4
  • 1
    Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet Commented Jun 12, 2020 at 18:05
  • Filter them Commented Jun 12, 2020 at 18:07
  • 5
    wow, three upvotes and no code? and no result. Commented Jun 12, 2020 at 18:09
  • @mplungjan I searched for related topic before I ask, and I tried their solution, does not work that's why I came here to ask for help Commented Jun 12, 2020 at 18:27

4 Answers 4

6

array.filter + array.some will work:

let project = {
    title: "some title",
    participants: [{
        _id: "12345678",
        name: "John"
    }, {
        _id: "44332211",
        name: "Jackson"
    }, {
        _id: "55667788",
        name: "Steve"
    }]
}

let users = [{
    _id: "12345678",
    name: "John"
}, {
    _id: "44332211",
    name: "Jackson"
}, {
    _id: "09876543",
    name: "Aaron"
}, {
    _id: "55667788",
    name: "Steve"
}, {
    _id: "22334455",
    name: "Xavier"
}]

let result = users.filter(user => !project.participants.some(p => p._id === user._id));
console.log(result);

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

3 Comments

This is the same solution I was going to post until you did it, upvoted it.
thanks, I tried this filter + some function before I posted this question, it does not work on my actual code, when I console log project.partcipants[0]._id and users[0]._id, both has the same value, but it still evaluate as "false", guess I have to apply toString method to make it work
If the ids don't have the same type. For example one being an integer and on being a string you could also use == instead of ===.
1

You can also make use of find with filter:

var users = [{ _id: "12345678", name: "John"}, { _id: "44332211", name: "Jackson"}, { _id: "09876543", name: "Aaron"}, { _id: "55667788", name: "Steve"}, { _id: "22334455", name: "Xavier"}];
var project = { title: "some title", participants: [{ _id: "12345678", name: "John" }, { _id: "44332211", name: "Jackson" }, { _id: "55667788", name: "Steve" }]};

var result = users.filter(k=>!project.participants.find(p=>p._id==k._id));

console.log(result);

Comments

1

Using Array.map and Array.filter

let project = { title: "some title",
    participants: [{_id: "12345678", name: "John"},
    {_id: "44332211", name: "Jackson"},
    {_id: "55667788", name: "Steve"}]
}

let users = [{ _id: "12345678", name: "John"}, 
{ _id: "44332211", name: "Jackson"},
{ _id: "09876543", name: "Aaron"},
{ _id: "55667788", name: "Steve"},
{ _id: "22334455", name: "Xavier"}];

var participants = project.participants.map(function(p){ return p._id; })
var non_participants = users.filter(function(user){
  return participants.indexOf(user._id) == -1;
});
console.log(non_participants);

Comments

1

I would first extract the participant ids into a Set for fast lookup. Then filter the users and check if the id is not included in the set (using has).

let project = {
  title: "some title",
  participants: [
    { _id: "12345678", name: "John"    },
    { _id: "44332211", name: "Jackson" },
    { _id: "55667788", name: "Steve"   },
  ]
};
let users = [
  { _id: "12345678", name: "John"    },
  { _id: "44332211", name: "Jackson" },
  { _id: "09876543", name: "Aaron"   },
  { _id: "55667788", name: "Steve"   },
  { _id: "22334455", name: "Xavier"  },
];

const participantIds = new Set(project.participants.map(participant => participant._id));
const usersWithoutProject = users.filter(user => !participantIds.has(user._id));

console.log(usersWithoutProject);

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.