gpt4 book ai didi

javascript - Js linq : group by 2 properties

转载 作者:行者123 更新时间:2023-11-29 23:28:29 25 4
gpt4 key购买 nike

我有以下数组:

    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 : group by 2 properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com