【发布时间】:2016-11-04 14:45:28
【问题描述】:
我想用 jQuery 删除数组中的重复项
数组
[[["Time",0],["Budget",1],["Scope",2],["Technical",3],["Budget",1]]]
如果此示例中的名称(“预算”)出现多次, 然后我想删除这个值对。
我尝试了很多,但到目前为止没有任何效果。 我试图遍历数组并设置一个重复的变量,然后添加 没有重复的项目到新数组
var items = new Array ();
// get the data with ajax request
$.ajax({
url : "localhost/..",
dataType : 'json',
success: function(data) {
for (var prop_name in data.items) {
var duplicate = 0;
var count = 0;
// count how often the value appears
for (i = 0; i < data.items.length; i++) {
if (data.items[prop_name] == data.items[i]){
var count = count+1
}
}
// if there are duplicates, just add the value once
for (i = 0; i < items.length; i++) {
if (data.items[prop_name] == data.items[i]){
duplicate = duplicate+1;
}
if (duplicate <= 1){
items.push([ data.items[prop_name], count])
}
duplicate = 0;
}
}
【问题讨论】:
-
您确定
data.items是数组数组中的数组吗?看起来你的一对括号太多了。您能否提供console.log(JSON.stringify(data.items))的输出来确定这一点? -
是的,对不起,最后它再次被推入一个数组。因为jqplot(制作图表)需要那种数据格式
标签: jquery arrays multidimensional-array duplicates