JavaScript过滤数组中重复元素
JavaScript过滤数组中重复元素
我是个JS初学者,我即将要说的这个方法也是大部分人都能想到的:
从旧数组中取元素,一个个添加到新数组中,在添加的时候,与添加过的元素比较,如果相同,则不添加。
首先定义两个数组:
Code
var arrA = new Array(1,23,43,64,1,23,5,8,3,5,9);
var arrB = getNewArray(arrA); // 过滤函数
然后开始编写函数getNewArray()
Code
function getNewArray(receiveArray)
{
var arrResult = new Array(); //定义一个返回结果数组.
for (var i=0; i<receiveArray.length; i++)
{
if (/*在这里做i元素与所有判断相同与否, 调用函数check(arrResult, receiveArray[i]);*/)
{
arrResult.push(receiveArray[i]); // 添加该元素到新数组。如果if内判断为false(即已添加过),则不添加。
}
}
return arrResult;
}
然后编写判断函数check()
Code
function check(receiveArray, checkItem)
{
var index = -1; // 函数返回值用于布尔判断
for (var i=0; i<receiveArray.length; i++)
{
if (receiveArray[i]==checkItem)
{
index = i;
break;
}
}
return index;
}
则在函数getNewArray()中的if应该写成:
Code
chec
相关文档:
Javascript Closures
Introduction
The Resolution of Property Names on Objects
Assignment of Values
Reading of Values
Identifier Resolution, Execution Contexts and Scope Chains
The Execution Context
Scope chains and [[scope]]
Identifier Resolution
Closures
Automatic Garbage Collecti ......
转载理由:短小精悍。
来源:互联网
一 Undefined 类型
只有一种值 undefined
以下三种情况typeof 返回类型为undefined
1. 当变量未初始化时
2. 变量未定义时
3. 函数无明确返回值时(函数没有返回值时返回的都是undefined)
undefined 并不等同于未定义的值
typeof 并不真正区分是否是未定义
看以下示例 ......
代码示例
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<mce:style><!--
.man{ float:left; background:#9af; margin:3px; padding:3px; cursor:default}
--></mce:style>< ......
以下全是个人理解以及网上查找而来,如有不对请指正...
假如有n段js代码 用<script>标签隔开的.
运行顺序是
step1. 读入第一个代码段
step2. 做语法分析,有错则报语法错误(比如括号不匹配等),并跳转到step5
step3. 对var变量和function定义做“预解析”(永远不会报错的,因为只解析正确的声明)
......