0

我正在使用 Linq.js 从数组中的 JSON 对象中获取元素的子集。我有一组 JSON 对象,如下所示:

[
   {      
      "buy": "Buy",
      "type": "list",
      "total": 0,
      "product": [
         {
            "id": "12345",
            "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.",
            "description": "Lorem ipsum dolor sit amet, consectetuer...",
            "price": "9,95",
            "modelo": [
               {                 
                  "stock": "Y",
                  "color": "red"
               },
               {                  
                  "stock": "Y",
                  "color": "blue"
               }
            ],
            "Valor": 4.571429,
            "Comments": 7
         },
         {
            "id": "78945",
            "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.",
            "description": "Lorem ipsum dolor sit amet, consectetuer...",
            "price": "7,95",
            "modelo": [
               {
                  "stock": "Y",
                  "color": "green"
               },
               {
                  "stock": "Y",
                  "color": "red"
               }
            ],
            "Valor": 7.345256,
            "Comments": 12
         }
         
      ]
   }
]

我可以按价格过滤:

var result = Enumerable.From(data)  
    .Where("$.price > 2")
    .Select("$.price")  
    .ToArray();

但是我必须使用Linqs.js过滤并获取所有具有红色的id

感谢您的帮助。

4

1 回答 1

0

使用 LINQ:

var data = [ { "buy": "Buy", "type": "list", "total": 0, "product": [ { "id": "12345", "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.", "description": "Lorem ipsum dolor sit amet, consectetuer...", "price": "9,95", "modelo": [ { "stock": "Y", "color": "red" }, { "stock": "Y", "color": "blue" } ], "Valor": 4.571429, "Comments": 7 }, { "id": "78945", "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.", "description": "Lorem ipsum dolor sit amet, consectetuer...", "price": "7,95", "modelo": [ { "stock": "Y", "color": "green" }, { "stock": "Y", "color": "red" } ], "Valor": 7.345256, "Comments": 12 } ] } ],
    result = Enumerable.From(data[0].product)
        .Where("($.modelo).some(function (x) { return x.color === 'red'; })")
        .OrderBy("$.id")
        .Select("$.id")
        .ToArray();

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>

或者在纯 Javascript 中,以Array.prototype.filter()Array.prototype.some()为特色Array.prototype.map()

var data = [ { "buy": "Buy", "type": "list", "total": 0, "product": [ { "id": "12345", "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.", "description": "Lorem ipsum dolor sit amet, consectetuer...", "price": "9,95", "modelo": [ { "stock": "Y", "color": "red" }, { "stock": "Y", "color": "blue" } ], "Valor": 4.571429, "Comments": 7 }, { "id": "78945", "title": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.", "description": "Lorem ipsum dolor sit amet, consectetuer...", "price": "7,95", "modelo": [ { "stock": "Y", "color": "green" }, { "stock": "Y", "color": "red" } ], "Valor": 7.345256, "Comments": 12 } ] } ],
    result = data[0].product.filter(function (a) {
        return a.modelo.some(function (b) {
            return b.color === 'red';
        });
    }).map(function (a) { return a.id; });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

于 2015-11-16T16:30:46.037 回答