根据带下划线的嵌套数组对象中的条件查找数组索引
Find index of array based on criteria within a nested array's object with underscore
所以,我有这些数据,假设我正在尝试查找包含特定日期的数组的索引(比如“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
];
在我的 arr
数组中,我有另一组数组 - 每个数组都有一个特定日期的事件。我的目标是找到具有特定日期数组的数组索引。以下是我目前拥有的,但我从错误的数组中获取索引(我认为)。
var date = '2018-01-03';
var currentIndex = _.findIndex(arr, function(obj) {
return obj[0].start == date ;
}); //currentIndex should equal 2
我觉得我启动正确,但也许我还需要映射一些东西?
编辑
我没有使用 ES6,所以我认为箭头功能不适合我。
您正在寻找类似这样的东西,也许使用 Vanilla JavaScript 的 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)));
ES6 之前的版本:
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) : '';
});
如果 findIndex
和 Array.some
用于内部数组,则使用组合:
let availableIndex = arr.findIndex(a => a.some(b => b.start === date)); //2 for your example
由于您正在使用 moment
,isSame
可用于检查相同的日期。
注意: Firefox 不支持 RFC2822 或 ISO 格式以外的日期格式,因此需要暂时给出格式。
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>
所以,我有这些数据,假设我正在尝试查找包含特定日期的数组的索引(比如“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
];
在我的 arr
数组中,我有另一组数组 - 每个数组都有一个特定日期的事件。我的目标是找到具有特定日期数组的数组索引。以下是我目前拥有的,但我从错误的数组中获取索引(我认为)。
var date = '2018-01-03';
var currentIndex = _.findIndex(arr, function(obj) {
return obj[0].start == date ;
}); //currentIndex should equal 2
我觉得我启动正确,但也许我还需要映射一些东西?
编辑 我没有使用 ES6,所以我认为箭头功能不适合我。
您正在寻找类似这样的东西,也许使用 Vanilla JavaScript 的 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)));
ES6 之前的版本:
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) : '';
});
如果 findIndex
和 Array.some
用于内部数组,则使用组合:
let availableIndex = arr.findIndex(a => a.some(b => b.start === date)); //2 for your example
由于您正在使用 moment
,isSame
可用于检查相同的日期。
注意: Firefox 不支持 RFC2822 或 ISO 格式以外的日期格式,因此需要暂时给出格式。
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>