-2

I'm trying to get single object with different item based by the id from compared result of two array of objects.

Here's the case:

First Array

[
    {
        "id": "792657571767910421",
        "type": 0,
        "allow": "0",
        "deny": "0"
    },
    {
      "id": "1020443938668171294",
      "type": 0,
      "allow": "0",
      "deny": "377959221312"
    },
    {
      "id": "791708642813411358",
      "type": 0,
      "allow": "0",
      "deny": "0"
    }
]

Second Array

[
    {
      "id": "792657571767910421",
      "type": 0,
      "allow": "1024",
      "deny": "0"
    },
    {
      "id": "1020443938668171294",
      "type": 0,
      "allow": "0",
      "deny": "377959221312"
    },
    {
      "id": "791708642813411358",
      "type": 0,
      "allow": "0",
      "deny": "0"
    },
]

Expected Output

[
    {
      "id": "792657571767910421",
      "type": 0,
      "allow": "1024",
      "deny": "0"
    },
]

Different value

// First array
{
    "allow": "0"
}

// Second array
{
    "allow": "1024"
}

Can anyone help me? Thank you.

  • And have you tried anything? – Rajesh Oct 11 '22 at 03:37
  • Please post the broken JavaScript as a [mcve] so we can fix it, we do not write code (or at least we shouldn't) if there's no effort on your side. – zer00ne Oct 11 '22 at 03:42
  • there are 2 arrays . and your expected result is the second array . then what is the logic to filter them . on which basis you wanna filter them . you just simply expect your output as the second array – Rahul Mohanty Oct 11 '22 at 03:43
  • I've tried to find it like this: `arr1.filter((item) => !arr2.some((i) => i.allow === item.allow))` but it return all the items. – Rezky Rizaldi Oct 11 '22 at 03:47
  • Are the arrays _symmetric_? meaning that objects must correspond in their position (index) in each array? Are objects at different indices ever compared? (if you sort them by id, are they symmetric?) or are we always comparing first[i] to second[i]? In which case you'll just need to look for a [_deepEqual_ implementation](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects). Comparing `JSON.stringify`'ed versions of the objects can work in a pinch. – Wyck Oct 11 '22 at 04:08

1 Answers1

0

You will have to use custom filter logic:

Logic:

  • Loop over arr2 or the array you need output from.
  • Test items with param2. On match:
    • Loop over arr1 or the other array.
    • Find the object with same ID.
    • On finding,
      • Test item with param1. Return test value.
    • Else return false

function filter(arr1, arr2, param1, param2) {
  const test = (param, item) =>
    Object
      .entries(param)
      .every(([k, v]) => item[k] === v)

  return arr2.filter((itemB) => {
    if (test(param2, itemB)) {
      const itemA = arr1.find(({id}) => id === itemB.id)
      return !!itemA ? test(param1, itemA) : false
    }
    return false
  })
}

const arr1 = [{"id": "792657571767910421","type": 0,"allow": "0","deny": "0"},{"id": "1020443938668171294","type": 0,"allow": "0","deny": "377959221312"},{"id": "791708642813411358","type": 0,"allow": "0","deny": "0"}]
const arr2 = [{"id": "792657571767910421","type": 0,"allow": "1024","deny": "0"},{"id": "1020443938668171294","type": 0,"allow": "0","deny": "377959221312"},{"id": "791708642813411358","type": 0,"allow": "0","deny": "0"},]

const param1 = {"allow": "0"}
const param2 = {"allow": "1024"}

console.log(filter(arr1, arr2, param1, param2))
Rajesh
  • 24,354
  • 5
  • 48
  • 79