1

I want to map the keys of a large array into another key while removing the original key using Underscore.js' map() function.

large_array = _.map(data, function(element) { 
  element.b = element.a;
  delete element.a;
  return element;
});
console.log(large_array) // Returns an array with length == 0

Why would large_array have a length of zero?

I feel like I'm using the delete statement incorrrectly but I'm not sure.

Edit:

I may be abstracting this code too much, since simple runs seem to be working just fine.

The original data array is response from the FB.api('/me/friends', function(response) {...}

More specifically, it is an array of objects such as {id: "12345", name: "Bubba Watson"}

As this is a response from Facebook, each object is guaranteed to have the 'id' attribute

The actual code is changing the the 'id' property to a 'facebook_id' property.

FB.api('/me/friends', function(response) { 
  console.log(response.data);   // Returns 600+ Array of Bubba Watson like objects, each with an id.
  large_array = _.map(response.data, function(element) { 
    element.facebook_id = element.id;
    delete element.id;
    return element;
   });
  console.log(large_array);   // Mysteriously Returns: {length: 0, __proto__: Array[0]}
}
TimeEmit
  • 4,516
  • 4
  • 18
  • 19
  • How did `large_array` look before? – Niko Apr 16 '13 at 16:44
  • console.log(data) returns an array with about 600 objects, each with an 'a' property. – TimeEmit Apr 16 '13 at 16:45
  • It seems that \_.map() is taking away the length property, as console.log(_.map(data, function(element) { return element; })); returns an array with length 0 as well... – TimeEmit Apr 16 '13 at 16:49
  • Could you please post your `data` array (or a minimal, running testcase that shows the issue) – Bergi Apr 16 '13 at 16:54
  • @TimeEmit If we `console.log(large_array)` immediately after `_.map()` what does it output? – Amy Apr 16 '13 at 17:36
  • Maybe the question should not be whether the elements have ids and you deleted them correctly, but whether you have friends (as harsh as that might sound :-)). Can you do a `console.log(response)` and post the results? – Bergi Apr 16 '13 at 17:39
  • I've updated my question to address your queries. However, I'm starting to think that this is a problem with Google chrome's logger as I don't seem to having this problem with Firefox's Firebug. – TimeEmit Apr 16 '13 at 18:12
  • Restarting Google Chrome does not alleviate the issue, but Firebug is showing console.log(large_array) to be a beautiful array of objects with facebook_ids and no ids. – TimeEmit Apr 16 '13 at 18:16
  • Then maybe it's a possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays)… – Bergi Apr 16 '13 at 21:52

1 Answers1

3

You're using delete correctly, however you may need to make sure:

  1. data is indeed type Array and there are elements in data array
  2. each element in the data array has property named a

Your code works for me: http://jsfiddle.net/EJTgx/

var data = [
    { a: 10 },
    { a: 20 },
    { a: 30 }
];

var large_array = _.map(data, function(element) {
   element.b = element.a;
   delete element.a;
   return element;
});

console.log(large_array.length);    // Returns 3
Amy
  • 7,388
  • 2
  • 20
  • 31
  • This is very reassuring, thank you. I've just updated my question to explain that I am confident that all elements in the array have the given property. – TimeEmit Apr 16 '13 at 17:16