0

I am using api which return me this response:-

{ 
 VJiQND:
  { 
   optin: 'double',
   is_default: 'no',
   from_email: '[email protected]',
   name: 'ding',
   reply_to_email: '[email protected]',
   created_on: '2015-05-18 11:32:37',
   from_name: 'Xa' 
 },
 VJj3KAS:
 {
 optin: 'double',
 is_default: 'yes',
 from_email: '[email protected]',
 name: 'xtusxx',
 reply_to_email: '[email protected]',
 created_on: '2015-05-18 09:09:43',
 from_name: 'Ads'
} 
}

now I want to get VJj3KAS if from_name equals to 'Ads' I try filter function but it didn't work it throw me an error can you please help me to get that value.

Thanks

3
  • not valid json - please use a jsonlint Commented May 18, 2015 at 11:52
  • Yes I know not valid json but this is what web api returns. Commented May 18, 2015 at 11:53
  • What is your code so far? If you ask why something isn't working you'll have to show the code that isn't working. Commented May 18, 2015 at 11:54

1 Answer 1

1

The filter function works for arrays and what you have there is a map. You must simply iterate through the map key/value pairs and check if they match your condition.

function getFilteredKeys(map, filter) {
  var filteredKeys = [];
  for (var key in map) {
    if (filter(key, map[key])) {
      filteredKeys.push(key);
    }
  }
  return filteredKeys;
}

Usage:

var keys = getFilteredKeys(apiResult, function(key, value){
  return value.from_name && value.from_name === 'Ads';
});
Sign up to request clarification or add additional context in comments.

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.