简体   繁体   English

使用 javascript 基于相同的属性获取 object 的数组

[英]fetch the array of object based on property same using javascript

I have two array of objects, arr1 and arr2我有两个对象数组, arr1arr2

if taskId and id same then retreive from arr1如果taskIdid相同,则从arr1检索

How to check based on property from arr1 and arr2 in javascript如何根据 javascript 中 arr1 和 arr2 的属性进行检查

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}
]

 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":编辑:要获得“我有两个 arrays 对象,arr1 和 arr2,如果 taskId 和 id 不同,则从 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);

This works too:这也有效:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用数组 object 和基于条件的数组列表使用 javascript 获取对象数组 - How to fetch the array of objects with array object and array list based on conditions using javascript 使用数组方法根据具有相同 id 的另一个数组的元素的存在设置数组元素 object 属性 - set array element object property based on the presence of an element of another array with the same id using array methods 根据一个属性的值和日期 object 对数组进行排序,以使用 javascript 获取最近的条目 - Sort an array with respect to value of one property and date object to fetch recent entry using javascript 如何使用javascript将特定数组提取到对象中 - How to fetch specific array into object using javascript 如何将一个数组和一个对象在同一个属性上“合并/连接”成一个平面数组(使用Javascript)? - How to “merge/concat” an array and an object over a same property into a flat array(using Javascript)? javascript如何获取对象属性 - javascript how to fetch object property 如何根据 object 属性(即数组)对对象数组进行排序 Javascript - How to sort array of objects based on object property (which is Array) Javascript 如何基于JavaScript中数组中对象的属性对数组进行排序 - How to sort array based on property of an object in the array in JavaScript Javascript中的数组对象中的组对象具有相同的属性 - Group Object have same Property in Array Object in Javascript 使用数组的对象的目标属性-Javascript - Target property of object using array - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM