计数出现在整个对象中包含的值substring
我想计算对象值的 substring ,即整个对象包含一个字符串而不是键 equals equals a字符串。 XSLT中的工作XPath将是
count(// v [contrains(。,current-grouping-key())])
但我无法在JavaScript中弄清楚这一点。
我尝试了以下内容:
const obj =
[ { v: 'Bla Blu Bli' },
{ v: 'Bla Blu Bli' },
{ v: 'Bla Blu' },
{ v: 'Bla Bli' }
];
const count = obj.reduce( function(sums,entry) {
sums[entry.v] = (sums[entry.v] || 0) + 1;
return sums;
},{});
console.log(count)
但这仅计算确切的字符串。 所以我得到:
"Bla Blu Bli": 2,
"Bla Blu": 1,
"Bla Bli": 1
有没有
"Bla Blu Bli": 2,
"Bla Blu": 3,
"Bla Bli": 3
办法计算子字代替确切值?
I want to count substrings of object values, i.e. the entire object contains a string instead of a key equals a string.
A working Xpath in XSLT would be
count(//v[contains(.,current-grouping-key())])
But i can't figure this out in javascript.
I tried the following:
const obj =
[ { v: 'Bla Blu Bli' },
{ v: 'Bla Blu Bli' },
{ v: 'Bla Blu' },
{ v: 'Bla Bli' }
];
const count = obj.reduce( function(sums,entry) {
sums[entry.v] = (sums[entry.v] || 0) + 1;
return sums;
},{});
console.log(count)
But this counts the exact strings only.
So I get:
"Bla Blu Bli": 2,
"Bla Blu": 1,
"Bla Bli": 1
instead of
"Bla Blu Bli": 2,
"Bla Blu": 3,
"Bla Bli": 3
Is there a way to count the substrings instead of the exact values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第二版应该更快。
(您写了我有100k+ values ,在评论中)
它仅创建一个不同的系列的数组,与相同系列的副本相关联
并通过将此数量添加到包括相同值的其他集合的数量中来遍历此数组,
通过仅选择大小较大的人。
我之所以使用集元素,是因为根据DOC A [set] .has(value)比[数组]更快。
This second version should be faster.
(you wrote I have 100k+ values , in a comment)
It creates an array only of different series, associated with the number of copies of identical series
And traverses this array by adding to this quantity those of the other sets including the same values,
by selecting only those whose size is larger.
I used Set elements because according to the doc a [set].has(value) is faster than an [array].includes(value)
您可以使用它:
you can use that:
您必须使用索引或类似的索引,以查看字符串中是否存在子字符串。
例子:
You have to use indexOf or similar, to see if a substring exists in a string.
Example: