获取重复的密钥名称json jquery

时间:2013-07-24 09:44:50

标签: javascript jquery json

我想要一个可以获取json重复键的数组 例如:

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],

这样的数组

到目前为止尝试过(Fiddle Demo):

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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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现在将包含一组数组(重复项集)

给它一个运行,看看它是如何工作的

答案 2 :(得分: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);
        });
    }
}

答案 3 :(得分: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/