0
\$\begingroup\$

I have an array of API objects, below is an example of one object:

{
 description: "Get a list of most recent social alerts"
 example_input: ""
 example_output: "status"
 id: 19
 method: "GET"
 resource: "/api/alerts"
}

With my code below, I'm able to loop through the big Array, then filter out items into 1 of 4 arrays (for GET, POST, PUT and DELETE)

RestFactory.getREST().then(function(res) {
 filterArrays(res.data.rest_methods);
});
function filterArrays(data) {
 for (var i=0; i<data.length; i++) {
 if (data[i].method === 'GET') {
 getArray.push(data[i]);
 }
 else if (data[i].method === 'POST') {
 postArray.push(data[i]);
 }
 else if (data[i].method === 'PUT') {
 putArray.push(data[i]);
 }
 else if (data[i].method === 'DELETE') {
 deleteArray.push(data[i]);
 }
 }
}

I feel that this is not the most efficient way to accomplish this, how should this be refactored? Would you recommend lodash in this scenario?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Mar 9, 2016 at 20:40
\$\endgroup\$
1
  • \$\begingroup\$ You are basically looking for groupBy, like groupBy(data, x => x.method) \$\endgroup\$ Commented Mar 9, 2016 at 20:46

2 Answers 2

4
\$\begingroup\$

This can easily be done with array.reduce

let resultsByMethod = data.reduce((results, result) => {
 if(!results[result.method]) results[result.method] = [];
 results[result.method].push(result);
 return results;
}, {});

The end result is something like:

{
 GET: [...],
 POST: [...],
 PUT: [...],
 DELETE: [...]
}

One disadvantage in your approach is when it encounters a method that's note one of the 4 you're comparing.

answered Mar 9, 2016 at 20:58
\$\endgroup\$
0
0
\$\begingroup\$

Ended up with this, using _.each and a switch case:

function filterColumns(apis) {
 _.each(apis, function(api) {
 switch (api.method) {
 case 'GET' : getArray.push(api); break;
 case 'POST' : postArray.push(api); break;
 case 'PUT' : putArray.push(api); break;
 case 'DELETE' : deleteArray.push(api); break;
 }
 });
}

With the original for loop, each item had the possibility of being checked a maximum of 4 times.

Using _.each simplified the loop, making it much more readable.

Next using a switch case solves the problem of if statements having to be checked for multiple cases. The correct case will pass immediately using the switch case.

answered Mar 9, 2016 at 21:39
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.