且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用JavaScript/jquery仅获取多维数组中的唯一项?

更新时间:2022-12-12 13:10:08

使用词典查看您已经拥有的项目:

Use a dictionary to see which item you have already:

var dict = {};

$.getJSON(url1,addToLocalArray);

function addToLocalArray(data1){

  for (var i in data1.layers){

    $.each(data1.layers[i].legend, function( a, b ) {

        for(var a in data1.layers[i].legend[a]){

            $.each(data1.layers[i].legend, function( key, val ){

              if(!dict[val.url+'-'+val.label]){
                // or create some sort of unique hash for each element...
                dict[val.url+'-'+val.label] = 1;

                items.push({label: val.label, url: "long link" + val.url});
              }

            });

        }
    });
  }
}