简体   繁体   English

如何迭代和比较 JS Object 中的值?

[英]How do I iterate over and compare values in a JS Object?

The result should be a 2 element array with the 2 number that are most frequent;结果应该是一个 2 元素数组,其中 2 个数字最频繁; I can return the top 2 most amount of times a number has been spotted (my //comments).我可以返回发现数字的次数最多的前 2 次(我的//评论)。 But I can not figure out how to return the correct keys from the pairs object that appear the most times.但我不知道如何从出现次数最多的 object 对中返回正确的密钥。 Please help.请帮忙。

 /* majorityElementTopTwo * Write a function which accept an array of integers and returns in a new two item array * the two integers in the input that appear most frequently. * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1] */ function majorityElementTopTwo(array) { let result = []; let pairs = {}; for (let i = 0; i < array.length; i++) { if (array[i] in pairs) { pairs[array[i]] += 1; } else { pairs[array[i]] = 1; } } return pairs; // obj.sort(); // for(let i = obj.length; i > 0; i--){ // let num = obj.pop(); // console.log(num); // result.push(num); // } // return result.slice(0,2); } console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4])); // [4,1]

I hope this is the answer you are looking for what I have done here is我希望这是您正在寻找的答案 我在这里所做的是

  1. first map all the entries as an array ie: [["1",3],["2",1],["3",2],["4",4]]首先 map 将所有条目作为一个数组,即: [["1",3],["2",1],["3",2],["4",4]]
  2. Then sort it based upon the second element which is number of times an element has occured然后根据第二个元素对其进行排序,第二个元素是元素出现的次数
  3. Then slice the first two elements that are most occurred [["4",4],["1",3]]然后切片出现次数最多的前两个元素[["4",4],["1",3]]

 /* majorityElementTopTwo * Write a function which accept an array of integers and returns in a new two item array * the two integers in the input that appear most frequently. * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1] */ function majorityElementTopTwo(array) { let pairs = {}; for (let i = 0; i < array.length; i++) { if (array[i] in pairs) { pairs[array[i]] += 1; } else { pairs[array[i]] = 1; } } return Object.entries(pairs).map((elements) => elements).sort((a,b) => b[1]-a[1]).slice(0,2) } console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4])); // [4,1]

As of JavaScript you can use Object.keys(obj) to get an Array of properties defined on the object itself (the ones that return true for obj.hasOwnProperty(key)).从 JavaScript 开始,您可以使用 Object.keys(obj) 来获取在 object 本身(返回 truekeywn)obj.ha 的属性数组。

Object.keys(obj).forEach(function(key,index) {
    // key: the name of the object key
    // index: the ordinal position of the key within the object 
});

This is better (and more readable) than using a for-in loop.这比使用 for-in 循环更好(并且更具可读性)。

Its supported on these browsers:它在这些浏览器上受支持:

Firefox (Gecko): 4 (2.0) Chrome: 5 Internet Explorer: 9 Firefox (Gecko):4 (2.0) Chrome:5 Internet Explorer:9

ref: Iterate through object properties参考: 遍历 object 属性

Using Array.prototype.map(),Array.prototype.filter() and Array.prototype.sort().使用Array.prototype.map(),Array.prototype.filter() and Array.prototype.sort().

 /* majorityElementTopTwo * Write a function which accept an array of integers and returns in a new two item array * the two integers in the input that appear most frequently. * majorityElementTopTwo([3,3,1,2,1,1,4,4,4,4]); // [4,1] */ function majorityElementTopTwo(array) { const [first, second] = [...new Set(array)].map((num) => [num, array.filter((n) => n === num).length]).sort((a, b) => b[1] - a[1]).slice(0, 2); return [first[0], second[0]]; } console.log(majorityElementTopTwo([3, 3, 1, 2, 1, 1, 4, 4, 4, 4]));

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

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