2017-02-20 89 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));

相關問題