删除JSON数组的重复键并获取其JSON路径

时间:2013-06-03 06:19:35

标签: javascript arrays json node.js key

我有以下JSON:

  

{ “响应”:{ “结果”:{ “信息”:{ “行”:[{ “LEADID”: “849730000000063017”, “SMOWNERID”: “849730000000061001”},{ “LEADID”: “849730000000063015” “SMOWNERID”: “849730000000061001”, “HIII”: “你好”},{ “LEADID”: “849730000000063007”, “SMOWNERID”: “849730000000061001”, “BYEE”: “塔塔”},{ “LEADID”:” 849730000000063005" , “SMOWNERID”: “849730000000061001”},{ “LEADID”: “849730000000063003”, “SMOWNERID”: “849730000000061001”},{ “LEADID”: “849730000000063001”, “SMOWNERID”: “849730000000061001”}]}} , “URI”: “/ CRM /私人/ JSON /信息/ getMyRecords”}}

我需要获取JSON路径,我使用下面的代码:

var str={"response":{"result":{"Leads":{"row":[{"LEADID":"849730000000063017","SMOWNERID":"849730000000061001",},
  {"LEADID":"849730000000063015","SMOWNERID":"849730000000061001"},
  {"LEADID":"849730000000063007","SMOWNERID":"849730000000061001","HIII":"hello"},
  {"LEADID":"849730000000063005","SMOWNERID":"849730000000061001","BYEE":"tata"},
  {"LEADID":"849730000000063003","SMOWNERID":"849730000000061001"},
  {"LEADID":"849730000000063001","SMOWNERID":"849730000000061001"}]}},
  "uri":"/crm/private/json/Leads/getMyRecords"}}
var keys = [];
getKeys(keys,str, '');
for(var i=0; i<keys.length; i++) {
  var d=new Array();
  d=keys[i][1].replace(/^\.|\.$/g, '')
  console.log(keys[i][0] + '=' +d)
}
function getKeys(keys, obj, path) {
  for(key in obj) {
    var currpath = path+'.'+key;
    keys.push([key, currpath]);
    if(typeof(obj[key]) === 'object') {
      getKeys(keys, obj[key], currpath);
    }
  }
}

以下是输出:

response=response
result=response.result
Leads=response.result.Leads
row=response.result.Leads.row
0=response.result.Leads.row.0
LEADID=response.result.Leads.row.0.LEADID
SMOWNERID=response.result.Leads.row.0.SMOWNERID
1=response.result.Leads.row.1
LEADID=response.result.Leads.row.1.LEADID
SMOWNERID=response.result.Leads.row.1.SMOWNERID
HIII=response.result.Leads.row.1.HIII
2=response.result.Leads.row.2
LEADID=response.result.Leads.row.2.LEADID
SMOWNERID=response.result.Leads.row.2.SMOWNERID
 BYEE=response.result.Leads.row.2.BYEE
3=response.result.Leads.row.3
LEADID=response.result.Leads.row.3.LEADID
SMOWNERID=response.result.Leads.row.3.SMOWNERID
4=response.result.Leads.row.4
LEADID=response.result.Leads.row.4.LEADID
SMOWNERID=response.result.Leads.row.4.SMOWNERID
5=response.result.Leads.row.5
LEADID=response.result.Leads.row.5.LEADID
SMOWNERID=response.result.Leads.row.5.SMOWNERID
uri=response.uri

数组元素键是重复的(即)LEADID和SMOWNERID在数组中是重复的。我想删除整个数组的重复项并显示如下输出:

response=response
result=response.result
Leads=response.result.Leads
row=response.result.Leads.row
0=response.result.Leads.row.0
LEADID=response.result.Leads.row.0.LEADID
SMOWNERID=response.result.Leads.row.0.SMOWNERID
HIII=response.result.Leads.row.0.HIII
BYEE=response.result.Leads.row.0.BYEE
uri=response.uri

我被困在这里,对此有任何帮助都会非常有帮助。

2 个答案:

答案 0 :(得分:1)

将函数getKeys修改为:

function getKeys(keys, obj, path) {
  for(key in obj) {
    var currpath = path+'.'+key;
    keys.push([key, currpath]);
    if(typeof(obj[key]) === 'object') {
      if(obj[key] instanceof Array){                   //added check for array
        keys.push(['0', currpath+'.0']);
        getKeys(keys, obj[key][0], currpath+'.0');
      }
      else
      getKeys(keys, obj[key], currpath);
    }
  }
}

更新

function getKeys(keys, obj, path) {
  for(key in obj) {
    var currpath = path+'.'+key;
    addKeys(key, currpath);
    //keys.push([key, currpath]);
    if(typeof(obj[key]) === 'object') {
      getKeys(keys, obj[key], currpath);
    }
  }
}

function addKeys(key, currpath) {                      //check before adding
  present = keys.map(function(x){return(x[1].split('.').slice(-1)[0])});
  if (present.indexOf(key)==-1){
    keys.push([key, currpath]);
  }
}

答案并不完全像你想要的那样,但却是我能得到的最接近的答案。你给HIII和BYEE的价值也是错误的。最后,可以在不同级别使用相同的键,但我只匹配键的名称。如果在不同级别使用相同的键名,则可能无效。

答案 1 :(得分:0)

另一种方法是从数组更改对象的键 - 将var keys = []更改为var keys = {},然后更改getKeys方法,如下所示:

function getKeys(keys, obj, path) {
 for(key in obj) {
  var currpath = path+'.'+key;
  if(keys[key])
    continue;
  keys[key] = currpath;
  if(typeof(obj[key]) === 'object') {
    getKeys(keys, obj[key], currpath);
  }
 }
}

现在,您还需要更改从getKeys返回后打印值的方式,因为现在键不是数组而是对象。