views:

296

answers:

5

I have two arrays of objects. I would like iterate over each of these two arrays and add a property ("IsPriced"). Then I would like to combine the two arrays into one array.

How can I do this in JavaScript (with MooTools is fine, but not with jQuery please)?

I really don't know where to begin.

+3  A: 

You could combine the two arrays first and then iterate only the combined one:

// assuming that your arrays are called array1 and array2:
var combined = array1.concat(array2);

var n = combined.length;
while(n--) {
  combined[n].isPriced = true; // or maybe call a getIsPriced function 
}

I used a reverse loop since you only need to add a property to all the elements, you don't really care about the order of iteration.

CMS
+1 Nice, super succinct. Love it!
Doug Neiner
Thank you Doug!
CMS
A: 
for (key in myArray1)
  myArray1[key].isPrice = 123;
for (key in myArray2)
  myArray2[key].isPrice = 123;
result = myArray1.concat(myArray2);
Scott Evernden
for-in is used to iterate all attributes of a particular object, for array this will include the .length property since length is not object adding isPrice attribute is not allowed, for iterating array elements for-in should not be used
jerjer
@jerjer: You're right, the `for...in` statement should be *always* avoided when iterating arrays. About `length`, in most implementations this property is not enumerable and it will not be iterated (`[].propertyIsEnumerable('length') == false`), but another critical issue that this code has, is that the `key` variable becomes global (`window.key` is created)...
CMS
A: 
var totalArr = new Array();


for (var i = 0; i < arr1.length; i++)
{
    arr1[i].isPriced = {};
    totalArr.push(arr1[i]);
}

for (var i = 0; i < arr2.length; i++)
{
    arr2[i].isPriced = {};
    totalArr.push(arr1[i]);
}
Rodrick Chapman
+1  A: 

You can use concat to combine the arrays, and each (from MooTools) to apply a function to each item in the combined array.

var first = [{a: "something"}, {b: "or other"}];
var second = [{d: "string"}, {e: "last object"}];

var combined = first.concat(second);
combined.each(function (item) {
  item.IsPriced = 10;
});

each is defined by MooTools, so if you're already using MooTools, you might as well use that. ECMAScript now provides a forEach method that does the same thing, but that might not be available on all browsers. If you'd rather use the standard method, the following definition should add it to browsers that don't already support it (from the MDC article, licensed under the MIT license):

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}
Brian Campbell
A: 

To combine two arrays a1 and a2 you can use

var combined = a1.concat(a2);

This creates a new array combining a1 and a2. If you want to append the contents of a2 to a1 you can use the method described in this StackOverflow post:

a1.push.apply(a1, a2);

To add a new property to each new element in plain JavaScript it is best to use this approach:

combined.map(function(item){item.newProperty = "someValue";});

This avoids iterating over the array manually. Note however that Array.filter()was introduced in JavaScript 1.6.

MKroehnert