Looking at the map
method in JavaScript, what am I doing wrong here?
// Input: [ { name: "Kevin"}, { name: "Bob" } ]
// Output: [ { "Kevin" :0 }, { "Bob": 1 } ]
var map = function(arr, property) {
var i = 0;
var m = arr.prototype.map(makeKv);
// Input: { name: "Kevin" }
// Output: { "Kevin" = i } // GLOBAL
function makeKv(item) {
return {
item: i++
};
};
console.log("m : " + m);
};
JSFiddle
Please also help me get rid of the global, too.
Looking at the map
method in JavaScript, what am I doing wrong here?
// Input: [ { name: "Kevin"}, { name: "Bob" } ]
// Output: [ { "Kevin" :0 }, { "Bob": 1 } ]
var map = function(arr, property) {
var i = 0;
var m = arr.prototype.map(makeKv);
// Input: { name: "Kevin" }
// Output: { "Kevin" = i } // GLOBAL
function makeKv(item) {
return {
item: i++
};
};
console.log("m : " + m);
};
JSFiddle
Please also help me get rid of the global, too.
Share Improve this question edited Mar 6, 2022 at 22:19 Sebastian Simon 19.5k8 gold badges61 silver badges84 bronze badges asked Nov 18, 2013 at 18:29 Kevin MeredithKevin Meredith 41.9k67 gold badges220 silver badges390 bronze badges 3-
Note, I updated to include
var i = 0;
per a user's ment. Thanks to that user, but still getting no output. jsfiddle/FgdSj/1 – Kevin Meredith Commented Nov 18, 2013 at 18:32 - 3 You never call map() in your fiddle – carter Commented Nov 18, 2013 at 18:39
- As an aside - I meant to originally create this - jsfiddle/9U2GK/1 – Kevin Meredith Commented Nov 18, 2013 at 20:00
4 Answers
Reset to default 3There are a few issues here:
First,
var m = arr.prototype.map(makeKv);
You don't need prototype
here. You only use that when you are using the constructor, like Array.prototype.map
. Here, you just need to do arr.map
.
Second,
function makeKv(item) {
return {item: i++};
};
You never declare i
anywhere. How can you add one to something that doesn't exist. You need to have var i = 0;
before this.
Finally, return {item: i++};
will make a key called literally "item"
. You need to declare the object first (var ret = {};
), then use [item]
to set the value.
Array.map
's callback is passed the element in the array as the 1st parameter, so item
will be an object. You need to do item[property]
to get the value you want.
P.S. Don't do "m : " + m
in your console.log
, that will concat strings, thus converting m
to a string. Use ,
instead: console.log("m : ", m);
So, all together, try:
var map = function(arr, property) {
var i = 0;
var m = arr.map(makeKv);
function makeKv(item) {
var ret = {};
ret[item[property]] = i++;
return ret;
};
console.log("m : ", m);
}
DEMO: http://jsfiddle/FgdSj/3/
EDIT: Array.map
's callback is passed the index in the array as the 2nd parameter, so var i = 0;
isn't needed here:
var map = function(arr, property) {
var m = arr.map(makeKv);
function makeKv(item, index) {
var ret = {};
ret[item[property]] = index;
return ret;
};
console.log("m : ", m);
}
DEMO: http://jsfiddle/FgdSj/5/
arr.prototype.map(makeKv);
should be
arr.map(makeKv);
Now you have another issue since it will return
[ { item : 0}, { item : 1} ]
If you change the mapped function to
function makeKv(item) {
var x = {}
x[item.name] = i++;
return x;
};
it would give you what you want.
JSFiddle
Just call .map
directly
arr.map(makeKv)
I, for whatever reason (maybe map
is overridden), you want to use the Array.prototype
's method
[].map.call(arr, makeKv);
Here's, it's all fixed up for you to match your desired output
// input: [{name: "Kevin"}, {name: "Bob"}], "name"
var map = function(arr, property) {
var i = 0;
function makeKv(item) {
var obj = {};
obj[item[property] = i++;
return obj;
};
return arr.map(makeKv);
}
var result = map([{name: "Kevin"}, {name: "Bob"}], "name");
console.log(result);
// [{"Kevin" : 0}, {"Bob" : 1}];
var map = function(arr, property) {
var i = 0;
var m = Array.prototype.map(makeKv);
// input: {name: "Kevin"}
// output: "Kevin" = i // GLOBAL
function makeKv(item) {
return {item: i++};
};
console.log("m : " + m);
return m;
}
map();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742315471a4420750.html
评论列表(0条)