-1

I have an array called active_filters which contains objects. Does anyone know how I would be able to access the category property to use like below?

My goal is the following

// For each active filter, use category for:

   $("#" + category).find("input:checked").each(function() 
   {
       // Do something
   }

// End for

Active Filters Array

{category: "search-filter-type", id: "data-type", value: "ICAR,SCAR", type: "filtered-out-by-car-type"}
{category: "search-filter-company", id: "data-company", value: "BU", type: "filtered-out-by-company"}
{category: "search-filter-location", id: "data-location", value: "AV-123", type: "filtered-out-by-location"}
2
  • What exactly are you wanting to do to the inputs? Commented May 8, 2018 at 21:28
  • Are you asking how to iterate over an array of objects? Commented May 8, 2018 at 21:29

3 Answers 3

0

As you have an array of objects you can use $.each() to iterate on them see a demo below.

var json = [{
  category: "search-filter-type",
  id: "data-type",
  value: "ICAR,SCAR",
  type: "filtered-out-by-car-type"
}, {
  category: "search-filter-company",
  id: "data-company",
  value: "BU",
  type: "filtered-out-by-company"
}, {
  category: "search-filter-location",
  id: "data-location",
  value: "AV-123",
  type: "filtered-out-by-location"
}];

$.each(json, function(index, filters) {
  console.log(filters.category);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

0

actually you can loop over your categories and the search for each category inside and use the $("#" + category)

let categories = [{
  category: "search-filter-type",
  id: "data-type",
  value: "ICAR,SCAR",
  type: "filtered-out-by-car-type"
}, {
  category: "search-filter-company",
  id: "data-company",
  value: "BU",
  type: "filtered-out-by-company"
}, {
  category: "search-filter-location",
  id: "data-location",
  value: "AV-123",
  type: "filtered-out-by-location"
}]


categories.forEach(cat => {
  let category = cat.category;
  console.log(category);
  $("#" + category).find("input:checked").each(function() {
    // Do something
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

You can use just js for the active_filters, like this:

active_filters = [
    {category: "search-filter-type", id: "data-type", value: "ICAR,SCAR", type: "filtered-out-by-car-type"},
    {category: "search-filter-company", id: "data-company", value: "BU", type: "filtered-out-by-company"},
    {category: "search-filter-location", id: "data-location", value: "AV-123", type: "filtered-out-by-location"}
];

for (var i in active_filters) {
    if (active_filters.hasOwnProperty(i)) {
        var category = active_filters[i];
        $("#" + category).find("input:checked").each(function() {
            // Do something
        }
    }
}

2 Comments

when using for..in you should check for .hasOwnProperty()
You are right, you always have to check, in case of "removed" items.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.