Js获取 数组或对象 的长度
来源:互联网 发布:apache modwsgi 编辑:程序博客网 时间:2024/05/16 13:00
Javascript 计算Object的长度
在我们日常开发中,对象的使用频率很高,我们计算数组的长度是非常方便的,但是如何计算对象的长度呢?
假如我们有一个图书馆的项目,项目中有一组图书和作者,像下面这样:
1
var
bookAuthors = {
2
"Farmer Giles of Ham"
:
"J.R.R. Tolkien"
,
3
"Out of the Silent Planet"
:
"C.S. Lewis"
,
4
"The Place of the Lion"
:
"Charles Williams"
,
5
"Poetic Diction"
:
"Owen Barfield"
6
};
我们分析现在的需求,我们给一个API发送数据,但是书的长度不能超过100,因此我们需要在发送数据之前计算在一个对象中总共有多少本书。那么我们总怎么做呢?我们可能会这样做:
01
function
countProperties (obj) {
02
var
count = 0;
03
04
for
(
var
property
in
obj) {
05
if
(Object.prototype.hasOwnProperty.call(obj, property)) {
06
count++;
07
}
08
}
09
10
return
count;
11
}
12
13
var
bookCount = countProperties(bookAuthors);
14
15
// Outputs: 4
16
console.log(bookCount);
这是可以实现的,幸运的是Javascript提供了一个更改的方法来计算对象的长度:
01
var
bookAuthors = {
02
"Farmer Giles of Ham"
:
"J.R.R. Tolkien"
,
03
"Out of the Silent Planet"
:
"C.S. Lewis"
,
04
"The Place of the Lion"
:
"Charles Williams"
,
05
"Poetic Diction"
:
"Owen Barfield"
06
};
07
08
var
arr = Object.keys(bookAuthors);
09
10
//Outputs: Array [ "Farmer Giles of Ham", "Out of the Silent Planet", "The Place of the Lion", "Poetic Diction" ]
11
console.log(arr);
12
13
//Outputs: 4
14
console.log(arr.length);
下面我们来对数组使用keys方法:
1
var
arr = [
"zuojj"
,
"benjamin"
,
"www.zuojj.com"
];
2
3
//Outputs: ["0", "1", "2"]
4
console.log(Object.keys(arr));
5
6
//Outputs: 3
7
console.log(arr.length);
Object.keys() 方法会返回一个由给定对象的所有可枚举自身属性的属性名组成的数组,数组中属性名的排列顺序和使用for-in循环遍历该对象时返回的顺序一致(两者的主要区别是 for-in 还会遍历出一个对象从其原型链上继承到的可枚举属性)。方法的兼容性及详情请戳这里。
Object.keys()方法,只能使用在现代浏览器,IE8及以下是不支持的,如果想支持IE低版本,可以使用es5-shim来兼容。其中代码如下:
01
// ES5 15.2.3.14
02
// http://es5.github.com/#x15.2.3.14
03
04
// http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
05
var
hasDontEnumBug = !({
'toString'
:
null
}).propertyIsEnumerable(
'toString'
),
06
hasProtoEnumBug = (
function
() {}).propertyIsEnumerable(
'prototype'
),
07
dontEnums = [
08
"toString"
,
09
"toLocaleString"
,
10
"valueOf"
,
11
"hasOwnProperty"
,
12
"isPrototypeOf"
,
13
"propertyIsEnumerable"
,
14
"constructor"
15
],
16
dontEnumsLength = dontEnums.length;
17
18
defineProperties(Object, {
19
keys:
function
keys(object) {
20
var
isFn = isFunction(object),
21
isArgs = isArguments(object),
22
isObject = object !==
null
&&
typeof
object ===
'object'
,
23
isStr = isObject && isString(object);
24
25
if
(!isObject && !isFn && !isArgs) {
26
throw
new
TypeError(
"Object.keys called on a non-object"
);
27
}
28
29
var
theKeys = [];
30
var
skipProto = hasProtoEnumBug && isFn;
31
if
(isStr || isArgs) {
32
for
(
var
i = 0; i < object.length; ++i) {
33
theKeys.push(String(i));
34
}
35
}
else
{
36
for
(
var
name
in
object) {
37
if
(!(skipProto && name ===
'prototype'
) && owns(object, name)) {
38
theKeys.push(String(name));
39
}
40
}
41
}
42
43
if
(hasDontEnumBug) {
44
var
ctor = object.constructor,
45
skipConstructor = ctor && ctor.prototype === object;
46
for
(
var
j = 0; j < dontEnumsLength; j++) {
47
var
dontEnum = dontEnums[j];
48
if
(!(skipConstructor && dontEnum ===
'constructor'
) && owns(object, dontEnum)) {
49
theKeys.push(dontEnum);
50
}
51
}
52
}
53
return
theKeys;
54
}
55
});
56
57
var
keysWorksWithArguments = Object.keys && (
function
() {
58
// Safari 5.0 bug
59
return
Object.keys(arguments).length === 2;
60
}(1, 2));
61
var
originalKeys = Object.keys;
62
defineProperties(Object, {
63
keys:
function
keys(object) {
64
if
(isArguments(object)) {
65
return
originalKeys(ArrayPrototype.slice.call(object));
66
}
else
{
67
return
originalKeys(object);
68
}
69
}
70
}, !keysWorksWithArguments);
0 0
- Js获取 数组或对象 的长度
- JS对象数组的复制和获取长度
- js-获取JSON数组的长度
- js-获取JSON数组的长度
- js如何获取json数组的长度
- 遍历js数组对象的长度
- JS中获取map对象的长度
- js对象长度与js数组长度
- System.Length - 获取字符串或数组的长度
- c/c++中或获取数组长度的函数
- js/javascript获取数组长度
- js数组长度获取问题?
- 获取数组的长度
- 数组长度的获取
- C或C++获取数组长度
- js获取json数据Object对象的长度
- js克隆对象或数组
- js或者vue获取object数组长度
- jeeCmsV7-src 源码解析之八(后台导入模板与资源文件)
- (14)HTML5-Dom控制
- 用对象的思维了解使用Mybatis框架
- memcached
- Mac为nginx安装nginx-sticky-module
- Js获取 数组或对象 的长度
- maven项目部署到tomcat上
- NoSQL Manager for MongoDB 破解
- 提高影响力:职权+威信
- 配置tomcat使用https协议
- BZOJ 2595: [Wc2008]游览计划 斯坦纳树
- Qt4/Qt5 qDebug输出到文件
- php从memcache读取数据再批量写入mysql的方法
- iOS的本地通知tishikuang