dlimes
dlimes

Reputation: 105

Find index of array based on criteria within a nested array's object with underscore

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.

Upvotes: 0

Views: 212

Answers (3)

Niladri Basu
Niladri Basu

Reputation: 10614

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) : '';
});

Upvotes: 0

Vignesh Raja
Vignesh Raja

Reputation: 8751

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>

Upvotes: 1

tymeJV
tymeJV

Reputation: 104775

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

Upvotes: 0

Related Questions