1

Filter Array with objects based on another array dynamically.

I need to filter a main array using another array. But the filter array will only contain a few fields or several.

    var codes = [{
"title": "lore",
"city": "New York",
"desc": "lorem lorem",
"age": "32"
 },
{
    "title": "lore2 ",
    "city": "Santa Monica",
    "desc": "lorem2",
    "age": "20"
  }

];

let filter = [{
  "city": "New York"
}, {
  "age": "20"
},

...Or more filters. This filter is dynamically created by the person clicking on the checkbox.

]

2
  • why is your filter an array ? making it object makes more sense. no ? Commented Sep 29, 2021 at 19:04
  • what is the wanted result? please add your code. Commented Sep 29, 2021 at 19:32

4 Answers 4

2

You could take a better data structure which is easier to use instead of unknown keys.

As result you get object which all keys/values match.

const
    codes = [{ title: "lore", city: "New York", desc: "lorem lorem", age: "32" }, { title: "lore2 ", city: "New York", desc: "lorem2", age: "20" }],
    filter = [{ key: "city", value: "New York" }, { key: "age", value: "20" }],
    result = codes.filter(o => filter.every(({ key, value }) => o[key] === value));

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

interesting idea. ty
1

This should work

let codes = [{
    "title": "lore",
    "city": "New York",
    "desc": "lorem lorem",
    "age": "20"
  },
  {
    "title": "lore2 ",
    "city": "Santa Monica",
    "desc": "lorem2",
    "age": "20"
  }
];

let filter = [{
  "city": "New York"
}, {
  "age": "20"
}];

let res = codes.filter(code => {
  return filter.every(f => {
    const [key, value] = Object.entries(f)[0];
    return code[key] == value
  });
});

console.log(res)

1 Comment

Worked perfectly. Tyvm
0

Maybe something like this could work for you?

Basically looping through filters and then also through the seperate filter objects key-value pairs and checking them against the codes.

var codes = [{
  "title": "lore",
  "city": "New York",
  "desc": "lorem lorem",
  "age": "32"
},
{
  "title": "lore2 ",
  "city": "Santa Monica",
  "desc": "lorem2",
  "age": "20"
}];

var filters = [{"city": "New York"}, {"age": "32"}];


function filterByMultipleFilters(data, filters) {
  return data.filter(item => {
    // Loop through all filters and check the current item
    for (const filter of filters) {
      for (const [key, value] of Object.entries(filter)) {
        if (!item[key] || item[key] !== value)
          return false;
      }
    }
    return true;
  });
}

console.clear();
let result = filterByMultipleFilters(codes, filters);
console.log(result);

Comments

0

Try the following code sample, I think what you want;

try to get filters object directly instead of another array;

var codes = [{ "title": "lore", "city": "New York", "desc": "lorem lorem", "age": "32" }, { "title": "lore2 ", "city": "Santa Monica", "desc": "lorem2", "age": "20" } ];

let filters = [{
  "city": "Santa Monica"
}, {
  "age": "20"
}];

const filterCode = {};
filters.forEach(filter => {
  for (var key of Object.keys(filter))
    filterCode[key] = filter[key];
});
var filteredCodes = codes.filter(code => {
  for (var key of Object.keys(filterCode)) {
      if(code[key] !== filterCode[key])
        return false;
  }
  return true;
});

console.log(filteredCodes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.