Js linq:按 2 个属性分组

Js linq: group by 2 properties

我有以下数组:

    var data = [
  {street: "Галенка", house: "11", flats: "10,11,12,13,17,18,21,24,27,29,3,30,32,33,36,4,40", isNotApply: false},
  {street: "Галенка", house: "3", flats: "1,12,14,15,16,19,2,21,23,24,28,29,3,30,31,32,6", isNotApply: true},
  {street: "Літке", house: "110", flats: "0", isNotApply: true},
  {street: "Вадима Гурова", house: "11", flats: "10,100,101,102,104,105", isNotApply: false},
  {street: "Вадима Гурова", house: "120", flats: "0", isNotApply: true}
];

我使用 GroupBy 得到了这个结果,这很好,但是我如何将 IsNotApply 设置为这 2 个数组的键?

var data = [
  {street: "Галенка", house: "11", flats: "10,11,12,13,17,18,21,24,27,29,3,30,32,33,36,4,40,4…6,58,61,64,65,67,68,69,7,70,71,73,75,76,78,8,80,9", isNotApply: false},
  {street: "Галенка", house: "3", flats: "1,12,14,15,16,19,2,21,23,24,28,29,3,30,31,32,6", isNotApply: true},
  {street: "Літке", house: "110", flats: "0", isNotApply: true},
  {street: "Вадима Гурова", house: "11", flats: "10,100,101,102,104,105,106,107,109,110,111,112,113…73,75,76,79,8,81,82,83,85,86,88,90,91,94,95,96,99", isNotApply: false},
  {street: "Вадима Гурова", house: "120", flats: "0", isNotApply: true}
];

result = Enumerable.From(data)
        .GroupBy(
            "{IsNotApply: $.isNotApply, House: $.house}",
            "{Street: $.street, House: $.house, Flats: $.flats}",
            "{ data: $$.ToArray(), isNotApply: $.IsNotApply }",
            "$.IsNotApply"
        )
        .ToArray();

console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

JSLinq 添加了片段。

var data = [
  {street: "Галенка", house: "11", flats: "10,11,12,13,17,18,21,24,27,29,3,30,32,33,36,4,40,4…6,58,61,64,65,67,68,69,7,70,71,73,75,76,78,8,80,9", isNotApply: false},
  {street: "Галенка", house: "3", flats: "1,12,14,15,16,19,2,21,23,24,28,29,3,30,31,32,6", isNotApply: true},
  {street: "Літке", house: "110", flats: "0", isNotApply: true},
  {street: "Вадима Гурова", house: "11", flats: "10,100,101,102,104,105,106,107,109,110,111,112,113…73,75,76,79,8,81,82,83,85,86,88,90,91,94,95,96,99", isNotApply: false},
  {street: "Вадима Гурова", house: "120", flats: "0", isNotApply: true}
];

console.log(data);

//Javascript
var result = {};        
data.forEach(function(item){
    item = JSON.parse(JSON.stringify(item));
    var bool = item.isNotApply;
    delete item.isNotApply;
    result[bool] = result[bool] || {data:[],isNotApply:bool};
    result[bool].data.push(item);
})

console.log("Javascript result : ",result);

//Linq
result = Enumerable.From(data)
        .GroupBy(
            "{IsNotApply: $.isNotApply, House: $.house}",
            "{Street: $.street, House: $.house, Flats: $.flats}",
            "{ data: $$.ToArray(), isNotApply: $.IsNotApply }",
            "$.IsNotApply"
        )
        .ToObject("$.isNotApply");
        
console.log("Linq JS result : ",result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

您可以添加一个函数来获取零件结果对象所需的键。

var data = [{ street: "Галенка", house: "11", flats: "10,11,12,13,17,18,21,24,27,29,3,30,32,33,36,4,40,4…6,58,61,64,65,67,68,69,7,70,71,73,75,76,78,8,80,9", isNotApply: false }, { street: "Галенка", house: "3", flats: "1,12,14,15,16,19,2,21,23,24,28,29,3,30,31,32,6", isNotApply: true }, { street: "Літке", house: "110", flats: "0", isNotApply: true }, { street: "Вадима Гурова", house: "11", flats: "10,100,101,102,104,105,106,107,109,110,111,112,113…73,75,76,79,8,81,82,83,85,86,88,90,91,94,95,96,99", isNotApply: false }, { street: "Вадима Гурова", house: "120", flats: "0", isNotApply: true }],
    result = Enumerable
        .From(data)
        .GroupBy(
            "$.isNotApply",
            "{ Street: $.street, House: $.house, Flats: $.flats }",
            "(k, v) => ({ [k]: v.ToArray() })"
        )
        .ToArray();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>