2017-05-17 7 views
0

私はJavascriptを初めて使用し、オンラインで見つかったいくつかの演習を行っています。私は物事がどのように作用するのか、なぜいくつかのアプローチが他のものより優れていることを本当に理解したいので、私のアプローチが悪い(または良い)理由をいくつかフィードバックしていただきたいと思います。オブジェクト配列内の値のカウントが発生する

私はXMLを持っていて、それをSAXパーサで処理して "トレンドキーワード"を取得します。全体的な結果として、私の関数は5つの最も一般的なものを返す必要があります。私の解析自体の結果は、今、私の考えは、その結果、これらすべてのchildren値を引き出し、新しい配列

let flattenArr = keywords.reduce((acc, cur) => acc.concat(cur.children), []); 

でそれを保存するために、したフォーマットで20個のオブジェクトの配列

[{ 
    attributes: {} 
    //children is just one big entry, separated by ';' 
    children: ["Comey, James B;Federal Bureau of Investigation;Trump, Donald J;United States Politics and Government"] 
    IsSelfClosing: false, 
    name: 'ADX_KEYWORDS', 
    parent: null, 
    __proto__: {} 
}] 

ですI形式

[["Comey, James B;Federal Bureau of Investigation;Trump, Donald J;United States Politics and Government"], 
["Trump, Donald J;Comey, James B;Federal Bureau of Investigation;United States Politics and Government"]] 

単一値を処理するために、アレイ(これも長さ20)を取得し、私は、配列を分割

let mapArr = flattenArr 
    .map(arr => arr.split(';')) 
    .reduce((acc, cur) => acc.concat(cur), []); 

となり、結果として、長さ115の新しい配列には、すべての単一のキーワードが区切られています。そしてここに私はどのように進むべきか分からない点があります。私はMDN(Array.Reduce)の例を試しましたが、実際にこれらのオブジェクトを処理できるかどうかはわかりませんでした。

//Result from MDN example, {value: quantity} 
{ 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } 

は、だから私は、その量

{ 
    key: keyword, 
    count: n 
} 

のプロパティで、すべてのキーワードのためのオブジェクトを作成するためのアイデアを持っていたし、

let countKeywords = mapArrreduce((allKeywords, keyword) => { 
    if (allKeywords[0] != null && keyword in allKeywords[keyword]) { 
    allKeywords[keyword].count++ 
    } else { 
    allKeywords.push({ 
     keyword: keyword, 
     count: 1 
    }) 
    } 
    return allKeywords; 
}, []); 

でこれを実現しようとしましたが、私のコードはdoesnの2日後でも私はまだそれを動作させるのに苦労しています。これは私にこの質問を投稿させました(私はいつも私の答えをstackoverflowで見つけたので自分自身で質問してください)

+0

あなたの最後のスニペット....それは '.'が欠けているのですか?私は{key:count}構造体​​が構築/操作するのがはるかに簡単であると期待しています。なぜあなたはmdnの例を使用できませんでしたか? –

+0

最初のオブジェクトに '、'がいくつかありません。変数の名前は何ですか? 'cur'? 'acc'とは何ですか?配列データのエンティティが複数ある場合にも役立ちます。 – brandonscript

+0

'単一の値を処理するには、配列を分割します。スニペットがエラーをスローしませんか?あなたの質問はかなり不明です。あなたの配列が正しく平坦化されたmdnの例は正常に動作します。 –

答えて

-1

このようにする必要があります。多数のネストされた配列を使用しているので、フラット・マップ(reduce + concat)が2回必要になります。プロトタイプのメソッドに変換することで、読みやすくすることができます。

let flattenArr = [ 
    ["Comey, James B;Federal Bureau of Investigation;Trump, Donald J;United States Politics and Government"], 
    ["Trump, Donald J;Comey, James B;Federal Bureau of Investigation;United States Politics and Government"], 
    ["Trump, Donald J;Comey, James B;Federal Bureau of Investigation;United States Politics and Government"], 
    ["Trump, Donald J;Comey, James B;Federal Bureau of Investigation;United States Politics and Government"] 
] 

let mapArr = flattenArr.reduce((a, b) => a.concat(b)).map(words => words.split(";")).reduce((a, b) => a.concat(b)) 

let orderedCount = mapArr.reduce(function (acc, cur) { 
    if (typeof acc[cur] === 'undefined') { 
    acc[cur] = 1; 
    } else { 
    acc[cur] += 1; 
    } 

    return acc; 
}, {}) 

console.log(orderedCount) 

出力:

{ 'Comey, James B': 4, 
    'Federal Bureau of Investigation': 4, 
    'Trump, Donald J': 4, 
    'United States Politics and Government': 4 } 

はここでプロトタイプの内線としてflatMapです:

Array.prototype.flatMap = function(lambda) { 
    return Array.prototype.concat.apply([], this.map(lambda)); 
} 

そして、それを使用する:

let mapArr = flattenArr.flatMap(a => a).map(words => words.split(";")).flatMap(a => a) 
+0

遅れて申し訳ありませんが、ISPの問題です...私はJavaScriptの初心者ですから、その "プロトタイプ"メソッドの "使用"は正確ですか?もちろん、プロトタイプのメソッドについては読んでいますが、これまでのところ、 "使用"を理解できませんでした。 – Tobias

+0

基本的にArrayクラスの拡張で、配列に対して関数を実行したり連鎖したりできます。 – brandonscript

-1
let countKeywords = mapArr.reduce((allKeywords, keyword) => {//typo <== 
var elem=allKeywords.find(el=>el.keyword===keyword);//its an array so allKeywords[keyword] wont work... 
    if (elem) { 
     elem.count++ 
    } else { 
    allKeywords.push({ 
     keyword: keyword, 
     count: 1 
    }) 
} 
return allKeywords; 
}, []); 
関連する問題