0

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.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
JamesBrownIsDead
  • 4,655
  • 6
  • 30
  • 29
  • What do you mean by "add a property"? Add a property to each element of the array? – Brian Campbell Jan 07 '10 at 05:25
  • Yes. They elements are objects. So if the current object is 'foobar', the code would be foobar.IsPriced = ... – JamesBrownIsDead Jan 07 '10 at 05:27
  • to add a property in javascript, you just...well... add a property. just declare your `foobar.IsPriced = yourValue` and so it shall be. – Jason Jan 07 '10 at 05:30
  • how do you want to combine the arrays? do you want to append one to the other? put them in some sort of order? get rid of dupes? MORE INFO PLZ – Jason Jan 07 '10 at 05:31

5 Answers5

4

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.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

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
  • 322,767
  • 57
  • 360
  • 340
1

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.

Community
  • 1
  • 1
MKroehnert
  • 3,637
  • 2
  • 34
  • 43
0
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
  • 5,437
  • 2
  • 31
  • 32
-1
for (key in myArray1)
  myArray1[key].isPrice = 123;
for (key in myArray2)
  myArray2[key].isPrice = 123;
result = myArray1.concat(myArray2);
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
  • 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 Jan 07 '10 at 05:39
  • @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)... – Christian C. Salvadó Jan 07 '10 at 05:52