与linqJS的内联 [英] Inner join with linqJS

查看:75
本文介绍了与linqJS的内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个dateRange,它是一个日期数组.

I create a dateRange wich is an array of dates.

然后我有一组日期数字,例如0代表星期日,1代表星期一,等等...

Then I have an array of day numbers like 0 for Sunday, 1 for Monday etc...

现在,我想根据visibleWeekDays数组获取所有dateRange日期.

Now I want to get all dateRange dates according to the visibleWeekDays array.

解决方案位于getVisibleDateRange函数中.

The solution is in the getVisibleDateRange function.

但是我想用LINQ来做,因为为什么要重新发明轮子...

But I want to do it with LINQ because why reinvent the wheel...

内部或外部选择器仍然需要.day(),因为选择器之一是momentJS对象.

The inner or outer selector would still need a .day() because one of the selector is a momentJS object.

但是要获得星期几,我需要将".day()"放入无法使用的linqJS字符串中...

But to get the day of week I would need to put the ".day()" into the linqJS string which can not work...

使用linqJS的解决方案是什么?

What would be your solution with linqJS ?

// Arrange
var startDate = moment(new Date(2014, 1, 1));
var endDate = moment(new Date(2014, 1, 15));
var visibleWeekDays = [0,1]

// Act
var dates = dateFactory.dateRange(startDate, endDate);

var visibleDays = dateFactory.getVisibleDateRange(visibleWeekDays ,dates);



 function getVisibleDateRange(visibleWeekDays, dateRange) {
        var visibleDateRange = [];
        for (var i = 0; i < dateRange.length; i++) {
            for (var j = 0; j < visibleWeekDays.length; j++) {
                var currentDate = dateRange[i];
                var dayOfWeek = currentDate.day();
                var visibleDayOfWeek = visibleWeekDays[j];
                if (visibleDayOfWeek === dayOfWeek) {
                    visibleDateRange.push(currentDate);
                }
            }
        }        
        return visibleDateRange;
    }

    var visibleDateRange = Enumerable.from(visibleWeekDays).join(dateRange,"","","outer,inner=>outer + ':' + inner")

推荐答案

这是我编写内部联接的方法:

Here's how I would write the inner join:

var dateRange = Enumerable.Range(1, 15).Select("new Date(2014, 1, $)");
var visibleWeekDays = Enumerable.From([0, 1]);

var visibleDateRange = dateRange.Join(visibleWeekDays,
    "$.getDay()", // outer selector
    "$",          // inner selector
    "$")          // result selector (select outer value, the date)
    .ToArray();

在这里,我使用了更紧凑的语法来定义lambda.基本上,lambda通常最多具有4个参数.因此,您只需在标识符中添加其他$即可引用第n个参数.因此$引用第一个参数,$$引用第二个参数,等等.

Here, I've used a more compact syntax for defining lambdas. Basically lambdas will generally have at most 4 paramters. So you can reference the nth parameter simply by adding additional $'s in the identifier. So $ refers to the first parameter, $$ the second, etc.

联接的参数与您在LINQ中进行的调用完全相同.第一个参数是内部集合,然后是外部选择器,内部选择器和结果选择器.

The parameters of the join is exactly the same as in the call you would make in LINQ. The first parameter is the inner collection, then the outer selector, inner selector, and result selector.

由于外部集合是日期,因此您可以访问相应的项目及其属性.由于我们要获得调用date.getDay()的结果,因此只需在对象(第一个参数)上调用getDay().

Since the outer collection is the dates, you have access the corresponding item and its properties. Since we want to get the result of calling date.getDay(), you simply call getDay() on the object (the first parameter).

这篇关于与linqJS的内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆