0

I have rewritten a version of each below. In my output I want it to log

[['motorbike', 0, vehicles],[['car', 1, vehicles]],[['plane', 2, vehicles]]]

However it currently logs the entire contents of the array rather than just the title. How can I get it to provide the the list name instead?

function each(collection, callback) {
    if (Array.isArray(collection)) {
        for (var i = 0; i < collection.length; i++) {
            callback(collection[i], i, collection);
        }
    } else {
        return collection;
    }
}
var vehicles = ['motorbike', 'car', 'plane'];
var iterationInputs = [];
each(vehicles, function(vehicle, index, list) {
    iterationInputs.push([vehicle, index, list]);
});

console.log(iterationInputs);
Sender
  • 6,660
  • 12
  • 47
  • 66
Jessica O'Brien
  • 155
  • 1
  • 11
  • There is no "list name"..... Do you want an object and not an array? Maybe I have no clue what you are asking... what are you expecting the log to loo like?> – epascarello Jul 23 '15 at 02:25
  • I want it to log out `[['motorbike', 0, vehicles],[['car', 1, vehicles]],[['plane', 2, vehicles]]]` but instead it is logging out `[["motorbike", 0, ["motorbike", "car", "plane"]],["car", 1, ["motorbike", "car", "plane"],["plane", 3, ["motorbike", "car", "plane"]` – Jessica O'Brien Jul 23 '15 at 02:48

1 Answers1

0

You can't access variable names I think you need to use hash tables ,, this link can help you http://www.mojavelinux.com/articles/javascript_hashes.html

Ahmed Eid
  • 1,145
  • 8
  • 9