Javascript 计算对象数组中值的出现次数
我是Javascript新手,正在做一些我在网上找到的练习。由于我想真正了解事情是如何运作的,以及为什么有些方法比其他方法更好,我希望能得到一些反馈,说明我的方法为什么不好(或好) 我有一个XML,并使用SAX解析器对其进行处理,以获得“趋势关键字”。总的来说,我的函数应该返回5个最流行的函数。我的解析结果本身就是一个数组,该数组的格式为20个对象Javascript 计算对象数组中值的出现次数,javascript,node.js,Javascript,Node.js,我是Javascript新手,正在做一些我在网上找到的练习。由于我想真正了解事情是如何运作的,以及为什么有些方法比其他方法更好,我希望能得到一些反馈,说明我的方法为什么不好(或好) 我有一个XML,并使用SAX解析器对其进行处理,以获得“趋势关键字”。总的来说,我的函数应该返回5个最流行的函数。我的解析结果本身就是一个数组,该数组的格式为20个对象 [{ attributes: {} //children is just one big entry, separated by ';'
[{
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__: {}
}]
现在我的想法是,取出所有的子项
值,并将其存储在一个新数组中
let flattenArr = keywords.reduce( (acc, cur) => acc.concat(cur.children), []);
结果我得到了一个数组(长度也是20)的格式
[["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"]]
为了处理单个值,我拆分了数组
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;
}, []);
但是我的代码不起作用,即使两天后,我仍然在努力使它起作用,这让我发布了这个问题(这对我来说是一个新问题,因为我总是在stackoverflow上找到我的答案,因此我自己从来没有问过问题)让countKeywords=mapArr.reduce((allKeywords,keyword)=>{//typo el.keyword==keyword);//这是一个数组,所以所有关键字[关键字]都不起作用。。。
if(elem){
元素计数++
}否则{
所有关键字推送({
关键词:关键词,,
计数:1
})
}
返回所有关键字;
}, []);
应该可以这样做。由于使用了大量嵌套数组,因此最终必须两次展开映射(reduce+concat)。通过将其转换为原型方法,可以简化其可读性
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 }
以下是作为原型的平面图:
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)
你的最后一个片段。。。。是否缺少一个
?我希望{key:count}结构更容易构建/使用。为什么不能使用mdn示例?您的第一个对象缺少一些,
,变量的名称是什么<代码>当前值?什么是会计科目?如果您有多个数组数据实体,这也会有所帮助。要处理单个值,我拆分了数组。
该片段不会引发错误吗?你的问题有点不清楚。阵列展平的mdn示例工作正常。它一定是mapArr.reduce,您缺少了一个点…您缺少了一些代码>很抱歉回答晚了,ISP出现问题。。。既然我是一名JavaScript初学者,那么/a原型方法的“用途”到底是什么?当然我读过原型方法,但到目前为止我还不理解它的“用途”。它基本上只是Array类的一个扩展,允许您在数组上执行和链接函数。
let mapArr = flattenArr.flatMap(a => a).map(words => words.split(";")).flatMap(a => a)