0

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.

3
  • Note, I updated to include var i = 0; per a user's comment. Thanks to that user, but still getting no output. jsfiddle.net/FgdSj/1 Commented Nov 18, 2013 at 18:32
  • 3
    You never call map() in your fiddle Commented Nov 18, 2013 at 18:39
  • As an aside - I meant to originally create this - jsfiddle.net/9U2GK/1 Commented Nov 18, 2013 at 20:00

4 Answers 4

3

There 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.net/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.net/FgdSj/5/

Sign up to request clarification or add additional context in comments.

Comments

1
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

2 Comments

Thanks, but I'm still getting no output in console - jsfiddle.net/FgdSj/2.
@KevinMeredith: You are aware you never actually call map(), right?
1

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}];

1 Comment

arr.prototype doesn't exist.
0
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.