【问题标题】:How do I iterate over and compare values in a JS Object?如何迭代和比较 JS 对象中的值?
【发布时间】:2021-06-10 03:16:11
【问题描述】:

结果应该是一个 2 元素数组,其中 2 个数字最频繁;我可以返回发现数字的次数最多的前 2 次(我的 //cmets)。但我不知道如何从出现次数最多的对对象中返回正确的键。请帮忙。

/* 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]

【问题讨论】:

    标签: javascript arrays object counter frequency


    【解决方案1】:

    使用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]));

    【讨论】:

      【解决方案2】:

      我希望这是你正在寻找的答案我在这里所做的是

      1. 首先将所有条目映射为一个数组,即:[["1",3],["2",1],["3",2],["4",4]]
      2. 然后根据第二个元素(一个元素出现的次数)对其进行排序
      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]

      【讨论】:

        【解决方案3】:

        从 JavaScript 开始,您可以使用 Object.keys(obj) 来获取在对象本身上定义的属性数组(那些为 obj.hasOwnProperty(key) 返回 true 的属性)。

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

        这比使用 for-in 循环更好(并且更具可读性)。

        这些浏览器都支持它:

        火狐(壁虎):4 (2.0) 铬:5 Internet Explorer:9

        参考:Iterate through object properties

        【讨论】:

          猜你喜欢
          • 2021-12-04
          • 2016-02-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-03
          • 2020-07-23
          • 2020-03-31
          • 1970-01-01
          相关资源
          最近更新 更多