1

Hi I am trying to filter my JSON response to only get those values which are >=0.

My response is below :

  {
    "response": [{
            "countryId": {
                "id": "10050020025",
                "idScheme": "externalId"
            },
            "processedDateTime": "2019-11-07 05:39:09",
            "AAE": [{
                "amount": "1000",
                "currencyCode": "USD"
            }],
            "TAE": [{
                "amount": "0",
                "currencyCode": "USD"
            }]
        },
        {
            "countryId": {
                "id": "10291520071",
                "idScheme": "InternalId"
            },
            "processedDateTime": "2019-02-03 08:21:22",
            "AAE": [{
                "amount": "0",
                "currencyCode": "USD"
            }],
            "TAE": [{
                "amount": "-2001",
                "currencyCode": "USD"
            }]
        }
    ]
}

I am trying to achieve :

{
"response": [{
        "countryId": {
            "id": "10050020025",
            "idScheme": "externalId"
        },
        "processedDateTime": "2019-11-07 05:39:09",
        "AAE": [{
            "amount": "1000",
            "currencyCode": "USD"
        }]
    },
    {
        "countryId": {
            "id": "10291520071",
            "idScheme": "InternalId"
            },
            "processedDateTime": "2019-02-03 08:21:22",
        }
    ]
}

I am trying to use map & filter from es6 to achieve this but not good with don't know how i can do that. so far i am trying some hardcoded approach as.

const outputResponse = response
.map(value => value)
.filter(value => value.AAE[0].amount !== '0') 

but it will remove whole object which don't have amount '0'.

Any help be appreciated.

3
  • .map(value => value) what's the purpose of writing this? it's like doing a=a. Commented Nov 12, 2019 at 9:50
  • i am learning es6 not know how to do it properly. Commented Nov 12, 2019 at 9:53
  • Ok, in that case you won't need to use filter using .map() is enough, but you should transform the mapped objects correctly. Commented Nov 12, 2019 at 10:04

1 Answer 1

1

You will just need an Array#map() method call to map all the response objects and remove the AAE and TAE or targeted keys values which has amount < 0:

let keys = ["AAE", "TAE"];

let result = data.response.map(val => {
  keys.forEach(key => {
    if (val[key] && val[key].some(x => +x.amount <= 0)) {
      delete val[key];
    }
  });
  return val;
});

Demo:

let keys = ["AAE", "TAE"];

let data = {
  "response": [{
      "countryId": {
        "id": "10050020025",
        "idScheme": "externalId"
      },
      "processedDateTime": "2019-11-07 05:39:09",
      "AAE": [{
        "amount": "1000",
        "currencyCode": "USD"
      }],
      "TAE": [{
        "amount": "0",
        "currencyCode": "USD"
      }]
    },
    {
      "countryId": {
        "id": "10291520071",
        "idScheme": "InternalId"
      },
      "processedDateTime": "2019-02-03 08:21:22",
      "AAE": [{
        "amount": "0",
        "currencyCode": "USD"
      }],
      "TAE": [{
        "amount": "-2001",
        "currencyCode": "USD"
      }]
    }
  ]
};

let result = data.response.map(val => {
  keys.forEach(key => {
    if (val[key] && val[key].some(x => +x.amount <= 0)) {
      delete val[key];
    }
  });
  return val;
});

console.log(result);

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

3 Comments

I have 20-30 keys as "AAE" & "TAE" how can i achieve without if condition writing each time.
@VirendersinghRathore You can put all these keys in an array and iterate over it to that, see my Edit.
thnx that was really helpful.

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.