1

I have two array of objects,arr1 and arr2

if taskId and id same then retreive from arr1

How to check based on property from arr1 and arr2 in javascript

var arr1= [
  {"name":"refresh task","memberIds":[981],"dueOn":"2022-08-30","taskId":19},
  {"name":"ref one","memberIds":[981,982],"dueOn":"2022-08-25","taskId":null}
]

var arr2 =[
{
"country": "IN", 
"tasks": [
    {id: 19, "name": "abc" },
    {id: 20, "name": "xyz" }
  ]
}
]
I tried 
var result = arr1.filter(e=>e.taskId===(arr2.map(i=>i.tasks.map(t=>t.id))

Expected Output
[
  {"name":"refresh task","memberIds":[981],"dueOn":"2022-08-30","taskId":19}
]

1
  • do a flatMap+map on arr2 to get a list of id. after that do a filter on arr1 and check if each element is include in the id list Commented Aug 2, 2022 at 19:16

2 Answers 2

2
  • Using Set, Array#flatMap, and [Array#map][3], get the list of task ids in arr2``
  • Using Array#filter, and Set#has, return the resulting array where taskId is in the above set

const
  arr1= [
    {"name":"refresh task", "memberIds":[981], "dueOn":"2022-08-30", "taskId":19},
    {"name":"ref one", "memberIds":[981,982], "dueOn":"2022-08-25", "taskId":null}
  ],
  arr2 =[
    { 
      "country": "IN", 
      "tasks": [ {"id": 19, "name": "abc"}, {"id": 20, "name": "xyz"} ]
    }
  ];

const taskIdSet = new Set(
  arr2.flatMap(({ tasks = [] }) => tasks.map(({ id }) => id))
);
const result = arr1.filter(({ taskId }) => taskIdSet.has(taskId));

console.log(result);

EDIT: To get "I have two arrays of objects, arr1, and arr2 if taskId and id not same then retrieve from arr2":

const
  arr1= [
    {"name":"refresh task", "memberIds":[981], "dueOn":"2022-08-30", "taskId":19},
    {"name":"ref one", "memberIds":[981,982], "dueOn":"2022-08-25", "taskId":null}
  ],
  arr2 =[
    { 
      "country": "IN", 
      "tasks": [ {"id": 19, "name": "abc"}, {"id": 20, "name": "xyz"} ]
    }
  ];

const taskIdSet = arr1.reduce((taskIdSet, { taskId }) => {
  if(taskId !== null) { taskIdSet.add(taskId); }
  return taskIdSet;
}, new Set);
const result = arr2
  .flatMap(({ tasks = [] }) => tasks)
  .filter(({ id }) => !taskIdSet.has(id));

console.log(result);

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

5 Comments

thanks for the solution, if i want get arr2 that are not matching with arr1 taskId , i.e(arr2 with no null and not same taskid and id)
@can help how to do
I have two array of objects,arr1 and arr2 if taskId and id not same then retreive from arr2 Expected Output [ {id: 20, "name": "xyz" } ]
@codelearn in this case, you need to save non-null task ids from arr1 in a set and do the filter on arr2
Answer updated.
0

This works too:

var result = arr1.filter(e=>{
    for(let obj of arr2){ 
      for(let task of obj.tasks){
        return e.taskId === task.id; 
        }
    }
 })

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.