You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

面向对象编程新手求助:数组去重函数的原理及排序疑问

Hey there! Let's unpack this code and your questions one by one—this is a common gotcha for folks new to JS and object patterns, so you're not alone here.

First, let's fix up the incomplete code snippet you shared to make it clear:

function duplicate(arr) {
  var i, len = arr.length, obj = {};
  for (i = 0; i < len; i += 1) {
    obj[arr[i]] = 0;
  }
  return obj;
}

// Example call
console.log(duplicate([1, 2, 2, 3, 3, 3])); 
// Output: {1: 0, 2: 0, 3: 0}

Now let's tackle your three questions:

1. How does the deduplication actually work?

The core trick here relies on a fundamental rule of JavaScript objects: object keys are unique. You can't have two identical keys in a single object.

Here's the play-by-play:

  • We start with an empty object obj.
  • We loop through every element in the input array arr.
  • For each element, we set obj[arr[i]] = 0. If the element is new (hasn't been processed before), this adds a new key-value pair to the object. If the element is a duplicate, this just overwrites the existing key's value (but since we're setting it to 0 every time, nothing really changes—we just don't add a new key).
  • By the end of the loop, the object only contains unique keys, which correspond to the unique elements from the original array.

2. Where do the duplicate values go?

Short answer: They don't "go" anywhere—they're just ignored.

When the loop hits a duplicate element, it tries to create a key that already exists in the object. Since JavaScript won't allow duplicate keys, this operation just updates the value of the existing key (in this case, setting it to 0 again, which is redundant). The duplicate element never gets stored in the object at all—it's effectively discarded because there's no way to represent it as a unique key.

3. What's the sorting logic here?

Wait a second—this function returns an object, not an array! The "sorting" you might be seeing is actually a behavior of JavaScript objects themselves, not something the function does intentionally.

Here's how object key ordering works in modern JS:

  • Integer keys (like numbers 1, 2, 3) are automatically sorted in ascending numerical order, regardless of their original order in the input array. For example, if you pass [3, 1, 2], the returned object will have keys 1, 2, 3.
  • Non-integer keys (like strings, booleans, or floats that aren't whole numbers) are kept in the order they were first added to the object. For example, ['b', 'a', true] would return {b: 0, a: 0, true: 0}.

Important note: If you wanted a sorted array of unique values, this function isn't the best tool—you'd need to convert the object keys back to an array (using Object.keys(obj)) and then sort it explicitly if needed. For example:

const uniqueArr = Object.keys(duplicate([3,1,2])).map(Number);
// uniqueArr is [1,2,3]

Hope that clears up all your confusion! 😊

内容的提问来源于stack exchange,提问作者Ranjith Reddy Keesari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:22:56