Reputation: 7641
I have a situation where i have array of objects...
array[0] = {key:value,key1:value}
array[1] = {key:value,key1:value}
array[2] = {key:value,key1:value}
array[3] = {key:value,key1:value}
Now i want to associate array[0] to another array of object {key,value}
let's take newArray[0] 's {key,value}
My array[0]
has its own set of key,value
and also points to another object's key,value which it can change.
Update:
array[0] having its orginal key,value as well as another key,value from different object... so my array[0] object has two key,value...
Upvotes: 0
Views: 158
Reputation: 3077
Do you want something like this?
var arrayA=[];
arrayA[0] = {key:1,keyB:"key3"}
var arrayB=[];
arrayB[0] = {key3:"Hello"}
arrayB[0][arrayA[0]["keyB"]]="Bye";
alert(arrayB[0].key3);
Upvotes: 1
Reputation: 147413
In an object, the "key" is just a named property, it is not a reference to anything. You can't change the name of a property, all you can do is create a property with a different name, assign the value of the first property to the second, then delete the first one, e.g.
function reNameProperty(obj, prop0, prop1) {
obj[prop1] = obj[prop0];
delete obj.prop0;
}
But there doesn't seem much point to that. What I think you want to do is:
var obj0 = {p: 'value};
var arr = [obj0, obj0];
So both arr[0] and arr[1] reference the same object. And if one changes:
var arr[0] = {q: 'different value');
then somehow arr[1] will also reference this new object.
You can only do that if you create a function to do the setting of the value of arr[0] and if it somehow knows which other array members should reference the same object.
Upvotes: 1
Reputation: 26753
Do you mean:
array[0] = [array[0], newArray[0]]
This will make an array of the objects. The old one and the new one.
It's very hard to understand what you are trying to do.
Upvotes: 1
Reputation: 5850
You can simply add a second key.
array[0].key2 = object2
If I have understood your question correctly.
Upvotes: 1