[英]Count the number of occurrences and add it to grouped objects in a JSON array
Here's my input value -这是我的输入值 -
var input = [
{
status: 'yes',
data: ['a', 'b', 'c', 'd'],
score: 2,
rank: 2,
},
{
status: 'yes',
data: ['a', 'b', 'c'],
score: 9,
rank: 2,
},
{
status: 'yes',
data: ['a', 'b', 'c'],
score: 8,
rank: 2,
},
{
status: 'no',
data: ['a', 'b', 'c', 'd'],
score: 12,
rank: 3,
},
{
status: 'no',
data: ['a', 'b', 'c'],
score: 9,
rank: 3,
},
{
status: 'no',
data: ['a', 'b', 'c', 'd'],
score: 5,
rank: 3,
},
]
And here's what I'm trying to get as an output value -这就是我试图获得的输出值 -
[
{
status: 'yes',
data: ['a', 'b', 'c', 'd'],
occurrence: 1,
rank: 2,
},
{
status: 'yes',
data: ['a', 'b', 'c'],
occurrence: 2,
rank: 2,
},
{
status: 'no',
data: ['a', 'b', 'c', 'd'],
occurrence: 2,
rank: 3,
},
{
status: 'no',
data: ['a', 'b', 'c'],
occurrence: 1,
rank: 3,
},
]
The idea is to -这个想法是——
score
parameter from all the objectsscore
参数occurrence
parameter to all the objectsoccurrence
参数添加到所有对象data
being repeated as per each of the status
status
重复相同data
的次数Here's the code that I'm using (adopted from the 2nd half of this solution ) -这是我正在使用的代码(从本解决方案的第二部分采用)-
const res = Array.from(input.reduce((acc, {score, ...r}, index) => {
const key = JSON.stringify(r);
const current = acc.get(key) || {...r, occurrence: 0};
return acc.set(key, {...current, occurrence: current.occurrence + index});
}, new Map).values());
console.log(res);
But that's producing an unexpected output that looks like this -但这会产生一个意想不到的输出,看起来像这样 -
[
{
"status": "yes",
"data": ["a","b","c","d"],
"rank": 2,
"occurrence": 0
},
{
"status": "yes",
"data": ["a","b","c"],
"rank": 2,
"occurrence": 3
},
{
"status": "no",
"data": ["a","b","c","d"],
"rank": 3,
"occurrence": 8
},
{
"status": "no",
"data": ["a","b","c"],
"rank": 3,
"occurrence": 4
}
]
There's something that I'm missing to get the correct occurrences & I can't for the life of me understand what.为了获得正确的事件,我缺少一些东西,而且我一生都无法理解是什么。
I will do that this way我会这样做
const input = [ { status: 'yes', data: [ 'a', 'b', 'c', 'd' ], score: 2, rank: 2 } , { status: 'yes', data: [ 'a', 'b', 'c' ], score: 9, rank: 2 } , { status: 'yes', data: [ 'a', 'b', 'c' ], score: 8, rank: 2 } , { status: 'no', data: [ 'a', 'b', 'c', 'd' ], score: 12, rank: 3 } , { status: 'no', data: [ 'a', 'b', 'c' ], score: 9, rank: 3 } , { status: 'no', data: [ 'a', 'b', 'c', 'd' ], score: 5, rank: 3 } ] , result = input.reduce((a,{status,data,rank}) => { let same = a.find( x => x.status === status && x.rank === rank && JSON.stringify(x.data) === JSON.stringify(data) ) if (!same) a.push({ status, data: [...data], occurrence: 1, rank }) else same.occurrence++ return a },[]) console.log( result )
.as-console-wrapper {max-height: 100%!important;top:0}
I really didn't catch what did you want to do with occurence
value.我真的没有打扰到你想与做什么做
occurence
值。 In your example you were showing:在您的示例中,您显示的是:
status: 'no', data: ['a', 'b', 'c', 'd'], occurrence: 2,
And why is here occurence
equals 2
whereas 'no' is the first status with this value?为什么在这里
occurence
等于2
,而“不”是这个值的第一个状态?
At first try to remove current.occurrence + index
and leave just current.occurrence
if it will be always zero it will be correct to make next steps to calculate what you want.首先尝试删除
current.occurrence + index
并只保留current.occurrence
如果它始终为零,那么下一步计算您想要的内容将是正确的。
I doubt that you need index
there.我怀疑你在那里需要
index
。 Try to explain, and it helps to provide a decision.试着解释一下,这有助于做出决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.