简体   繁体   English

Javascript基于JSON对象填充数组

[英]Javascript populate array based on JSON object

I have a JSON object as follows: 我有一个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" }];

What I want to do is step through the object and add repeating keys to another array, usedAssets. 我想要做的是逐步通过对象并将重复键添加到另一个数组,usedAssets。 Here is my code so far: 到目前为止,这是我的代码:

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;
}

However, my output of that is just an array with asset_a and asset_b. 但是,我的输出只是一个包含asset_a和asset_b的数组。 The usedAssets array should contain asset_a, asset_b, and asset_c. usedAssets数组应包含asset_a,asset_b和asset_c。 My end goal is to be able to determine at the end of the iteration, that I used asset_a only once, asset_b only once, and asset_c only once. 我的最终目标是能够在迭代结束时确定我只使用了asset_a一次,asset_b只使用了一次,而asset_c只使用了一次。

It sounds like you're trying to count how many times each asset was used... The simplest way is to keep a map of objects you have seen before. 这听起来像是在尝试计算每个资产的使用次数......最简单的方法是保留您之前看到的对象的地图。 Array.prototype.reduce is a nice way to do that 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)) 

A version of the above without reduce would be 没有减少的上述版本将是

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))

This snippet takes your array and reduces it to the seen values. 此代码段将获取您的数组并将其减少到所看到的值。

  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); 

Basically, you could iterate all elements of the array and all properties of the object and count the occurence. 基本上,您可以迭代数组的所有元素和对象的所有属性并计算出现的次数。

 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 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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM