1

I have an array in which i want to compare the object 'defaultvalue' and 'selectedvalue' in type script if there is a value in object 'higherOptions' using typescript.

let coverageArray = [
    {   
        "id":1, 
        "defaultValue": 100000,
        "selectedValue": 100000,
        "higherOptions": []
    },
    {
        "id":2,
        "defaultValue": 300000,
        "selectedValue": 300000,
         "higherOptions": [150000, 300000]
    },
    {
        "id":3, 
        "defaultValue": 500,
        "selectedValue": 500,
        "higherOptions": [500, 1000]
    },
    {
        "id":4, 
        "defaultValue": "ALS (12 months of restroration)",
        "selectedValue": "ALS (12 months of restroration)",
        "higherOptions": []
    },
    {
        "id":5,
        "defaultValue": 15000,
        "selectedValue": 15000,
        "higherOptions": [ 15000, 20000, 25000, 30000, 35000 ]
     }];

2 Answers 2

1

Use the following code:

let filteredCoverageArray = coverageArray
      .filter( coverage => coverage.higherOptions.length
                           && coverage.selectedValue === coverage.defaultValue);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch man. I am new to typescript and this really helped me :)
1

Rodrigo's answer is good but it's missing to check if the higherOptions array has a value.

Code:

let filteredCoverageArray = coverageArray
              .filter( coverage => coverage.higherOptions.length > 0
                                   && coverage.selectedValue === coverage.defaultValue);

Result:

 [  
   {  
      "id":2,
      "defaultValue":300000,
      "selectedValue":300000,
      "higherOptions":[  
         150000,
         300000
      ]
   },
   {  
      "id":3,
      "defaultValue":500,
      "selectedValue":500,
      "higherOptions":[  
         500,
         1000
      ]
   },
   {  
      "id":5,
      "defaultValue":15000,
      "selectedValue":15000,
      "higherOptions":[  
         15000,
         20000,
         25000,
         30000,
         35000
      ]
   }
]

1 Comment

yes thanks for writing but i figured that part by myself :)

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.