linq,js GroupBy坐标(lat,lng)

时间:2017-09-11 13:27:50

标签: javascript arrays linq.js

我有以下数组:

var coord = [
{flat:"7", house:"14", indication1: "60", lat: "47.93896450", lng:"33.43282100"},
{flat:"9", house:"18", indication1: "65", lat: "47.87736800", lng:"33.27488850"},
{flat:"10", house:"14", indication1: "24", lat: "47.93896450", lng:"33.43282100"},
{flat:"11", house:"14", indication1: "11", lat: "47.93896450", lng:"33.43282100"},
{flat:"12", house:"14", indication1: "56", lat: "47.93896450", lng:"33.43282100"}];

我想使用linq.js将这些数组分为两个字段:latlng

var h = Enumerable.From(data).GroupBy(
            "{lat: $.lat, lng: $.lng}",
            "{ House: $.house, Flat: $.flat }",
            "{ Qlat: $.lat, Qlng: $.lng, info: $$.ToArray()}").ToArray();

console.log(h);

我希望看到2组,但得到了所有5.我错在哪里?

1 个答案:

答案 0 :(得分:0)

您使用对象进行分组。每个新生成的对象都是一个新对象,不等于最后一个或任何其他对象。

您可以将null作为第一个参数,将带有分组信息的字符串作为第四个参数。



var coord = [{ flat: "7", house: "14", indication1: "60", lat: "47.93896450", lng: "33.43282100" }, { flat: "9", house: "14", indication1: "65", lat: "47.87736800", lng: "33.27488850" }, { flat: "10", house: "14", indication1: "24", lat: "47.93896450", lng: "33.43282100" }, { flat: "11", house: "14", indication1: "11", lat: "47.93896450", lng: "33.43282100" }, { flat: "12", house: "14", indication1: "56", lat: "47.93896450", lng: "33.43282100" }],
    grouped = Enumerable.From(coord).GroupBy(
        null,
        "{ House: $.house, Flat: $.flat }",
        "{ Qlat: $.lat, Qlng: $.lng, info: $$.ToArray()}",
        "$.lat + '|' + $.lng"
    ).ToArray();

console.log(grouped);

.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>
&#13;
&#13;
&#13;