0

I have an array of object b and an array a. I would like to filter b to only have objects where the to values are in a. I've tried many ways but I always receive an empty array. For example this case, the final result would be an array with the first and the second object from the array but not the last one.

const a = [
    "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ];

  const b = [
    {
      name: "toto",
      to: [
        "d7099abd-6872-48db-bdd7-ca99e4513586",
        "93272093-5089-40cd-8c3d-2412377e1f32"
      ]
    },
    { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
    { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
  ];

  console.log(b.filter(user => a.includes(user.to)))
samuel potter
  • 189
  • 3
  • 13

3 Answers3

0

Need to check array with array (use some and includes)

const a = [
  "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
  "d7099abd-6872-48db-bdd7-ca99e4513586",
  "93272093-5089-40cd-8c3d-2412377e1f32",
];

const b = [
  {
    name: "toto",
    to: [
      "d7099abd-6872-48db-bdd7-ca99e4513586",
      "93272093-5089-40cd-8c3d-2412377e1f32",
    ],
  },
  { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
  { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] },
];

console.log(b.filter(({ to }) => to.some((item) => a.includes(item))));
Siva K V
  • 10,561
  • 2
  • 16
  • 29
0

Your attempt,

console.log(b.filter(user => a.includes(user.to)))

fails to work because user.to is an array, and a does not include any arrays.

You need to use the every method to check if all elements of the array in the to property are inside a:

const a = [
    "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ];

const b = [
  {
    name: "toto",
    to: [
      "d7099abd-6872-48db-bdd7-ca99e4513586",
      "93272093-5089-40cd-8c3d-2412377e1f32"
    ]
  },
  { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
  { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
];

 console.log(b.filter(user => user.to.every(str => a.includes(str))));

EDIT: you could use some instead of every, as in @SivaKV's answer. That depends on whether you want an item to be included whose "to" array has some items in a and some not ("every" would not include these, but "some" would), which isn't clear from your question.

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34
0

You can use .some or .every depending on whether it is sufficient that one or all elements of the .to property must be in array a.

const a = [
"dfd302f4-571b-42da-9b35-07d1d9b6e68d",
"d7099abd-6872-48db-bdd7-ca99e4513586",
"93272093-5089-40cd-8c3d-2412377e1f32"
  ];
const b = [
{
  name: "toto",
  to: [
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ]
},
{ name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
{ name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
  ];

  console.log(b.filter(u => u.to.every(e=>a.includes(e))))
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43