GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

No menu could be recovered. Follow the blue links in the page.

This view

  • Original page
  • New address

GeezerVison is showing zh.javascript.info — 过滤数组中的唯一元素

  • 教程
  • JavaScript 编程语言
  • 数据类型
  • Map and Set(映射和集合)
回到课程

过滤数组中的唯一元素

重要程度: 5

定义 arr 为一个数组。

创建一个函数 unique(arr),该函数返回一个由 arr 中所有唯一元素所组成的数组。

例如:

function unique(arr) {
  /* 你的代码 */
}

let values = ["Hare", "Krishna", "Hare", "Krishna",
  "Krishna", "Krishna", "Hare", "Hare", ":-O"
];

alert( unique(values) ); // Hare, Krishna, :-O

P.S. 这里用到了 string 类型,但其实可以是任何类型的值。

P.S. 使用 Set 来存储唯一值。

打开带有测试的沙箱。

function unique(arr) {
  return Array.from(new Set(arr));
}

使用沙箱的测试功能打开解决方案。

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.