我有以下数组:

    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>

最佳答案

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



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>

关于javascript - Js Linq:按2属性分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48182064/

10-09 19:35