0

我想要一个数组,它将获取我的 json Ex 的重复键:

var catalog=[
        { ip: 'ipId_1', name: '192.160.121.11' },
        { ip: 'ipId_dyn_1_0', name: '192.160.121.12' },
        { ip: 'ipId_dyn_1_1', name: '192.160.121.12' }
    ];

由于 192.160.121.12 重复,我想要一个像 [ipId_dyn_1_0, ipId_dyn_1_1] 这样的数组,

到目前为止尝试过(小提琴演示):

var categories =[];
var dup= [];

$.each(catalog, function(index, value) {
    console.log( categories+''+value.name);
    if ($.inArray(value.name, categories) == -1) {
        categories.push(value.name);
    }else{
        dup.push(value.ip);
       console.log(value.ip); 
    }
});

console.log(categories);
console.log(dup);
4

4 回答 4

0

查看:

var catalog=[
        { ip: 'ipId_1', name: '192.160.121.11' },
        { ip: 'ipId_dyn_1_0', name: '192.160.121.12' },
        { ip: 'ipId_dyn_1_1', name: '192.160.121.12' }
    ];

var my = {};
var dup = [];

$.each(catalog, function(index, value) {
    if(!!!my[value.name]) {
        my[value.name] = [];
    }
    my[value.name].push(value.ip);    
});

$.each(my, function(index, value) {
    if(value.length > 1) {
        $.each(my[index], function(i, val) {
           dup.push(val);
        });
    }      
});

console.log(dup);

jsfiddle

于 2013-07-24T09:58:08.027 回答
0

如果你使用这个:

var catalog=[
        { ip: 'ipId_1', name: '192.160.121.11' },
        { ip: 'ipId_dyn_1_0', name: '192.160.121.12' },
        { ip: 'ipId_dyn_1_1', name: '192.160.121.12' }
    ];
var values = {};

$.each(catalog, function(index, data) {
    var name = data.name;
    if(typeof values[name] == 'undefined') values[name] = [];
    values[name].push(data.ip);
});
var dupes = [];
$.each(values,function(name,indexes) {
   if(indexes.length > 1)
   {
       dupes.push(indexes);
   }
});
console.dir(dupes);

dupes 现在将包含一个数组数组(重复集)

试一试,看看它是如何为你工作的

于 2013-07-24T09:58:48.180 回答
0
var cat_inverted = {}, categories = [];
$.each(catalog, function(index, value) {
    if (cat_inverted[value.name]) {
        cat_inverted[value.name].push(value.ip);
    } else {
        cat_inverted[value.name] = [value.ip];
        categories.push(value.name);
    }
});
var dup = [];
for (var name in cat_inverted) {
    if (cat_inverted[name].length > 1) {
        $.each(cat_inverted[name], function(i, ip) {
            dup.push(ip);
        });
    }
}
于 2013-07-24T09:59:18.777 回答
0

由于函数式方法,我会使用 lodash.js 或 underscore.js 来完成此任务。使用这种方法时,您可以更灵活地更改任务。您可以将数据存储在变量中,然后轻松地将它们转换为所需的数据结构。

var catalog=[
        { ip: 'ipId_1', name: '192.160.121.11' },
        { ip: 'ipId_dyn_1_0', name: '192.160.121.12' },
        { ip: 'ipId_dyn_1_1', name: '192.160.121.12' }
    ];
var categoriesObjectsUniqe =[];
var categories =[];

var dupObjects= [];
var dup= [];

categoriesObjectsUniqe = _.unique(catalog, 'name');
categories = _.map(categoriesObjectsUniqe, function(val) { 
            return val.name; })

dupObjects = _.filter(catalog, function(current) { 
    return  arr = _.filter(catalog, function(currentInner) { 
        return currentInner.name === current.name;  }).length > 1;
});
dup = _.map(categoriesObjUniqe, function(val) { 
            return val.ip; })

console.log('uniqe Objects :');
console.log(categoriesObjectsUniqe);

console.log('uniqe IPs :');
console.log(categories);

console.log('duplicate Objects :');
console.log(dupObjects);

console.log('duplicate ips :');
console.log(dup);

这是小提琴。 http://jsfiddle.net/3wHfB/94/

于 2013-07-24T11:02:49.117 回答