0

array1:

 [{"Attribute1":"Apple","Attribute2":"jacob.nelson@cognizant.com"}]

array2:

[{"Attribute1":"orange"}]`

I want to replace the value of "Attribute1" in array1 to value of "Attribute1" in array2. My output should be like

[{"Attribute1":"orange","Attribute2":"jacob.nelson@cognizant.com"}]

I'm new to javascript.Am stuck here.Any help will be much appreciated.

Pointy
  • 405,095
  • 59
  • 585
  • 614
user2882721
  • 221
  • 1
  • 3
  • 12
  • 4
    If both arrays are defined as above, are you wanting to do this?: array1[0].Attribute1 = array2[0].Attribute1; – David Fleeman Nov 12 '13 at 15:36
  • There's no function that will do this for you. You'll have to loop and compare the objects manually. – jbabey Nov 12 '13 at 15:38
  • In case it's not apparent, you should realize that both of your arrays currently only have one item in it - an object, which contains a set of members. Do you really need an array, or is the object sufficient? (In other words, are you looking for `array1[0].Attribute1 = array2[0].Attribute1` as @DavidFleeman suggested, or do you really just want `obj1.Attribute1 = obj2.Attribute1`?) Frequently in JavaScript, all you need is the dynamic object. – Scott Mermelstein Nov 12 '13 at 15:39
  • Relevant merge thread: http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript – megawac Nov 12 '13 at 15:42
  • Actually i need a array not an object – user2882721 Nov 12 '13 at 15:42
  • What is the big picture of what you're trying to accomplish? – Explosion Pills Nov 12 '13 at 15:46
  • using [Object.extend](https://gist.github.com/rlemon/e42d47195a7f88051c17) you can `arr1 = Object.extend(arr1, arr2)` for your desired results. However this is probably overkill. – rlemon Nov 12 '13 at 15:48
  • @user2882721, didn't your question's title sound like 'comparing and pushing array of OBJECTS in javascript'? Now you say you don't need objects.. – Feanor Nov 12 '13 at 15:55

2 Answers2

0

What you showed us is JSON objects representation.

In this case you have an array of objects, so if you do next:

>>ar=[{"Attribute1":"Apple","Attribute2":"jacob.nelson@cognizant.com"}]
[Object]

This says that you have one object in an array, then yoou have to get it:

>>obj=ar[0]
Object {Attribute1: "Apple", Attribute2: "jacob.nelson@cognizant.com"}

Then if you need to replace something in objects, you have to treat them like OBJECTS!

>>ar2=[{"Attribute1":"orange"}]
>>obj2=ar2[0]
>>obj1.Attribute1=obj2.Attribute1

And that's all!

TIP if you have many objects, loop over them:

>>objects_array=[
        {"Attribute1":"Apple","Attribute2":"jacob.nelson@cognizant.com"}, 
        {"Attribute1":"Cucumber","Attribute2":"asd@qwe.com"}
    ]
[Object, Object]
>>for obj in objects_array {
   obj.Attribute1='Whatever'
}
Feanor
  • 3,568
  • 5
  • 29
  • 49
0

This is probably overkill for this single case, however here it is:

Using Object.extend

// adds Object.extend if it does not already exist
if (typeof Object.extend !== 'function') {
    Object.extend = function (d /* DESTINATION */, s /* SOURCE */) {
        for (var k in s) {
            if (s.hasOwnProperty(k)) {
                var v = s[k];
                if (d.hasOwnProperty(k) && typeof d[k] === "object" && typeof v === "object") {
                    Object.extend(d[k], v);
                } else {
                    d[k] = v;
                }
            }
        }
        return d;
    };
}

you can get the desired results by doing this:

var arr1 = [{"Attribute1":"Apple","Attribute2":"jacob.nelson@cognizant.com"}],
    arr2 = [{"Attribute1":"orange"}];
arr1 = Object.extend(arr1, arr2);
>> [{"Attribute1":"orange","Attribute2":"jacob.nelson@cognizant.com"}]

But like it is mentioned in the comments; if this is the only case, do this by hand.

rlemon
  • 17,518
  • 14
  • 92
  • 123