确认在阵列的对象的出现次数 [英] Confirm number of occurrences of a objects in array
问题描述
我在创造一个程序来检查的基础上由我设定规则对象数组出现的次数了巨大的困难。如果特定对象存在多于一个,我指望出现的次数。
输入示例:
[
{NAME1:{12:{10:1}}},
{NAME1:{12:{10:1}}},
{NAME1:{12:{10:1}}}
]
输出示例:
[
{NAME1:{12:{10:3}}}
]
警告:这是一个示例
所以,我怎么做的:
让SUPER = [\r
{NAME1:{12:{10:1}}}\r
{NAME1:{12:{10:1}}}\r
{NAME1:{12:{10:1}}}\r
{NAME1:{12:{10:1}}}\r
]\r
最后= [];\r
\r
对于(让_super超){\r
_super = JSON.stringify(_super);\r
让II = 0,LL = SUPER.length,数量= 0;\r
\r
用于(ⅱ;ⅱ&下; LL;ⅱ++){\r
让电流= JSON.stringify(SUPER [II]);\r
如果(_super ===电流){\r
SUPER.splice(二,1);\r
数++;\r
}\r
}\r
\r
如果(数){\r
FINAL.push(克隆功能(目的,源){\r
目的地=目标|| {};\r
对于(源VAR道具){\r
typeof运算源[道具] ==='对象'和;&安培;来源[道具] == NULL和放大器;!&安培;来源[道具]\r
?目的地[道具] =克隆({},源[道具])\r
:目的地[道具] =号\r
;\r
}\r
返回目的地;\r
}({},JSON.parse(_super)));\r
}\r
}\r
\r
document.body.innerHTML = JSON.stringify(FINAL,NULL,4);
\r
所以,我遍历了SUPER两次,在另一个内测试每个对象,如果我找到相等的字符串,我由一个增加的数量,并从数组中删除的对象,然后使用这个脚本的号码分配给对象的最内属性
如果(数字){
FINAL.push(克隆功能(目的,源){
目的地=目标|| {};
对于(源VAR道具){
typeof运算源[道具] ==='对象'和;&安培;来源[道具] == NULL和放大器;!&安培;来源[道具]
?目的地[道具] =克隆({},源[道具])
:目的地[道具] =号
;
}
返回目的地;
}({},JSON.parse(_super)));
}
但无法正常工作,因为在这条线冲突:
如果(_super ===电流){
SUPER.splice(二,1);
数++;
}
我搞乱了循环。任何想法如何解决?我很开放的建议,我不知道是否有更好的方式来实现这一目标,我希望有人知道。
感谢。
在拼接数组,元素被移到左边1,但循环指数继续向前走不占这种转变。您可以通过每次拼接时间递减1电流回路指数占了。
例如:
T = [1,2,3,4];
的console.log(T [1]); //输出:2
在拼接//
t.splice(1,1);
//拼接后的数组元素被移到
的console.log(T [1]); //输出:3
工作方案:
让SUPER = {[\r
NAME1:{\r
12:{\r
10:1\r
}\r
}\r
},{\r
NAME1:{\r
12:{\r
10:1\r
}\r
}\r
},{\r
NAME1:{\r
12:{\r
10:1\r
}\r
}\r
},{\r
NAME1:{\r
12:{\r
10:1\r
}\r
}\r
}],\r
最后= [];\r
\r
对于(让我= 0; I< SUPER.length;我++){\r
让_super = JSON.stringify(SUPER [I]),\r
二= I + 1,\r
LL = SUPER.length,\r
数= 1;\r
\r
用于(ⅱ;ⅱ&下; LL;ⅱ++){\r
让电流= JSON.stringify(SUPER [II]);\r
如果(_super ===电流){\r
SUPER.splice(二,1);\r
ii--;\r
数++;\r
}\r
}\r
\r
如果(数){\r
FINAL.push(克隆功能(目的,源){\r
目的地=目标|| {};\r
对于(源VAR道具){\r
typeof运算源[道具] ==='对象'和;&安培;来源[道具] == NULL和放大器;!&安培;来源[道具]目的地[道具] =克隆({},源[道具]):目的地[道具] =号;\r
}\r
返回目的地;\r
}({},JSON.parse(_super)));\r
}\r
}\r
\r
document.body.innerHTML = JSON.stringify(FINAL,NULL,4);
\r
我还初始化二,数为1以减少内部循环的一次迭代。
I'm having a huge difficulty in creating a program to check the number of occurrences of objects in array based on rules set by me. If a particular object exists more then one, I count the number of occurrences.
Example input:
[
'{"NAME1":{"12":{"10":1}}}',
'{"NAME1":{"12":{"10":1}}}',
'{"NAME1":{"12":{"10":1}}}'
]
Example output:
[
'{"NAME1":{"12":{"10":3}}}'
]
WARNING: THIS IS A EXAMPLE.
So, how i'm doing:
let SUPER = [
{"NAME1":{"12":{"10":1}}},
{"NAME1":{"12":{"10":1}}},
{"NAME1":{"12":{"10":1}}},
{"NAME1":{"12":{"10":1}}}
],
FINAL = [];
for (let _super of SUPER) {
_super = JSON.stringify(_super);
let ii = 0, ll = SUPER.length, number = 0;
for (ii; ii < ll; ii++) {
let current = JSON.stringify(SUPER[ii]);
if (_super === current) {
SUPER.splice(ii, 1);
number++;
}
}
if (number) {
FINAL.push(function clone(destination, source) {
destination = destination || {};
for (var prop in source) {
typeof source[prop] === 'object' && source[prop] !== null && source[prop]
? destination[prop] = clone({}, source[prop])
: destination[prop] = number
;
}
return destination;
}({}, JSON.parse(_super)));
}
}
document.body.innerHTML = JSON.stringify(FINAL, null, 4);
So, i'm looping over the SUPER two times, one inside the other to test each object, and if i find equal strings, i increase the number by one and remove the object from the array, then i use this script to assign the number to the innermost property of the object:
if (number) {
FINAL.push(function clone(destination, source) {
destination = destination || {};
for (var prop in source) {
typeof source[prop] === 'object' && source[prop] !== null && source[prop]
? destination[prop] = clone({}, source[prop])
: destination[prop] = number
;
}
return destination;
}({}, JSON.parse(_super)));
}
But isn't working properly because of a conflict in this line:
if (_super === current) {
SUPER.splice(ii, 1);
number++;
}
I'm messing up the loop. Any ideas how to fix? I'm open to suggestions, i don't know if there is a better way to achieve this, i hope someone knows.
Thanks.
When you splice the array, elements are shifted to left by 1 but the loop index continues to move forward not accounting for this shift. You can account for it by decrementing the current loop index by 1 each time you splice.
e.g: t = [1,2,3,4];
console.log(t[1]); // output: 2
//when you splice
t.splice(1,1);
// after splicing the array elements are shifted
console.log(t[1]); //output: 3
Working solution:
let SUPER = [{
"NAME1": {
"12": {
"10": 1
}
}
}, {
"NAME1": {
"12": {
"10": 1
}
}
}, {
"NAME1": {
"12": {
"10": 1
}
}
}, {
"NAME1": {
"12": {
"10": 1
}
}
}],
FINAL = [];
for (let i = 0; i < SUPER.length; i++) {
let _super = JSON.stringify(SUPER[i]),
ii = i + 1,
ll = SUPER.length,
number = 1;
for (ii; ii < ll; ii++) {
let current = JSON.stringify(SUPER[ii]);
if (_super === current) {
SUPER.splice(ii, 1);
ii--;
number++;
}
}
if (number) {
FINAL.push(function clone(destination, source) {
destination = destination || {};
for (var prop in source) {
typeof source[prop] === 'object' && source[prop] !== null && source[prop] ? destination[prop] = clone({}, source[prop]) : destination[prop] = number;
}
return destination;
}({}, JSON.parse(_super)));
}
}
document.body.innerHTML = JSON.stringify(FINAL, null, 4);
I have also initialized ii, number to 1 to reduce one iteration of inner loop.
这篇关于确认在阵列的对象的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!