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.

4
  • What do you mean by "add a property"? Add a property to each element of the array? Commented Jan 7, 2010 at 5:25
  • Yes. They elements are objects. So if the current object is 'foobar', the code would be foobar.IsPriced = ... Commented Jan 7, 2010 at 5:27
  • to add a property in javascript, you just...well... add a property. just declare your foobar.IsPriced = yourValue and so it shall be. Commented Jan 7, 2010 at 5: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 Commented Jan 7, 2010 at 5:31

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
    }
  };
}

Comments

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.

Comments

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]);
}

Comments

-1
for (key in myArray1)
  myArray1[key].isPrice = 123;
for (key in myArray2)
  myArray2[key].isPrice = 123;
result = myArray1.concat(myArray2);

2 Comments

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: 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)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.