Home > Enterprise >  How to check array of object having same id and null in javascript
How to check array of object having same id and null in javascript

Time:08-04

Given array of objects arr1 and arr2

check every object in array

taskId of arr1 and id of arr2 are equal, and

and null taskid, remove and returning the arr2 values in javascript

if both array of object has same id and taskid, if arr2 has null value in taskid, return reamining object arrary in arr2 using javascript

tried

const result = arr2.map(async task => {!arr1.some(arr1 => arr1.taskId === task.id)})

var arr1 =[
  {
    "name": "sample task",
    "memberIds": [
      981,
      983
    ],
    "dueOn": "2022-08-31",
    "taskId": 10
  },
  {
    "name": "skills one more",
    "memberIds": [
      981,
      983,
      984
    ],
    "dueOn": "2022-08-31",
    "taskId": null
  },
  {
    "name": "new task one",
    "memberIds": [
      982,
      983
    ],
    "dueOn": "2022-08-2",
    "taskId": 15
  },
]
var arr2 =[
  {
    "id": 10,
    "createdAt": "2022-08-02T09:10:07.000Z",
    "createdBy": 21,
    "updatedAt": null,
    "updatedBy": null,
    "statusId": 6207,
    "caseGoalId": 11,
    "name": "sample task",
    "dueOn": "2022-08-31"
  }
]

Expected Output

 [{
    "name": "new task one",
    "memberIds": [
      982,
      983
    ],
    "dueOn": "2022-08-2",
    "taskId": 15
  }]

CodePudding user response:

You need to filter arr1, and on each item inside, you need to check if exist in the arr2 and return if it exist or if the item id equal null

var arr1 =[
  {
    "name": "sample task",
    "memberIds": [
      981,
      983
    ],
    "dueOn": "2022-08-31",
    "taskId": 10
  },
  {
    "name": "skills one more",
    "memberIds": [
      981,
      983,
      984
    ],
    "dueOn": "2022-08-31",
    "taskId": null
  },
  {
    "name": "new task one",
    "memberIds": [
      982,
      983
    ],
    "dueOn": "2022-08-2",
    "taskId": 15
  },
]
var arr2 =[
  {
    "id": 10,
    "createdAt": "2022-08-02T09:10:07.000Z",
    "createdBy": 21,
    "updatedAt": null,
    "updatedBy": null,
    "statusId": 6207,
    "caseGoalId": 11,
    "name": "sample task",
    "dueOn": "2022-08-31"
  }
]

const result = arr1.filter(item => item.id === null || arr2.some(i => i.id === item.taskId))

console.log(result)

CodePudding user response:

There must be some missing information. There's no way to get the expected output with these requirements. Your taskId's from arr1 are 10, null, & 15. The only id in arr2 is 10 therefore you would never get this output:

[{
   "name": "new task one",
   "memberIds": [
     982,
     983
   ],
   "dueOn": "2022-08-2",
   "taskId": 15
}]

but instead get:

[{
    "name": "sample task",
    "memberIds": [
        981,
        983
    ],
    "dueOn": "2022-08-31",
    "taskId": 10
}]

CodePudding user response:

This is a way you can do it...

Time complexity O(N^2)

const check = (arr1, arr2) => {
      for (let i = 0; i < arr1.length; i  ) {
        for (let j = 0; j < arr2.length; j  ) {
          if (arr1[i] === null) {
            return [...arr2];
          }
          if (arr1[i].taskId !== null && arr1[i].taskId === arr2[j].id) {
            return { ...arr1[i] };
          }
    
          // ...more checks...
        }
      }

};
  • Related