0

I have the following array:

route: [
  {
    flyFrom: "CDG",
    flyTo: "DUB",
    return: 0,
  },
  {
    flyFrom: "DUB",
    flyTo: "SXF",
    return: 0,
  },
  {
    flyFrom: "SFX",
    flyTo: "CDG",
    return: 1,
  }
]

I need to count how many times there is return: 0 and how many times there is return: 1.

The end result would look like:

for the cases where return: 0 appears 2 times --- 1 Stop

for the cases where return: 1 appears 1 time --- Non-stop

5
  • 1
    do you have some code to show? Commented Mar 30, 2016 at 20:56
  • Tried this but doesn`t work: 'var stops_count = route.length; $.each(function(route.length) { stops_count[route.length] = (stops_count[route.length] || 0)+1; });' Commented Mar 30, 2016 at 20:57
  • 1
    return is a reserved word (also a keyword) in javascript and thus cannot be used as an object's key. Commented Mar 30, 2016 at 20:58
  • 3
    @ThomasShields Reserved words and keywords are allowed as object's keys in Javascript. Commented Mar 30, 2016 at 20:58
  • @Paulpro whoops, forgot that was fixed in ES5! :) Commented Mar 30, 2016 at 21:00

5 Answers 5

3

You can filter 0 and 1 values with Array.prototype.filter() and check the length of result (ES6 syntax):

var route = [
    { flyFrom: 'CDG', flyTo: 'DUB', return : 0, },
    { flyFrom: 'DUB', flyTo: 'SXF', return : 0, },
    { flyFrom: 'SFX', flyTo: 'CDG', return : 1, }
];

var zeros = route.filter(r => r.return === 0).length;
var ones = route.filter(r => r.return === 1).length;

ES5 analogy:

var zeros = route.filter(function(r) {
    return r.return === 0;
}).length;

Another solution would be to reduce array:

var zeros = route.reduce((a, b) => a + (b.return === 0), 0);
Sign up to request clarification or add additional context in comments.

4 Comments

Is the same solution without arrows and adding function and return
I wonder when the community accept solutions without ES5 alternatives
filter seems inefficient as it creates a new array just to count the length. Consider reduce instead.
@RobG thanks for pointing that out. I've added it to the answer
0

Quick ES 5 solution:

var zeros = 0, ones = 0;

route.forEach(function(entry) {
    if(entry.return == 0) zeros++;
    else if(entry.return == 1) ones++;
});

alert("Zeros: " + zeros + " | Ones: " + ones);

Comments

0

create a json object. then select what child you want. for example:

var jsonObject = data {
route: [
{
flyFrom: "CDG",
flyTo: "DUB",
return: 0,
},
{
flyFrom: "DUB",
flyTo: "SXF",
return: 0,
},
{
flyFrom: "SFX",
flyTo: "CDG",
return: 1,
}]
}

var zeroCount = 0;
var oneCount = 0;
for(var i = 0; i < jsonObject.route.length; i++){
var j = jsonObject.route[i].return;
if(j == 0) zeroCount++;
else oneCount++;
}

1 Comment

What is a "json object"?
0

This function will return an object with two keys, outbound and inbound, containing the required messages, e.g. "non-stop", "1 stop", etc.:

function countStops(route) { 
   const legs = route.reduce((result, r) => {
        const leg = r['return'] ? 'inbound' : 'outbound';
        result[leg]++;
        return result;
    }, { outbound: 0, inbound: 0 });

    const message = parts => {
        switch (parts) {
            case 1:
                return 'non-stop';
            case 2:
                return '1 stop';
            default:
                return `${parts - 1} stops`;
        }
    };

    return {
        outbound: message(legs.outbound),
        inbound: message(legs.inbound)
    };
}

See fiddle here.

Comments

0

You can iterate over and count the returns in an object with a single loop.

var data = { route: [{ flyFrom: "CDG", flyTo: "DUB", 'return': 0, }, { flyFrom: "DUB", flyTo: "SXF", 'return': 0, }, { flyFrom: "SFX", flyTo: "CDG", 'return': 1 }] },
    count = {};

data.route.forEach(function (a) {
    count[a.return] = (count[a.return] || 0) + 1;
});

Object.keys(count).forEach(function (k) {
    document.getElementById('id' + k).innerHTML = count[k];
});
<p>for the cases where return: 0 appears <span id="id0">0</span> times --- 1 Stop</p>
<p>for the cases where return: 1 appears <span id="id1">0</span> time --- Non-stop</p>

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.