lovesh
lovesh

Reputation: 5401

store references to arrays in an array

I have a few arrays like this

var arr1 = ['name1', 'name2', 'name3']   //array of strings
var arr2 = ['name4', 'name5', 'name6']
var arr3 = [....]

Now i wanted to convert each of the above array of strings into array of objects like

arr1 = [{name: 'name1'}, {name:'name2'}, {name:'name3'}] 

Similarly for the other 2 arrays

So i came up with this

var bigArray = [arr1, arr2, arr3]
bigArray.forEach(function(arr, index, bigArray) {
     bigArray[index] = arr.map(function(item) {       // here i am assigning to current element of bigArray
     return {
           name: item
           };
     });
});

As you see while doing a forEach on bigArray and i modify each of its array(arr1, arr2,...) through a map.

After this forEach, bigArray looks like

[
   [{name: 'name1'}, {name:'name2'}, {name:'name3'}],
   [{name: 'name4'}, {name:'name5'}, {name:'name6'}],
   ....
]

but arr1 is still ['name1', 'name2', 'name3'] and arr2 is still ['name4', 'name5', 'name6']

What i understand here is that bigArray does not contain references to arr1, arr2, .... Any idea how can i pass references? Or am i missing something else?

EDIT: I guess i couldnt explain myself clearly, so here it is-------- The reason i create bigArray is that i have too many of arr1, arr2, ... so i dont want to go and apply map over each of these individually so i put them i a big array called bigArray and do a forEach on them

Upvotes: 0

Views: 86

Answers (3)

Adam Rackis
Adam Rackis

Reputation: 83358

The map method is what you want; this creates a new array, which is why you're seeing the results you are.

var arr1 = ['name1', 'name2', 'name3']; 

var arrNames = arr1.map(function(nm){ return { name: nm }; });

Here's a DEMO

Again, map will not modify the array in question, that's why arr1 is not changed in your example. It simply maps the array to a new array, and returns that.

If you want to change the array in question, you need to do something like this:

var arr1 = ['name1', 'name2', 'name3'];
arr1 = arr1.map(function(nm){ return { name: nm }; });

var arr2 ....

And then to have bigArray contain references to arr1, arr2, and arr3, you'd just create bigArray containing references to these arrays after they've been properly defined.

var bigArray = [arr1, arr2, arr3]

Last edit

If you really just want to save some key strokes, you could do something like this:

var arr1 = mapThis(['name1', 'name2', 'name3']);
var arr2 = mapThis(...
//and so on

function mapThis(arr){
    return arr.map(function(nm){ return { name: nm }; });
}

That'll create all your arrX arrays correctly from the start without too many extra key strokes.

Upvotes: 2

L0j1k
L0j1k

Reputation: 12625

According to documentation at MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map), the map function Creates a new array with the results of calling a provided function on every element in this array. You cannot operate on the original array using map() by itself, so you will need to assign the result of map() to arr1 or another array and use that array where you need the change to be.

You can do something like this, without map() to push a ton of arrays:

var bigArray = [arr1, arr2, arr3];
var bigArrayFinal = [];
var nextArray;
while(nextArray = bigArray.slice()) {
  var thisArray = [];
  var nextElement;
  while(nextElement = bigArray.slice()) {
    thisArray.push({"name": nextElement});
  }
  bigArrayFinal.push(thisArray);
}

Upvotes: 0

ketan
ketan

Reputation: 390

solution without map. It will work in all browser.

var arr1 = ['name1', 'name2', 'name3'];
var arr2 = [], i;

while(i = arr1.shift()) {
    arr2.push({"name": i});
}
arr1 = arr2;

Upvotes: 0

Related Questions