如何使用JavaScript/jquery仅获取多维数组中的唯一项? [英] How to get only unique items in a multidimensional array with JavaScript/jquery?
本文介绍了如何使用JavaScript/jquery仅获取多维数组中的唯一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我得到了一个具有2维的多维数组项. 我从数据库中获得了这个数组,但是它最多可以填充2600+个对象,但是如果我能知道它的独特性,它将大约有30个对象.怎么解决呢?
I got for example a multidimensional array items with 2 dimensions. I get this array from a database, but it will fill up to 2600+ objects, but if i could some how unique this it would be around 30 objects. How to solve this?
设置为: 我如何获取信息:
The set up is: How i get the information:
$.getJSON(url1,function(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 ){
items.push({label: val.label, url: "long link" + val.url});
});
};
});
};
items[0].label
items[0].url
items[1].label
items[1].url
等...
我在php中找到了另一个关于此的stackoverflow页面,但是我无法在JavaScript/JQuery中使它工作. Stackoverflow php解决方案
I found another stackoverflow page about this in php, but i can't get it to work in JavaScript/JQuery. Stackoverflow php solution
推荐答案
使用词典查看您已经拥有的项目:
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});
}
});
}
});
}
}
这篇关于如何使用JavaScript/jquery仅获取多维数组中的唯一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文