0

So, I have this data, Let's say I'm trying to find the index of the array that contains a specific date (let's say the '2018-01-03')

var arr = [
   [{ id: 'A1', start: '2018-01-01' }, { id: 'A2', start: '2018-01-01' }], // 0
   [{ id: 'B1', start: '2018-01-02' }, { id: 'B2', start: '2018-01-02' }], // 1
   [{ id: 'C1', start: '2018-01-03' }, { id: 'C2', start: '2018-01-03' }] // 2 <<<Want this index which should be 2     
];

Inside my arr array, I have another set of arrays - each array has events for one specific date. My goal is to find the index of the array that has the array of a specific date. Below is what I have currently, but I'm getting the index from the incorrect array (I think).

var date = '2018-01-03';

var currentIndex = _.findIndex(arr, function(obj) {
    return obj[0].start == date ;
});  //currentIndex should equal 2

I feel like I'm initiating it correctly, but maybe I need to map something as well?

EDIT I am not using ES6, so I don't think the arrow functionality will work for me.

3
  • You would have to nest another level of iteration, to search through every subarray. Commented Jul 12, 2018 at 16:15
  • 3
    Why the moment(…).date() wrapper? Does that actually return the right thing to be compared to your string? Commented Jul 12, 2018 at 16:16
  • @Bergi - You are correct, the string above was only for an example. Not needed and wouldn't work any ways. I'll edit real quick. Commented Jul 12, 2018 at 16:32

3 Answers 3

1

As you are using moment, isSame can be used to check same dates.

Note: The format need to be given in the moment as Firefox doesn't support date formats other than RFC2822 or ISO formats.

var arr = [
   [{ id: 'A1', start: '2018-01-01' }, { id: 'A2', start: '2018-01-01' }], // 0
   [{ id: 'B1', start: '2018-01-02' }, { id: 'B2', start: '2018-01-02' }], // 1
   [{ id: 'C1', start: '2018-01-03' }, { id: 'C2', start: '2018-01-03' }] // 2 <<<Want this index which should be 2     
];

function result(date)
{
    return arr.findIndex(function(value){
        return value.find(function(val){
            return moment(val.start,"YYYY-MM-DD").isSame(moment(date,"YYYY-MM-DD"));
         });
    });
}
console.log(result('2018-01-02'));
console.log(result('2018-01-01'));
console.log(result('2018-01-03'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

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

3 Comments

This is returning the array, I need the index of the specified array.
@dlimes Updated the answer. Hope it helps.
Thank you sir! Not the prettiest, but it will suffice, much appreciated!
0

Use a combo if findIndex and Array.some for the inner array:

let availableIndex = arr.findIndex(a => a.some(b => b.start === date)); //2 for your example

Comments

0

You are looking for something like this perhaps using Vanilla JavaScript's array#findIndex and array#some:

var arrN = [
   [{ id: 'A1', start: '2018-01-01' }, { id: 'A2', start: '2018-01-01' }], // 0
   [{ id: 'B1', start: '2018-01-02' }, { id: 'B2', start: '2018-01-02' }], // 1
   [{ id: 'C1', start: '2018-01-03' }, { id: 'C2', start: '2018-01-03' }] // 2 <<<Want this index which should be 2     
];

var date = '2018-01-03';

// if each element of sub-array has same date
console.log('index of '+ date + " is --> " + arrN.findIndex(e => e[0].start == date));


// if each element of sub-array do not have same date
console.log(arrN.findIndex(e => e.some(obj => obj.start == date)));   

Pre-ES6 version:

var arrN = [
   [{ id: 'A1', start: '2018-01-01' }, { id: 'A2', start: '2018-01-01' }], // 0
   [{ id: 'B1', start: '2018-01-02' }, { id: 'B2', start: '2018-01-02' }], // 1
   [{ id: 'C1', start: '2018-01-03' }, { id: 'C2', start: '2018-01-03' }] // 2 <<<Want this index which should be 2     
];

var date = '2018-01-03';

//  if each element of sub-array do not have same date
arrN.forEach(function(element, index) {
  element.some(function(obj){
      return obj.start == date
  }) ? console.log(index) : '';
});

1 Comment

@dlimes You should be able to trivially rewrite that to normal function expressions though

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.