Filtering JSON data

后端 未结 8 458
暖寄归人
暖寄归人 2020-12-24 08:59

I have a JSON file which contains data like the following:

{\"posts\": [ 
    { \"title\":\"1\", \"url\":\"n1.png\" }, 
    { \"title\":\"2\", \"url\":\"n2.p         


        
相关标签:
8条回答
  • 2020-12-24 09:14

    There is another solution to this: using jLinq.js (documentation), which has even more features. In this case, you can get the solution by using the following code:

    var selectedPosts = jLinq.from(posts)
                             .betweenEquals("title",4,8)
                             .select();
    
    0 讨论(0)
  • 2020-12-24 09:20

    try this

    var q = new RegExp(req.query.q,'i');
    
    posts = posts.filter(function(item){
        if(item.title.match(q) || item.url.match(q)){
            return item;
        }
    });
    
    0 讨论(0)
  • 2020-12-24 09:21

    I use Linq JS on my current project and it works really well for filtering data.

    http://jslinq.codeplex.com/

    var posts = [ 
        { "title":"1", "url":"n1.png" }, 
        { "title":"2", "url":"n2.png" }, 
        { "title":"3", "url":"n3.png" }, 
        { "title":"4", "url":"n4.png" }, 
        { "title":"5", "url":"n5.png" }, 
        { "title":"6", "url":"n6.png" }, 
        { "title":"7", "url":"n7.png" }, 
        { "title":"8", "url":"n8.png" }, 
        { "title":"9", "url":"n9.png" }, 
        { "title":"10", "url":"n10.png" }
    ];
    
    var filteredPost = JSLINQ(posts)
                       .Where(function(item){ return item.title >= "textBox1Value" && item.title <= "textBox2Value"; });
    
    0 讨论(0)
  • 2020-12-24 09:24

    Why not do this?

    var json = JSON.parse('{"posts": [ 
    { "title":"1", "url":"n1.png" }, 
    { "title":"2", "url":"n2.png" }, 
    { "title":"3", "url":"n3.png" }, 
    { "title":"4", "url":"n4.png" }, 
    { "title":"5", "url":"n5.png" }, 
    { "title":"6", "url":"n6.png" }, 
    { "title":"7", "url":"n7.png" }, 
    { "title":"8", "url":"n8.png" }, 
    { "title":"9", "url":"n9.png" }, 
    { "title":"10", "url":"n10.png" }
    ]}');
    
    var filteredJson = json.posts.filter(function (row) {
      if(row.title matches your criteria) {
        return true
      } else {
        return false;
      }
    });
    

    Yes, its an ES5 method but that can be shimmed quite nicely

    0 讨论(0)
  • 2020-12-24 09:25

    Well i have this JSON array full of projects, each of project belong to a product:

    [
     {
        "id": 1,
        "parentProduct": {
          "id": 12,
          "productName": "Test 123"
        },
        "phase": "Phase 4",
        "productNumber": "111223",
        "projectName": "Test JPEG Apple",
        "supplier1": "de",
      },
      {
       "id": 2,
       "parentProduct": {
         "id": 12,
         "productName": "Test from me"
       },
       "phase": "222",
       "productNumber": "11122",
       "projectName": "Test PNG",
       "supplier1": "222"
       }
    ]
    

    I wanted to get only those with specific parent id, and i did it as below:

    filterByProductId(projects, productId) : any[] {
        var filteredArray = new Array;
        for(var k in projects) {
          if(projects[k].parentProduct.id == productId) {
            filteredArray.push(projects[k]);
          }
        }
     return filteredArray;
    }
    
    0 讨论(0)
  • 2020-12-24 09:29

    First once you have all the json data, you need to traverse them. For that,

             **$.each(data, function (i, data) {
                 if (data.title >= "textBox1Value" && item.title <= "textBox2Value")
                 //then the data;
             });**
    
    1. List item
    0 讨论(0)
提交回复
热议问题