Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
d832f4f7
Commit
d832f4f7
authored
Mar 21, 2011
by
Dan Heberden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jQuery.map to iterate over objects with a .length property
parent
e38f074d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
25 deletions
+27
-25
core.js
src/core.js
+20
-20
core.js
test/unit/core.js
+7
-5
No files found.
src/core.js
View file @
d832f4f7
...
@@ -706,31 +706,31 @@ jQuery.extend({
...
@@ -706,31 +706,31 @@ jQuery.extend({
// arg is for internal usage only
// arg is for internal usage only
map
:
function
(
elems
,
callback
,
arg
)
{
map
:
function
(
elems
,
callback
,
arg
)
{
var
ret
=
[],
value
,
i
=
0
,
var
value
,
ret
=
[],
i
=
0
,
length
=
elems
.
length
,
length
=
elems
.
length
,
// same object detection used in jQuery.each, not full-proof but very speedy.
// process .length if it's just an object member
isObj
=
length
===
undefined
;
isArray
=
length
!==
undefined
&&
(
elems
[
length
-
1
]
||
jQuery
.
isArray
(
elems
)
)
;
// the work for the loops - run elems[x] through callback
// Go through the array, translating each of the items to their
inLoop
=
function
(
key
)
{
// new value (or values).
value
=
callback
(
elems
[
key
],
key
,
arg
);
if
(
isArray
)
{
for
(
;
i
<
length
;
i
++
)
{
value
=
callback
(
elems
[
i
],
i
,
arg
);
if
(
value
!=
null
)
{
if
(
value
!=
null
)
{
ret
[
ret
.
length
]
=
value
;
ret
[
ret
.
length
]
=
value
;
}
}
}
}
// Go thorugh every key on the object
// Go thorugh every key on the object,
if
(
isObj
)
{
}
else
{
for
(
key
in
elems
)
{
for
(
key
in
elems
)
{
inLoop
(
key
);
value
=
callback
(
elems
[
key
],
key
,
arg
);
}
// Go through the array, translating each of the items to their
if
(
value
!=
null
)
{
// new value (or values).
ret
[
ret
.
length
]
=
value
;
}
else
{
}
for
(
;
i
<
length
;
i
++
)
{
inLoop
(
i
);
}
}
}
}
...
...
test/unit/core.js
View file @
d832f4f7
...
@@ -608,7 +608,7 @@ test("first()/last()", function() {
...
@@ -608,7 +608,7 @@ test("first()/last()", function() {
});
});
test
(
"map()"
,
function
()
{
test
(
"map()"
,
function
()
{
expect
(
6
);
expect
(
7
);
same
(
same
(
jQuery
(
"#ap"
).
map
(
function
(){
jQuery
(
"#ap"
).
map
(
function
(){
...
@@ -630,26 +630,28 @@ test("map()", function() {
...
@@ -630,26 +630,28 @@ test("map()", function() {
var
keys
=
jQuery
.
map
(
{
a
:
1
,
b
:
2
},
function
(
v
,
k
){
var
keys
=
jQuery
.
map
(
{
a
:
1
,
b
:
2
},
function
(
v
,
k
){
return
k
;
return
k
;
});
});
equals
(
keys
.
join
(
""
),
"ab"
,
"Map the keys from a hash to an array"
);
equals
(
keys
.
join
(
""
),
"ab"
,
"Map the keys from a hash to an array"
);
var
values
=
jQuery
.
map
(
{
a
:
1
,
b
:
2
},
function
(
v
,
k
){
var
values
=
jQuery
.
map
(
{
a
:
1
,
b
:
2
},
function
(
v
,
k
){
return
v
;
return
v
;
});
});
equals
(
values
.
join
(
""
),
"12"
,
"Map the values from a hash to an array"
);
equals
(
values
.
join
(
""
),
"12"
,
"Map the values from a hash to an array"
);
// object with length prop
var
values
=
jQuery
.
map
(
{
a
:
1
,
b
:
2
,
length
:
3
},
function
(
v
,
k
){
return
v
;
});
equals
(
values
.
join
(
""
),
"123"
,
"Map the values from a hash with a length property to an array"
);
var
scripts
=
document
.
getElementsByTagName
(
"script"
);
var
scripts
=
document
.
getElementsByTagName
(
"script"
);
var
mapped
=
jQuery
.
map
(
scripts
,
function
(
v
,
k
){
var
mapped
=
jQuery
.
map
(
scripts
,
function
(
v
,
k
){
return
v
;
return
v
;
});
});
equals
(
mapped
.
length
,
scripts
.
length
,
"Map an array(-like) to a hash"
);
equals
(
mapped
.
length
,
scripts
.
length
,
"Map an array(-like) to a hash"
);
var
flat
=
jQuery
.
map
(
Array
(
4
),
function
(
v
,
k
){
var
flat
=
jQuery
.
map
(
Array
(
4
),
function
(
v
,
k
){
return
k
%
2
?
k
:
[
k
,
k
,
k
];
//try mixing array and regular returns
return
k
%
2
?
k
:
[
k
,
k
,
k
];
//try mixing array and regular returns
});
});
equals
(
flat
.
join
(
""
),
"00012223"
,
"try the new flatten technique(#2616)"
);
equals
(
flat
.
join
(
""
),
"00012223"
,
"try the new flatten technique(#2616)"
);
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment