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
c8a887af
Commit
c8a887af
authored
Feb 27, 2011
by
Jordan Boesch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bug 2616; Adding object support to jQuery.map
parent
71bd828d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
15 deletions
+27
-15
core.js
src/core.js
+22
-8
core.js
test/unit/core.js
+5
-7
No files found.
src/core.js
View file @
c8a887af
...
@@ -712,15 +712,29 @@ jQuery.extend({
...
@@ -712,15 +712,29 @@ 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
;
var
ret
=
[],
value
,
// Go through the array, translating each of the items to their
length
=
elems
.
length
,
// new value (or values).
// same object detection used in jQuery.each, not full-proof but very speedy.
for
(
var
i
=
0
,
length
=
elems
.
length
;
i
<
length
;
i
++
)
{
isObj
=
length
===
undefined
;
value
=
callback
(
elems
[
i
],
i
,
arg
);
if
(
isObj
)
{
for
(
key
in
elems
)
{
value
=
callback
(
elems
[
key
],
key
,
arg
);
if
(
value
!=
null
)
{
ret
[
ret
.
length
]
=
value
;
}
}
}
else
{
// Go through the array, translating each of the items to their
// new value (or values).
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
value
=
callback
(
elems
[
i
],
i
,
arg
);
if
(
value
!=
null
)
{
if
(
value
!=
null
)
{
ret
[
ret
.
length
]
=
value
;
ret
[
ret
.
length
]
=
value
;
}
}
}
}
}
...
...
test/unit/core.js
View file @
c8a887af
...
@@ -608,7 +608,7 @@ test("first()/last()", function() {
...
@@ -608,7 +608,7 @@ test("first()/last()", function() {
});
});
test
(
"map()"
,
function
()
{
test
(
"map()"
,
function
()
{
expect
(
2
);
//expect(
6);
expect
(
6
);
same
(
same
(
jQuery
(
"#ap"
).
map
(
function
(){
jQuery
(
"#ap"
).
map
(
function
(){
...
@@ -625,26 +625,24 @@ test("map()", function() {
...
@@ -625,26 +625,24 @@ test("map()", function() {
q
(
"ap"
,
"ap"
,
"ap"
),
q
(
"ap"
,
"ap"
,
"ap"
),
"Single Map"
"Single Map"
);
);
return
;
//these haven't been accepted yet
//for #2616
//for #2616
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"
);
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
;
}
,
{
length
:
0
}
);
});
equals
(
mapped
.
length
,
scripts
.
length
,
"Map an array(-like) to a hash"
);
equals
(
mapped
.
length
,
scripts
.
length
,
"Map an array(-like) to a hash"
);
...
...
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