2016-09-12 4 views
1

Ich habe ein Objekt JSON wie folgt:Javascript Array füllen basierend auf JSON-Objekt

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

Was ich tun möchte, Schritt für Schritt durch das Objekt und fügen Sie Schlüssel zu einem anderen Array wiederholen, usedAssets. Hier ist mein Code so weit:

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

jedoch meine Ausgabe von das ist nur ein Array mit asset_a und asset_b. Das usedAssets-Array sollte Asset_a, Asset_b und Asset_c enthalten. Mein Endziel ist es, am Ende der Iteration feststellen zu können, dass ich Asset_a nur einmal, Asset_b nur einmal und Asset_c nur einmal verwendet habe.

+0

testJSON ist ein Array. Um zu iterieren, teste testJSON.forEach (function (eachly) {}) – yBrodsky

+0

@ user1655756 Das stimmt, aber nicht die Quelle des Fehlers in dem Fall ... –

+0

Einfach die Pause entfernen; das tötet das forloop das ist nicht, was Sie wollen –

Antwort

1

Grundsätzlich können Sie alle Elemente des Arrays und alle Eigenschaften des Objekts durchlaufen und das Auftreten zählen.

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

+0

Die vorherigen Antworten verwenden auch die Notation '=>', aber ich bekomme eine Syntaxfehler bei diesen Vorkommen, wenn ich das Snippet in mein Projekt kopiere. – DeeTee

+0

versuchen Sie die mehr * traditionelle * Codierung. –

+0

würden Sie bitte ein Beispiel geben? – DeeTee

1

Es klingt, als ob Sie versuchen zu zählen, wie oft jedes Asset verwendet wurde ... Der einfachste Weg ist, eine Karte von Objekten zu behalten, die Sie schon einmal gesehen haben. Array.prototype.reduce ist eine schöne Art und Weise, ohne

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

Eine Version der oben genannten zu tun wäre zu reduzieren

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

Dieser Code scheint zu tun, was ich will. Ich bin jedoch sehr neu in JS, und habe es schwer zu verstehen, zu reduzieren. – DeeTee

+0

@DeeTee Haben Sie den von mir bereitgestellten Link gelesen?Der einfachste Weg, es zu verstehen, ist '[1,2,3,4] .reduce ((prev, next) => prev + next, 0)'. Dadurch werden alle Elemente im Array hinzugefügt, wobei als Startwert für die Berechnungen eine 0 angegeben wird. In meinem Beispiel ist der Startwert ein Objekt, das als Karte verwendet wird, und wir iterieren über das Array, für jedes Element das Objekt Füllung in dem Array –

+0

ich es gelesen habe, immer noch eine harte Zeit Verständnis hatte. Wie auch immer, diese Methode sagt mir, wie oft jedes Element im Objekt erscheint, aber es sagt mir nicht, wie viele Verbindungen zu welchen Assets gehören. Zum Beispiel hat Asset_a zwei Verbindungen zu Asset_b. In diesem Fall würde ich eine Box für asset_a und eine Box für asset_b zeichnen und zwei Verbindungen zwischen ihnen haben. – DeeTee

1

Dieser Code-Schnipsel nimmt Ihr Array und reduziert sie auf die gesehenen Werte.

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

Verwandte Themen