Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Filter an object by value from array

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)))
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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))));

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report

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))))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!