我有一个JSON对象如下:
var testJSON = [
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_c", "AssetB": "asset_d" },
{ "AssetA": "asset_c", "AssetB": "asset_e" }];
我想要做的是逐步浏览对象并将重复键添加到另一个数组,usedAssets。到目前为止,这是我的代码:
var usedAssets = [];
for (var key in testJSON) {
console.log("Current key: " + key + " " + "value: : " + testJSON[key].AssetA);
console.log("Current key: " + key + " " + "value: : " + testJSON[key].AssetB);
// check if in array
if ((isInArray(testJSON[key].AssetA, usedAssets) || isInArray(testJSON[key].AssetB, usedAssets))) {
break;
}
else {
usedAssets.push(testJSON[key].AssetA);
usedAssets.push(testJSON[key].AssetB);
}
}
console.log(usedAssets);
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
但是,我的输出只是一个包含asset_a和asset_b的数组。 usedAssets数组应包含asset_a,asset_b和asset_c。我的最终目标是能够在迭代结束时确定我只使用了asset_a一次,asset_b只使用了一次,而asset_c只使用了一次。
答案 0 :(得分:1)
听起来你正在尝试计算每种资产的使用次数......最简单的方法是保留以前看过的物体地图。 Array.prototype.reduce是一种很好的方法
var testJSON = [{"AssetA":"asset_a","AssetB":"asset_b"},{"AssetA":"asset_a","AssetB":"asset_b"},{"AssetA":"asset_c","AssetB":"asset_d"},{"AssetA":"asset_c","AssetB":"asset_e"}];
var usage = testJSON.reduce((prev, next) => {
prev[next.AssetA] = next.AssetA in prev ? prev[next.AssetA] + 1 : 1;
prev[next.AssetB] = next.AssetB in prev ? prev[next.AssetB] + 1 : 1;
return prev;
}, {});
console.log('How much were they used?', usage);
// If you want to know which ones were used two or more times, you can use
console.log('Used more than once', Object.keys(usage).filter(key => usage[key] > 1))

上面没有减少的版本将是
var usage = {};
testJSON.forEach(el => {
usage[el.AssetA] = el.AssetA in usage ? usage[el.AssetA] + 1 : 1;
usage[el.AssetB] = el.AssetB in usage ? usage[el.AssetB] + 1 : 1;
});
console.log('How much were they used?', usage);
// If you want to know which ones were used two or more times, you can use
console.log('Used more than once', Object.keys(usage).filter(key => usage[key] > 1))
答案 1 :(得分:1)
此代码段会获取您的数组并将其缩小为所看到的值。
var testJSON = [
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_a", "AssetB": "asset_b" },
{ "AssetA": "asset_c", "AssetB": "asset_d" },
{ "AssetA": "asset_c", "AssetB": "asset_e" }];
var seen = {};
var result = testJSON.map(function(value){
return Object.keys(value).map(function(key){
return value[key];
})
}).reduce(function(a, b) {
return a.concat(b);
}, []).filter(function(value){
if (!seen[value]){
seen[value] = 1;
return true;
}
seen[value] += 1;
return false;
})
// seen contains the number of times each value was 'seen'
console.log('seen: ' + JSON.stringify(seen));
console.log('result: ' + result);

答案 2 :(得分:1)
基本上,你可以迭代数组的所有元素和对象的所有属性并计算出现的次数。
var testJSON = [{ "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_c", "AssetB": "asset_d" }, { "AssetA": "asset_c", "AssetB": "asset_e" }],
count = {};
testJSON.forEach(o => Object.keys(o).forEach(k => count[o[k]] = (count[o[k]] || 0) + 1));
console.log(count);
console.log(Object.keys(count).filter(k => count[k] > 1));

.as-console-wrapper { max-height: 100% !important; top: 0; }

ES5
var testJSON = [{ "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_c", "AssetB": "asset_d" }, { "AssetA": "asset_c", "AssetB": "asset_e" }],
count = {};
testJSON.forEach(function (o) {
Object.keys(o).forEach(function (k) {
count[o[k]] = (count[o[k]] || 0) + 1;
});
});
console.log(count);
console.log(Object.keys(count).filter(function (k) {
return count[k] > 1;
}));

.as-console-wrapper { max-height: 100% !important; top: 0; }