skip to Main Content

wondering if someone can help me, i have an array of objects like this with more tha 200 records:

[
  {
    "id": "86106175277",
    "createdAt": "2022-09-26T20:57:10.850Z",
    "updatedAt": "2023-02-10T22:23:32.721Z",
    "publishedAt": "2023-02-10T22:46:29.561Z",
    "values": {
      "primary_image": {
        "url": "N17D0087-1.jpg",
        "width": 1920,
        "height": 1282,
        "altText": "",
        "type": "image"
      },
      "matterport": "Jabw7wfaoLg",
      "sleeps": 4,
      "brochure": "Jay-Flight-SLX-Brochure-2023.pdf",
      "visible": 1,
      "model_id_": "154BH",
      "color": "FARMHOUSE",
      "year": "2022",
      "black": 19.9,
      "length": "18",
      "chassis": "N/A",
      "type": "Travel Trailer",
      "floorplan": "6644-154BH.png",
      "condition": "USED",
      "gray": 19.9,
      "engine": "N/A",
      "fuel_capacity": "N/A",
      "msrp": "30070",
      "sales_price": 19988,
      "model": "SLX",
      "location": "Vogt RV - Towables",
      "sku": "N17D0087",
      "fresh": 10,
      "make": "JAYCO"
    },
    "path": "jayco-slx-154bh-2012",
    "name": "JAYCO SLX 154BH 2022",
    "childTableId": "0",
    "isSoftEditable": false
  },
  {
    "id": "86106175278",
    "createdAt": "2022-09-26T20:57:10.857Z",
    "updatedAt": "2023-02-10T22:23:36.289Z",
    "publishedAt": "2023-02-10T22:46:29.561Z",
    "values": {
      "primary_image": {
        "url": "N17D0334/N17D0334-1.jpg",
        "width": 1920,
        "height": 1282,
        "altText": "",
        "type": "image"
      },
      "matterport": "Jabw7wfaoLg",
      "sleeps": 4,
      "brochure": "Jay-Flight-SLX-Brochure-2023.pdf",
      "visible": 1,
      "model_id_": "154BH",
      "color": "FARMHOUSE",
      "year": "2022",
      "black": 19.9,
      "length": "18",
      "chassis": "N/A",
      "type": "Travel Trailer",
      "floorplan": "6644-154BH.png",
      "condition": "USED",
      "gray": 19.9,
      "engine": "N/A",
      "fuel_capacity": "N/A",
      "msrp": "32392",
      "sales_price": 27533,
      "model": "SLX",
      "location": "Vogt RV - Towables",
      "sku": "N17D0334",
      "fresh": 10,
      "make": "JAYCO"
    },
    "path": "jayco-slx-154bh-2012-2",
    "name": "JAYCO SLX 154BH 2022",
    "childTableId": "0",
    "isSoftEditable": false
  }
]

I would like to filter this array of objects (OR for each record with same key -> value), with another dynamic array of objects (this second array changes based on selected checkboxes), by their ‘values’ with jQuery or vanilla javascript, this is the second array:

[
  {
    "category": "location",
    "value": "Vogt RV - Motorhomes"
  }
]

or it could be:

[
  {
    "category": "location",
    "value": "Vogt RV - Motorhomes"
  },
  {
    "category": "location",
    "value": "Leisure RV Center"
  },
  {
    "category": "location",
    "value": "Vogt RV - Towables"
  },
  {
    "category": "condition",
    "value": "NEW"
  }
]

How do i filter the first array of objects based on this second array of objects?

Thank you!

I tried like this, but it doesn´t work:

myArrayFiltered = modelos[0].filter((item) => {
  return filtersHubDb.every(valo => {
      return valo.value === item.values[valo.category];
  });
});

Thank you!

2

Answers


    • x is the first array
    • y is second array
    • z is filtered result

    Example below is for single filter (one item in y). You can add a loop to check more than one filter from the y array.

    z = x.filter((item) => {
      return item.values[y[0].category] === y[0].value;
    });
    
    Login or Signup to reply.
  1. Not sure what modelos[0] is for. However it seems like the main problem is that your EVERY should be SOME.

    myArrayFiltered = modelos.filter((item) => {
      return filtersHubDb.some(valo => {
          return valo.value === item.values[valo.category];
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search