非常感谢您的贡献,所有这些我已经混合了,我已经解决了部分但我只剩下一件事...... 我已经完成了这个函数,它添加了num_tweets,当它们为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
}
}
我希望最后我返回一个json,只添加所有字段,添加contweets,但在这种情况下匹配的单词会像这样出来......
{
"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
}
}
欢迎任何建议或帮助......
答案 0 :(得分:0)
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))
&#13;
答案 1 :(得分:0)
您可以使用此功能:
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; }
答案 2 :(得分: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);
&#13;
答案 3 :(得分:0)
使用Array.map()方法和ES6箭头功能。
<强>样本强>
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));