0

enter image description here

I want to filter an object array taking into account multiple attribute values. The attributes are selected using checkboxes, I want to filter the array using those values (for example, Ram, Primary camera).

I want to filter like an E-commerce website:

var myObject = [
    {
        "ProId": 12,
        "ProName": "Samsung Galaxy A9",
        "AttriValue": {
            "Front Camera": "16 MP and Above",
            "Internal Memory": "128 GB and Above",
            "Network Type": "4G",
            "Primary Camera": "16 MP and Above",
            "Ram": "6 GB"
        }
    },
    {
        "ProId": 11,
        "ProName": "Vivo Y95",
        "AttriValue": {
            "Front Camera": "16 MP and Above",
            "Internal Memory": "64 GB",
            "Network Type": "4G",
            "Primary Camera": "13 - 15.9 MP",
            "Ram": "4 GB"
        }
    },
    {
        "ProId": 10,
        "ProName": "OPPO A7",
        "AttriValue": {
            "Front Camera": "16 MP and Above",
            "Internal Me...
        ....
     }
 ]
4
  • 1
    Your question is unclear. I have a multiple check box with attribute name. Please add your html code too. Commented Jun 17, 2019 at 11:33
  • 1
    the "myObject" array is incomplete. Please provide Commented Jun 17, 2019 at 11:36
  • Possible duplicate of How to filter object array based on attributes? Commented Jun 17, 2019 at 11:40
  • in the image when i multiple checked in internal memory(Attribute Name) then filter product according to checked value (Attribute Value like 64 gb 32 gb) and when i multiple checked in ram(Attribute Name) then filter product according to checked value like 4 gb 8 gb Commented Jun 18, 2019 at 4:52

2 Answers 2

1

1. Use Javascript filter method

filtered = myObject.filter(i => i.AttriValue.Ram === "4 Gb")

this way you can filter all products with 4GB ram

2. Iterate over myObject with for or while loop

filtered = []
for(let obj of myObject) {
  if(obj.AttriValue.RAM === '4 GB') filtered.push(obj)
}
Sign up to request clarification or add additional context in comments.

1 Comment

i have add image link. in this image i have a multiple check box i need unlimited filter for every attribute name with arrtibute value
0

You can use filter for that:

const myObject = [{
    "ProId": 12,
    "ProName": "Samsung Galaxy A9",
    "AttriValue": {
      "Front Camera": "16 MP and Above",
      "Internal Memory": "128 GB and Above",
      "Network Type": "4G",
      "Primary Camera": "16 MP and Above",
      "Ram": "6 GB"
    }
  },
  {
    "ProId": 11,
    "ProName": "Vivo Y95",
    "AttriValue": {
      "Front Camera": "16 MP and Above",
      "Internal Memory": "64 GB",
      "Network Type": "4G",
      "Primary Camera": "13 - 15.9 MP",
      "Ram": "4 GB"
    }
  },
]

const attrToFilter = {
  "Primary Camera": "13 - 15.9 MP",
  "Ram": "4 GB"
};

const filterFunction = (data, attrToFilter) =>
  data.filter(e =>
    Object.keys(attrToFilter)
    .map(i =>
      e.AttriValue[i] === attrToFilter[i]
    )
    .every(attr => !!attr)
  )

console.log(filterFunction(myObject, attrToFilter))


EDIT

I've updated my code for filtering with dynamic attributes.

You can set the attributes to be filtered with in: attrToFilter

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.