2017-02-20 63 views
4

我对在JavaScript中迭代到JS对象和一些数组函数有些怀疑。比方说,我有这些变量:映射到另一个节点并比较节点

var json1 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]"; 
var json2 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]"; 

我如何能阵列

var ids1 = json1.ids (would be 1,2) 
var ids2 = json2.ids (would be 1,2,3) 

在做一个变量,只有ID和使另一个变量只与不同的ID

var idsdiff = diff(ids1, ids2) (would be 3) 
+1

您的JavaScript对象不正确。你有嵌套的引号 - 还要注意[JSON不是JavaScript对象](012)http://www.fizerkhan.com/blog/posts/JSON-is-not-Javascript-Object.html) – mplungjan

回答

2

var json1 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}], 
 
    json2 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}, {"id":3,"name":"z"}], 
 
    result1 = json1.map(function (a) { return a.id; }), 
 
    result2 = json2.map(function (a) { return a.id; }); 
 

 
var diffs = result2.filter(function (item) { \t 
 
    return result1.indexOf(item) < 0; 
 
}); 
 

 
console.log(result1); 
 
console.log(result2); 
 
console.log(diffs);

indexOffiltermap不可在iE 012前。

UPDATE:按@亚历-Ionutmihai的评论,过滤器将失败[1,2,4][1,2,3]

此代码似乎更好:

var json1 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}], 
 
     json2 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}, {"id":3,"name":"z"}], 
 
     result1 = json1.map(function (a) { return a.id; }), 
 
     result2 = json2.map(function (a) { return a.id; }); 
 

 
//as per @alexandru-Ionutmihai this is inaccurate for [1,2,4] and [1,2,3] 
 
/*var diffs = result2.filter(function (item) { \t 
 
    return result1.indexOf(item) < 0; 
 
});*/ 
 

 
//here's a workaround 
 
function arr_diff(a, b) { 
 
    var i, 
 
    la = a.length, 
 
    lb = b.length, 
 
    res = []; 
 
    if (!la) 
 
    return b; 
 
    else if (!lb) 
 
    return a; 
 
    for (i = 0; i < la; i++) { 
 
    if (b.indexOf(a[i]) === -1) 
 
     res.push(a[i]); 
 
    } 
 
    for (i = 0; i < lb; i++) { 
 
    if (a.indexOf(b[i]) === -1) res.push(b[i]); 
 
    } 
 
    return res; 
 
} 
 

 
var diffs = arr_diff(result1, result2), 
 
    testDiff = arr_diff([1, 2, 4], [1, 2, 3]); 
 

 
console.log(result1); 
 
console.log(result2); 
 
console.log(diffs); 
 
console.log(testDiff);

arr_diff信贷@ Nomaed的评论这个question's的答案。

+0

As @ alexandru-Ionutmihai说,如果你使用'json1 = [1,2,3]','json2 = [1,2,4]' – ElChiniNet

+0

编辑的答案数组,这种方法不起作用。 –

1

您可以使用map方法和filter方法。

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]; 
 
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]; 
 
var j1=json1.map((x)=>{return x.id}); 
 
var j2=json2.map((x)=>{return x.id}); 
 
var diff = j2.filter(function(el){ 
 
    return j1.indexOf(el)==-1; 
 
}).concat(j1.filter(function(el){ 
 
    return j2.indexOf(el)==-1; 
 
})); 
 
console.log(diff);

此外,该代码工作如果两个JSON数组包含IDs是不同的。

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 4, "name":"y"}, {"id": 5, "name":"y"}]; 
 
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]; 
 
var j1=json1.map((x)=>{return x.id}); 
 
var j2=json2.map((x)=>{return x.id}); 
 
var diff = j2.filter(function(el){ 
 
    return j1.indexOf(el)==-1; 
 
}).concat(j1.filter(function(el){ 
 
    return j2.indexOf(el)==-1; 
 
})); 
 
console.log(diff);

+1

不错的解决方案,更干净。谢谢。 – 2Fast4YouBR

0

要获得只装着每一个对象,做简单的id属性阵列...

var ids1 = json1.map(x => x.id) 
var ids2 = json2.map(x => x.id) 

如果使用ES6,或版本transpiler,你可以使用扩散算子来得到两者之间的差异:

var diff = [...id1.filter(x => id2.indexOf(x) == -1), ...id2.filter(x => id1.indexOf(x) == -1)] 

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]; 
 
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]; 
 

 
var ids1 = json1.map(x => x.id); 
 
var ids2 = json2.map(x => x.id); 
 

 
var diff = [...ids1.filter(x => ids2.indexOf(x) == -1), ...ids2.filter(x => ids1.indexOf(x) == -1)]; 
 
console.log(diff);

1

如果这些JSONs不被解析,你需要一个额外的步骤之前:

json1 = JSON.parse(json1); 

如果没有,请使用此代码:

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]; 
 
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]; 
 

 
// extra steps, if necessary 
 
// json1 = JSON.parse(json1); 
 
// json2 = JSON.parse(json2); 
 

 
function returnID (item) { 
 
    return item.id; 
 
}; 
 

 
json1 = json1.map(returnID); 
 
json2 = json2.map(returnID); 
 

 
var diff = json2.filter(function (item) { 
 
    return json1.indexOf(item) < 0; 
 
}); 
 

 
console.log(diff);

+0

如果'json1 = [1,2,4]'和'json2 = [1,2,3]'这个代码不起作用。 –

2

您可以使用散列表id并与该值区分。然后通过过滤来呈现结果。

function getId(a) { return a.id; } 
 

 
var obj1 = JSON.parse('[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]'); 
 
var obj2 = JSON.parse('[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]'); 
 
var ids1 = obj1.map(getId); 
 
var ids2 = obj2.map(getId); 
 
var hash = {}; 
 

 
ids1.forEach(function (a) { 
 
    hash[a] = 1; 
 
}); 
 
ids2.forEach(function (a) { 
 
    hash[a] = (hash[a] || 0) - 1; 
 
}); 
 

 
var difference = Object.keys(hash).filter(function (a) { return hash[a]; }).map(Number); 
 
console.log(ids1); 
 
console.log(ids2); 
 
console.log(hash); 
 
console.log(difference);
.as-console-wrapper { max-height: 100% !important; top: 0; }

随着lodash,你可以使用_.xor的对称差。

var ids1 = [1, 2], 
 
    ids2 = [1, 2, 3]; 
 

 
console.log(_.xor(ids1, ids2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

0

在这里,我让你们两个函数来得到你想要的结果:

第一功能(getIds):

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]; 
 
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]; 
 

 
function getIds (array) { 
 
    return array.map(function (obj) { 
 
     return obj.id; 
 
    }); 
 
} 
 

 
console.log(getIds(json1)); 
 
console.log(getIds(json2));

二级功能(getDiff)

var json1 = [1, 2, 4, 5]; 
 
var json2 = [1, 2, 3]; 
 

 
function getDiff (array1, array2) { 
 
    return array1.concat(array2).filter(function (id, index, arr) { 
 
     return arr.indexOf(id) === arr.lastIndexOf(id); 
 
    }); 
 
} 
 

 
console.log(getDiff(json1, json2));

相关问题