简体   繁体   English

确认数组中对象出现的次数

[英]Confirm number of occurrences of a objects in array

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: 因此,我要遍历SUPER两次,一次在另一个内进行测试,以测试每个对象,如果我发现相等的字符串,则将数字加1并从数组中删除该对象,然后使用此脚本来分配对象最内部属性的编号:

  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. 当您对数组进行拼接时,元素将向左移动1,但循环索引会继续向前移动,而不考虑此移动。 You can account for it by decrementing the current loop index by 1 each time you splice. 您可以通过在每次拼接时将当前循环索引递减1来解决此问题。

eg: t = [1,2,3,4]; 例如:t = [1,2,3,4];

console.log(t[1]); 的console.log(T [1]); // output: 2 //输出:2

//when you splice //当你拼接时

t.splice(1,1); t.splice(1,1);

// after splicing the array elements are shifted //拼接后将数组元素移位

console.log(t[1]); 的console.log(T [1]); //output: 3 //输出: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. 我还初始化了ii,编号为1以减少内部循环的一次迭代。

尝试编写自己的equals函数并查找数组中那些事件的出现。

If you are the one to set the "dynamically" changing rules then to perform this job is very easy by an invention of two Object methods; 如果您是设置“动态”更改规则的人,那么通过发明两个Object方法就很容易执行此工作。 namely Object.prototype.getNestedValue() and Object.prototype.setNestedValue() . Object.prototype.getNestedValue()Object.prototype.setNestedValue() Lets do it. 我们开始做吧。 Both methods take a number of arguments which are the object properties (if the argument is "string" type) or array indices(if the argument is "number" type). 两种方法都使用多个参数,这些参数是对象属性(如果参数是"string"类型)或数组索引(如果参数是"number"类型)。 In addition to that, in the Object.setNestedValues() the very last argument is the value to be set. 除此之外,在Object.setNestedValues() ,最后一个参数是要设置的值。 Just simple. 很简单。

So here it goes like this; 所以这是这样的:

 Object.prototype.getNestedValue = function(...a) { return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]]; }; Object.prototype.setNestedValue = function(...a) { return a.length > 2 ? typeof this[a[0]] === "object" && this[a[0]] !== null ? this[a[0]].setNestedValue(...a.slice(1)) : (this[a[0]] = typeof a[1] === "string" ? {} : new Array(a[1]), this[a[0]].setNestedValue(...a.slice(1))) : this[a[0]] = a[1]; }; var data = [ '{"NAME1":{"12":{"10":1}}}', '{"NAME1":{"12":{"10":1}}}', '{"NAME1":{"12":{"10":1}}}' ], props = ["NAME1", "12", "10"], // this array is constructed dynamically !! reduced = [data.reduce((p,c) => {var v = p.getNestedValue(...props); p.setNestedValue(...props, !!v ? ++v : 1); return p;},{})]; console.log(JSON.stringify(reduced,null,2)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM