1

How can I count the number of non-empty dailyemails in the following json?

{
   "templates":[
      {
         "id":"2c1d99d9b6b2fb417601d24c10c9b041a7d6f37b",
         "dailyemails":[
            "[email protected]",
            "[email protected]"
         ],
         "name":"Registration Report"             
      },
      {
         "id":"45s4dsf4qdgze5zef1z3e2f1zevcd5s6sdfsdfsdf",
         "dailyemails":[
         ],
         "name":"Presentation"             
      },
      {
         "id":"7d7cc642ca13cc4a998cad364dfe8e623fd95ae3",
         "dailyemails":[
            "[email protected]"
         ],
         "name":"Live Report"
      }

   ]
}

I am currently reading the total number of nodes Object.keys(myArr).length:

function metaInfo (id, cb){

    var URL = someURLhere
    var count = []

    fs.readFile(__dirname +'/' + URL+'/myFile.json', 'utf8', function   (err, data) {
        if (err) cb (err);
        obj = JSON.parse(data);
        var myArr = obj.nodes;
        count.push(Object.keys(myArr).length);
        cb(null, count)
    });
};

How can I read the number of non-empty dailyemails?

2
  • 1
    Probably to loop and count, but if your json is compressed, than you could look for and count "dailyemails":[] - it may speed up the process (method here). Commented Apr 26, 2017 at 15:03
  • Use map and filter, and then count the resulting length. Commented Apr 26, 2017 at 15:19

4 Answers 4

2

You can use reduce like this:

var count = myArray.reduce(function(c, o) {
    return c + (o.dailyemails.length? 1: 0);
}, 0);

even shorter using arrow functions:

var count = myArray.reduce((c, o) => c + (o.dailyemails.length? 1: 0), 0);
Sign up to request clarification or add additional context in comments.

10 Comments

You don't actually need a ternary for the short version, unary operator + expression in this case will always return a number, if you use !!. You could also use Math.min(1, o.dailyemails.length). Upvoted btw :)
yea but then we would be aggregating the lengths of the non empty arrays and not the number of non-empty arrays which is what we want
@axelduch The ternary is to check whether or not the array is empty. If I take it off then ther could be some undesired result (if the array length is 555 for example).
@ibrahimmahrir yup that's why I suggested either a cast to a boolean (either 0 or 1), same with min(1, 555)
@ibrahimmahrir my comment was directed towards axelduch, my apologies. But I see what you mean now.
|
2

filter all empty values out

var count = obj.templates.filter(item => item.dailyemails.length).length

Comments

2

You need to make use of the Array filter method.

var obj = {
   "templates":[
      {
         "id":"2c1d99d9b6b2fb417601d24c10c9b041a7d6f37b",
         "dailyemails":[
            "[email protected]",
            "[email protected]"
         ],
         "name":"Registration Report"             
      },
      {
         "id":"45s4dsf4qdgze5zef1z3e2f1zevcd5s6sdfsdfsdf",
         "dailyemails":[
         ],
         "name":"Presentation"             
      },
      {
         "id":"7d7cc642ca13cc4a998cad364dfe8e623fd95ae3",
         "dailyemails":[
            "[email protected]"
         ],
         "name":"Live Report"
      }

   ]
};

var nonEmptyDailyEmails = obj.templates.filter(x => x.dailyemails && x.dailyemails.length > 0);
var countOfnonEmptyDailyEmails = nonEmptyDailyEmails.length;
console.log(countOfnonEmptyDailyEmails);
   

Comments

2

Instead of creating an array with filter and then getting the length you can also use reduce. This doesn't have the same overhead as filter because the only thing you are storing is the counter in the temporary variable p.

var x = {
   "templates":[
      {
         "id":"2c1d99d9b6b2fb417601d24c10c9b041a7d6f37b",
         "dailyemails":[
            "[email protected]",
            "[email protected]"
         ],
         "name":"Registration Report"             
      },
      {
         "id":"45s4dsf4qdgze5zef1z3e2f1zevcd5s6sdfsdfsdf",
         "dailyemails":[
         ],
         "name":"Presentation"             
      },
      {
         "id":"7d7cc642ca13cc4a998cad364dfe8e623fd95ae3",
         "dailyemails":[
            "[email protected]"
         ],
         "name":"Live Report"
      }

   ]
}

alert(x["templates"].reduce(function(p, e)
              {
                if(e["dailyemails"].length !==0)
                    p+=1;
                return p;
              },0));

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.