0

I havent been able to find much on the subject and I know im doing this completely wrong but Im attempting to do something similar to the following:

    var position1 = raw_dates.indexOf(date1);
    var position2 = raw_dates.indexOf(date2);

    var i = 0;
    while(i<=(officers.length-1)){
        var tmp1 = 'array1_'+i;
        var tmp2 = 'array2_'+i;

        tmp1 = tmp1.slice(position1,position2);
        tmp2 = tmp2.slice(position1,position2);
        i++;
    }

    dates = raw_dates.filter(function(x){
        return x >= date1 && x <= date2;
    }); 

My goal here is to change the size of array1 and array2 for each officers. officers is an array and has values like officer_0, officer_1, etc. Each one of those has two arrays containing data relative to dates. Im trying to trim down my officer arrays to be in sync with my trimmed down dates array.

EDIT

dates = [20190101,20190102,20190103,20190104,20190105];

officers = ['joe', 'bob', 'bill', 'jeb'];

//joe
array1_0 = [1,2,3,4,5];
array2_0 = [2,4,6,8,10];

//bob
array1_1 = [1,2,3,4,5];
array2_1 = [2,4,6,8,10];

//bill
array1_2 = [1,2,3,4,5];
array2_2 = [2,4,6,8,10];

//jeb
array1_3 = [1,2,3,4,5];
array2_3 = [2,4,6,8,10];

//DESIRED OUTPUT IF DATE
//RANGE = 20190102-20190103
array1_X = [2,3];
array2_X = [4,6];
3
  • 3
    Can you provide a sample of the input data and what you're expecting the output to be? Commented Sep 27, 2019 at 17:48
  • @rayhatfield see edit please Commented Sep 27, 2019 at 17:54
  • Are the arrays linked to the officers in any way? It seems you're trying to concatenate two strings to form the name of the array, and then access that string as if it were your array with data. This doesn't work: it will instead take a slice from a string like array_1, which in your example would yield rr instead of the dates you're looking for. I think you need to re-format your data to make the arrays properties of officer objects. Alternatively, maybe this is of some help: stackoverflow.com/questions/12149233/… Commented Sep 27, 2019 at 18:56

1 Answer 1

0

It's still not clear to me what you're trying to do exactly, but your while loop doesn't really do anything. Here are a couple of observations:

This creates a string, like 'array1_0':

var tmp1 = 'array1_'+i;

Slicing "array1_0" returns a substring. For example, 'array1_0'.slice(2, 4) returns characters 3-4: 'ra'.

tmp1 = tmp1.slice(position1,position2);
// now tmp1 is a substring of 'array1_0', e.g. 'ar' or 'ra' or 'ay1'

But none of that matters because nothing is ever done with tmp1 or tmp2. The variables are declared in the while loop, reassigned to a substring, and then discarded at the end of each iteration.


I'm speculating a bit here because, as I said, it's still not clear to me what you're trying to do, but would your purpose be better served by an object instead of a bunch of arrays? Something like:

const officers = {
  joe: {
    startDate: '2019-01-02',
    endDate: '2019-01-03'
  },
  bob: {
    startDate: '2019-01-02',
    endDate: '2019-01-03'
  },
}

Or an array of positions for each officer:

const officers = {
  joe: {
    positions: [ // array of position objects
      // each entry is an object with information about the position
      { title: 'grunt', startDate: '2019-01-01', endDate: '2019-01-02' },
      { title: 'rookie', startDate: '2019-01-02', endDate: '2019-01-03' },
      { title: 'captain', startDate: '2019-01-03', endDate: '2019-01-04' },
    ]
  },
  bob: {
    positions: [
      { title: 'grunt', startDate: '2019-01-01', endDate: '2019-01-02' },
      { title: 'rookie', startDate: '2019-01-02', endDate: '2019-01-03' },
    ]
  },
}

Or as an array of objects with officer name and officer positions entries. (Basically the same as above, but avoids name collisions if you have two officers named joe:

const officers = [
  {
    name: 'joe',
    positions: [
      { title: 'grunt', startDate: '2019-01-01', endDate: '2019-01-02' },
      { title: 'rookie', startDate: '2019-01-02', endDate: '2019-01-03' },
    ]
  },
  {
    name: 'bob',
    positions: [
      { title: 'grunt', startDate: '2019-01-01', endDate: '2019-01-02' },
      { title: 'rookie', startDate: '2019-01-02', endDate: '2019-01-03' },
      { title: 'captain', startDate: '2019-01-03', endDate: '2019-01-04' },
    ]
  },
]

Good data structures can clarify your code and your thinking.

Happy to update this if you can clarify your goal, elaborate on what problem you're trying to solve.

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

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.