1

const users=[[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]]
const user_id=["5fbfa748fc46a415ce5503a8", "5fbf6f91aff7f3320a906547"]

const {matching, nonMatching} = users.reduce((acc, userRecord) => {
  userRecord.key && userRecord.key.some(key=> user_id.includes(key))
    ? acc.matching.push(userRecord)
    : acc.nonMatching.push(userRecord)
  return acc
}, {matching: [], nonMatching: []})

console.log(matching)

How to separate match unpatch data base on user_id array data ? for putting string into user_id includes does work fine but putting array into user_id it does not work

4 Answers 4

0

Use Array#some() if you want any match or Array#every() if you need all to match

const users=[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]
const user_id=["5fbfa748fc46a415ce5503a8", "5fbf6f91aff7f3320a906547"]

const {matching, nonMatching} = users.reduce((acc, userRecord) => {
  userRecord.key && userRecord.key.some(key=> user_id.includes(key))
    ? acc.matching.push(userRecord)
    : acc.nonMatching.push(userRecord)
  return acc
}, {matching: [], nonMatching: []})

console.log(matching)

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

Comments

0

includes method can handle only primitive values, but you put into it an array. This can help I think.

const users=[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]
const user_id=["5fbfa748fc46a415ce5503a8", "5fbf6f91aff7f3320a906547"]

const {matching, nonMatching} = users.reduce((acc, userRecord) => {
  userRecord.key && userRecord.key.every(key => user_id.includes(key))
    ? acc.matching.push(userRecord)
    : acc.nonMatching.push(userRecord)
  return acc
}, {matching: [], nonMatching: []})

console.log(matching)

Comments

0

This solution should work.

const users=[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]
const user_id=["5fbfa748fc46a415ce5503a8", "5fbf6f91aff7f3320a906547"]

const {matching, nonMatching} = users.reduce((acc, userRecord) => {
  userRecord.key && userRecord.key.find(userRecordKeyItem => user_id.find(userIdItem => userIdItem === userRecordKeyItem))
    ? acc.matching.push(userRecord)
    : acc.nonMatching.push(userRecord)
  return acc
}, {matching: [], nonMatching: []})

console.log(matching)

Comments

0

You can use a simple forEach with an if else block

const users =[{_id:"5fbfa729fc46a415ce5503a6",first_name:"Allen",last_name:"Border",timestamp:1606395689121,key:["5fbf6f91aff7f3320a906547","5fbfa748fc46a415ce5503a8"]},{_id:"5fbfa6fbfc46a415ce5503a4",first_name:"james",last_name:"roger",timestamp:1606395689125},{_id:"5fbf6f91aff7f3320a906547",first_name:"david",last_name:"gosh",timestamp:1606395689130,key:["5fbfa729fc46a415ce5503a6"]},{_id:"5e4e74eb380054797d9db623",first_name:"Ricky",last_name:"bichel",timestamp:1606395689131}]
const user_id=["5fbfa748fc46a415ce5503a8", "5fbf6f91aff7f3320a906547"]
    
const matches = [];
const dismatches = [];
users.forEach(user => {
       if(user_id.includes(user._id)) {
         matches.push(user);
       } else {
         dismatches.push(user);
      }
});
console.log({matches ,dismatches });

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.