2010-01-18 27 views
13

Tôi có một mảng JavaScript như dưới đây mà tôi cần phải lọc để có được các giá trị con chính xác từ dữ liệu thử nghiệm bên dưới.Lọc các mục trong JavaScript Array bằng cách sử dụng jQuery

var arrChildOptions2 = [ 
     {Parent:'opt1',Value:'opt1',Text:'Parent1 - Child 1'}, 
     {Parent:'opt2',Value:'opt1',Text:'Parent 2 - Child 1'}, 
     {Parent:'opt2',Value:'opt2',Text:'Parent 2 - Child 2'} 
    ]; 

Giá trị được sử dụng để điền danh sách thả xuống dựa trên sự kiện thay đổi của menu thả xuống gốc như dưới đây.

$(function() { 
    $('#ddl1').change(function() { 
     $('#ddl2 option:gt(0)').remove(); 
     $('#ddl2').addItems('#ddl2', arrChildOptions2[Parent=opt2]); 
    }); 
}); 

trong đó additems là hàm lặp qua mảng. Vấn đề là tôi không thể lấy nó để lọc bởi cha mẹ, tôi đã thử sử dụng chứa và trên arrChildOptions2 [Parent = opt2] nhưng tôi không thể lọc, tôi muốn tìm một giải pháp thay vì sử dụng vòng lặp for? Bất kỳ ý tưởng nào, hãy cổ vũ

+0

[ 'array.filter()'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Blazemonger

Trả lời

30

Bạn có thể có nhiều may mắn hơn bằng cách sử dụng hàm jQuery.grep() thay vì lộn xộn xung quanh bằng các vòng lặp.

Chức năng này "Tìm các phần tử của mảng đáp ứng chức năng bộ lọc. Mảng gốc không bị ảnh hưởng".

+0

Tuyệt vời Phil , cảm ơn bạn, tôi đã không nhận thức được rằng chức năng, vẫn còn khá mới để jquery, làm việc một điều trị. – Israfel

2

Có thử jquery grep, như thế này:

arr = jQuery.grep(JSON_ARRAY, index); 
2

array.filter() tồn tại trong vanilla JavaScript:

function isBigEnough(element) { 
    return element >= 10; 
} 
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); 
// filtered is [12, 130, 44] 

Đó trang tài liệu bao gồm một polyfill cho các trình duyệt cũ:

if (!Array.prototype.filter) 
{ 
    Array.prototype.filter = function(fun /*, thisArg */) 
    { 
    "use strict"; 

    if (this === void 0 || this === null) 
     throw new TypeError(); 

    var t = Object(this); 
    var len = t.length >>> 0; 
    if (typeof fun !== "function") 
     throw new TypeError(); 

    var res = []; 
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 
    for (var i = 0; i < len; i++) 
    { 
     if (i in t) 
     { 
     var val = t[i]; 

     // NOTE: Technically this should Object.defineProperty at 
     //  the next index, as push can be affected by 
     //  properties on Object.prototype and Array.prototype. 
     //  But that method's new, and collisions should be 
     //  rare, so use the more-compatible alternative. 
     if (fun.call(thisArg, val, i, t)) 
      res.push(val); 
     } 
    } 

    return res; 
    }; 
} 
0

Bạn có thể sử dụng hàm jQuery.filter() để xây dựng một jQuery ob mới ject từ một tập con của các phần tử phù hợp.

var result = [ 
 
     {Parent:'opt1',Value:'opt1',Text:'Parent1 - Child 1'}, 
 
     {Parent:'opt2',Value:'opt1',Text:'Parent 2 - Child 1'}, 
 
     {Parent:'opt2',Value:'opt2',Text:'Parent 2 - Child 2'} 
 
    ]; 
 
    
 
     
 
var filteredResult = $(result).filter(function(idx) { 
 
    return result[idx].Parent === 'opt2'; 
 
});   
 
    
 
    
 
var options = $("#result-list"); 
 
$.each(filteredResult, function() { 
 
    options.append($("<option />").val(this.Value).text(this.Text)); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<select id="result-list" name="select" title="List"/>

Các vấn đề liên quan