![](/img/trans.png)
[英]How do I implement contents of this github into my website? I don't know much about javascript
[英]I don't know how to implement this function in javascript
非常感謝您所做的貢獻,我與所有人共同做出了混合,部分解決了問題,但是我只剩下一件事了……我完成了此功能,添加了num_tweets,當添加了“ words”時是1,但是當它匹配並且應該+ 1時,該變量是未定義的,我不明白原因..問題必須從這些JSON執行此操作
{
"numtweets" : 1,
"dic_words" : {
"word1" : 1,
"word2" : 1,
"word3" : 1,
"word4" : 1,
"word5" : 1,
"word6" : 1,
"word7" : 1
}
}
{
"numtweets" : 1,
"dic_words" : {
"word1" : 1,
"word2" : 1,
"word9" : 1
}
}
{
"numtweets" : 1,
"dic_words" : {
"word1" : 1,
"word3" : 1,
"word10" : 1
}
}
我希望最后我返回一個僅添加所有字段,添加contweet的json,而且在這種情況下匹配的單詞也會這樣顯示...
{
"numtweets" : 3, //add numtweets
"dic_words" : {
"word1" : 3, //num_occurs word1 in array
"word2" : 2, //num_occurs word2 in array
"word3" : 2, //num_occurs word3 in array
"word4" : 1, //num_occurs word3 in array
"word5" : 1,
"word6" : 1,
"word7" : 1,
"word9" : 1,
"word10" : 1
}
}
我的功能是...
var r = function(key, values) {
result = { "numtweets" : 0,
"dic_words" : {}
};
// print(" entrada: " + tojson(values));
for (var idx = 0; idx < values.length; idx++) {
result.numtweets += values[idx].numtweets;
// print(" json: " + tojson(values[idx].dic_words));
for (paraula in values[idx].dic_words) {
if(values[idx].dic_words.hasOwnProperty(paraula)) {
if(values[idx].dic_words[paraula]) {
result.dic_words[paraula] = result.dic_words[paraula] + values[idx].dic_words[paraula]; //IS these **part result.dic_words[paraula]** is ***undefined***
}
else
result.dic_words[paraula] = 1; /^* this part is correct, and if the word appers one only it appears ocurrs
}
}
}
return result;
};
輸出是
{
"numtweets" : 3, //add numtweets
"dic_words" : {
**"word1" : NaN, //num_occurs word1 in array MUST BE 3
"word2" : NaN, //num_occurs word2 in array MUST BE 2
"word3" : NaN, //num_occurs word3 in array MUST BE 2**
"word4" : 1, //num_occurs word3 in array
"word5" : 1,
"word6" : 1,
"word7" : 1,
"word9" : 1,
"word10" : 1
}
}
請任何建議或幫助將受到歡迎...
var data = [{ "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word3" : 1, "word4" : 1, "word5" : 1, "word6" : 1, "word7" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word9" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word3" : 1, "word10" : 1 } }]; function processData(input) { var result = {numtweets: 0, dic_words:{}}; input.forEach(function(item){ result['numtweets'] += item['numtweets']; for(var i in item['dic_words']) { result['dic_words'][i] = (result['dic_words'][i]) ? result['dic_words'][i]+item['dic_words'][i] : item['dic_words'][i]; } }) return result; } console.log(processData(data))
您可以使用以下功能:
function getSummary(data) { return data.reduce(function (acc, o) { return { numtweets: acc.numtweets + (+o.numtweets || 0), dic_words: Object.keys(o.dic_words || {}).reduce(function (words, word) { words[word] = (words[word] || 0) + o.dic_words[word]; return words; }, acc.dic_words) }; }, { numtweets: 0, dic_words: {} }); } // Sample data var data = [{ "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word3" : 1, "word4" : 1, "word5" : 1, "word6" : 1, "word7" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word9" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word3" : 1, "word10" : 1 } }, { // empty object added to test algorithm will survive and ignore this... }]; console.log(getSummary(data));
.as-console-wrapper { max-height: 100% !important; top: 0; }
var arr = [ { "numtweets": 1, "dic_words": { "word1": 1, "word2": 1, "word3": 1, "word4": 1, "word5": 1, "word6": 1, "word7": 1 } }, { "numtweets": 1, "dic_words": { "word1": 1, "word2": 1, "word9": 1 } }, { "numtweets": 1, "dic_words": { "word1": 1, "word3": 1, "word10": 1 } } ] var newObj = { numtweets: 0, dic_words: {} }; arr.forEach(function (item, index, array) { newObj.numtweets += item.numtweets; for (var prop in item.dic_words) { if (!newObj.dic_words[prop]) { newObj.dic_words[prop] = 0 }; newObj.dic_words[prop] += item.dic_words[prop]; } }) console.log(newObj);
將Array.map()方法與ES6 Arrow函數一起使用。
DEMO
var CountObjVal = [{ "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word3" : 1, "word4" : 1, "word5" : 1, "word6" : 1, "word7" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word2" : 1, "word9" : 1 } }, { "numtweets" : 1, "dic_words" : { "word1" : 1, "word3" : 1, "word10" : 1 } }]; var r = function (countObjVals) { var obj = {numtweets: 0, dic_words: {}}; CountObjVal.map(elem => Object.keys(elem.dic_words).map(item => obj.dic_words[item] = 0)); for (var i in CountObjVal) { obj.numtweets += CountObjVal[i].numtweets; Object.keys(CountObjVal[i].dic_words).map(item => obj.dic_words[item] += CountObjVal[i].dic_words[item]); } return obj; }; console.log(r(CountObjVal));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.