0

I am developing an applicaiton where I am getting the following JSON data from the server:

var requests =     
    {
    "id":"12","start":"2016-05-20 07:00:00",
    "end":"2016-05-20
    10:00:00",
    "price":"0",
    "paid":"no",
    "accepted":"yes",
    "reviewed_by_provider":"no",
    "reviewed_by_client":"no",
    "created_by_user_id":"35",
    "provided_to_user_id":"36"
    },
    {"id":"13",
    "start":"2016-05-20 07:30:00",
    "end":"2016-05-20 11:00:00",
    "price":"0",
    "paid":"no",
    "accepted":"yes",
    "reviewed_by_provider":"no",
    "reviewed_by_client":"no",
    "created_by_user_id":"35",
    "provided_to_user_id":"36"
    },
    {
    "id":"14",
    "start":"2016-05-20 07:30:00",
    "end":"2016-05-20 10:00:00",
    "price":"0",
    "paid":"no",
    "accepted":"no",
    "reviewed_by_provider":"no",
    "reviewed_by_client":"no",
    "created_by_user_id":"35",
    "provided_to_user_id":"36"
    }

In JavaScript by running a for each loop, I want to eliminate the sets where "accepted":"no" and then from the remaining sets, I want to delete all the other parameters except id, end and start.

How do I do this in JavaScript? Note that I am developing this application in angularJS.

Any ideas?

Dasmond Miles
  • 183
  • 2
  • 10
  • That's not JSON. JSON is a *textual notation* for data exchange. If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 27 '16 at 12:46
  • If you are willing to use a external lib for this, lodash is really nice for exactly what you are trying to do. See: http://stackoverflow.com/questions/21281286/how-can-i-remove-an-element-from-a-list-with-lodash – Wagner Danda da Silva Filho May 27 '16 at 12:51
  • Oh sorry, I miss wrote. Its without the [] signs – Dasmond Miles May 27 '16 at 12:55
  • Since I can't actually provide an answer (post was marked duplicate) I'll just say it here. This is how you'd what ur asking in lodash: First: _.remove(requests, { accepted: 'no' }); Then I'd follow to what the link suggested as duplicated. – Wagner Danda da Silva Filho May 27 '16 at 12:57
  • @wdanda thank you for this information. I have managed to filter the request for accepted == no but how do I remove other parameters such as paid, price,etc? – Dasmond Miles May 27 '16 at 13:01
  • Use lodash omit. See more here: https://lodash.com/docs#omit Just keep in mind this one actually returns new object that you need to reassign. – Wagner Danda da Silva Filho May 27 '16 at 13:20

0 Answers0