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

131
Views
searching an array of objects on the basis of value present inside an value of an object in form of array

I have an an object with two fields filter1 and filter2 having values in form of array

let filter = {filter1:["mine","your"]: filter2:["C","D"]} //values not fixed

the data is in form of array of objects

let data = [
{ id:1, filter1:["mine"], filter2:["E","C"]},
{ id:2, filter1:["mine"], filter2:["E","C","F"]},
{ id:3, filter1:["your"], filter2:["C"]},
{ id:3, filter1:["your"], filter2:["D","C"]},
{ id:5, filter1:["other"], filter2:["F"]},
...
]

I have to filter out those objects which have any one of the fields present in specific key for eg if filter is {filter1:["mine"]: filter2:["F","D"]} it will first search for any element of filter1 in filter1 of data object and after that search for any element of filter2 which is present in the filter2 of data object and return the object if any one them is found

few example

result for {filter1:["mine"]: filter2:["F","D"]}

result = [
{ id:1, filter1:["mine"], filter2:["E","C"]}, //since filter1 "mine"
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since filter1 "mine"
{ id:3, filter1:["your"], filter2:["D","C"]}, //since from "F" and "D" from filter2 "D" is present
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is present
]

result for {filter1:["your"]: filter2:["F","G"]}

result = [
{ id:2, filter1:["mine"], filter2:["E","C","F"]}, //since "F" from filter2 is present
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter1 is "your"
{ id:5, filter1:["other"], filter2:["F"]}, //since "F" from filter2 is present
]

result for {filter1:[]: filter2:["D"]}

result = [
{ id:3, filter1:["your"], filter2:["D","C"]}, //since filter2 has "D"
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use a combination of .filter(), .some() and .includes():

const data = [
  { id:1, filter1:["mine"], filter2:["E","C"]},
  { id:2, filter1:["mine"], filter2:["E","C","F"]},
  { id:3, filter1:["your"], filter2:["C"]},
  { id:3, filter1:["your"], filter2:["D","C"]},
  { id:5, filter1:["other"], filter2:["F"]}
];

const search = ({ filter1, filter2 }) => 
  data.filter(item => 
    item.filter1.some(fItem => filter1.includes(fItem)) ||
    item.filter2.some(fItem => filter2.includes(fItem))
);

const result = search({ filter1:["mine"], filter2:["F","D"] });
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can generalize the solution proposed by RoMilton by calling some() on the Object.entries() of the passed filter object, and then iterating each key and filter_array with nested some() calls.

If you also Array#concat() the currently iterated data element property into an array you can include non-array properties in the filter object ie id in this case.

const data = [
  { id: 1, filter1: ["mine"], filter2: ["E", "C"] },
  { id: 2, filter1: ["mine"], filter2: ["E", "C", "F"] },
  { id: 3, filter1: ["your"], filter2: ["C"] },
  { id: 3, filter1: ["your"], filter2: ["D", "C"] },
  { id: 5, filter1: ["other"], filter2: ["F"] }
];

const search = (array, filter_object) =>
  array.filter(item =>
    Object.entries(filter_object).some(([key, filter_array]) =>
      [].concat(item[key]).some(fitem => filter_array.includes(fitem)))
  );

const filter = { filter1: ["mine"], filter2: ["F", "D"] };
const result = search(data, filter);

console.log(...result.map(({ id }) => ({ id })));

const filter2 = { id: [5], filter1: ["mine"] }
const result2 = search(data, filter2);

console.log(...result2.map(({ id }) => ({ id })));
.as-console-wrapper { max-height: 100% !important; top: 0; }

This use of concat() can also be applied to the hard coded solution to allow for properties that may not be arrays in your data array.

const data = [
  { id: 1, filter1: "mine", filter2: ["E", "C"] },
  { id: 2, filter1: ["mine"], filter2: ["E", "C", "F"] },
  { id: 3, filter1: ["your"], filter2: ["C"] },
  { id: 3, filter1: ["your"], filter2: ["D", "C"] },
  { id: 5, filter1: ["other"], filter2: ["F"] }
];

const search = ({ filter1, filter2 }) => (
  data.filter(item =>
    [].concat(item.filter1).some(fItem => filter1.includes(fItem)) ||
    [].concat(item.filter2).some(fItem => filter2.includes(fItem))
  ));

const result = search({ filter1: ["mine"], filter2: ["F", "D"] });
console.log(result);

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!