1

I have two javascript arrays

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015", ...]

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", ...]

How do I count how many times openDates occurs in entries in the format..

var entriesPerDay = [0, 0, 10, 2, 16, 18, 20, ...]

I have lodash installed but can't figure it out. I need to return the 0 value if there is no match.

1
  • Take an item in openDates, compare it with each item in entries, increase the count if matched. Is there any difficulties in it? Commented Oct 1, 2015 at 2:04

5 Answers 5

1

What I can think as of now.

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"]

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"]

var entriesPerDay = [];

for(var i = 0; i < openDates.length; i++) {
 var currDate = openDates[i];
 var temp = _.filter(entries, function(date) {
   return date === currDate;   
 });
 entriesPerDay.push(temp.length);
}

for above example the entriesPerDay is this: [0, 0, 2, 0, 42, 0]

PS: I am using lodash, as you said in question that you have lodash.

PPS: Do accept and upvote if it worked.

Sign up to request clarification or add additional context in comments.

2 Comments

@Jesse Why are you saying so? I din't get what did you mean by that.. But why deleting your answer? It's just that I have used lodash and you did it without any library. Isn't it good that one can know it in both ways?
This one works for me, thank you both for your answers. I up voted both - just need more reputation points for them to be visible.
1

This example below will help you get started and getting the idea on how this can be accomplished.

In this example forEach is being used on the arrays to compare the values.

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"]
var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"]

function countEntries(arr1, arr2) {
    var count, total = [];
    arr1.forEach(function(date) {
        count = 0;
        arr2.forEach(function(entry) {
            count += date == entry ? 1 : 0;
        });
        total.push(count);
    });
    return total;
}

console.log(countEntries(openDates, entries));

Here we are simply running a forEach loop and resetting our count on each iteration. See the output below :

Output: [0, 0, 2, 0, 42, 0]

Comments

0

For a O(n) solution instead of (On^2):

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"];
var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"];

function countEntries(open, existing) {
  var openHash = {};
  var entriesHash = {};

  for(var i = 0; i < open.length; i ++){
    openHash[open[i]] = 0;
  }

  for(i = 0; i < existing.length; i ++){
    if(entriesHash.hasOwnProperty(entries[i])){
      entriesHash[entries[i]] ++;
    }else{
      entriesHash[entries[i]] = 1;
    }    
  }

  var result = [];
  for(var date in openHash){
    if(openHash.hasOwnProperty(date)){
      if(entriesHash[date]){
        openHash[date] = entriesHash[date];
      }
      result.push(openHash[date]);
    }
  }

  return result;
}

console.log(countEntries(openDates, entries));

http://jsbin.com/jinolapuza/edit?js,console

Comments

0

You can do it using lodash's .countBy() and .at(). You can use _.pick() instead of _.at() to get an object with the found entries (at least 1 occurrence) as property names.

Complexity - O(n).

var openDates = ["6/14/2015", "6/15/2015", "6/16/2015", "6/17/2015", "6/18/2015", "6/19/2015"];

var entries = ["6/16/2015", "6/16/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015", "6/18/2015"];

var counts = _(entries)
            .countBy(function(entry) { return entry }) // count the number of occurances of each entry
            .at(openDates) // get the values of the keys that appear in openDates
            .map(function(count) { return count || 0 }) // map undefined (not found) t0 0
            .value(); // get the values

var countsObject = _(entries)
            .countBy(function(entry) { return entry }) // count the number of occurances of each entry
            .pick(openDates) // get the values of the keys that appear in openDates
            .value(); // get the values

document.write('_.at(): ' + JSON.stringify(counts));
document.write('<br />');
document.write('_.pick(): ' + JSON.stringify(countsObject));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>

Comments

-1

Using map() and filter():

_.map(openDates, function(item) {
    return _.filter(entries, function(entry) {
        return item === entry;
    }).length;
});

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.