0

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

7
  • 4
    You have not managed to convey the nature of your problem at all. Commented Jul 31, 2011 at 22:48
  • Is it true that you want to clone the values from newArray and not just create a copy to that both arrays refer? Commented Jul 31, 2011 at 22:52
  • @Jens Struwe: I want to create a newArray with my own set of key's and value's which my array[0] would refer... nothing over cloning the object... Commented Jul 31, 2011 at 22:55
  • So what's the problem? Just assign it. Commented Jul 31, 2011 at 22:56
  • @Ariel: i want both the key,value to persist... instead of pointing one to another... Commented Jul 31, 2011 at 22:59

4 Answers 4

1

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.

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

Comments

1

You can simply add a second key.

array[0].key2 = object2

If I have understood your question correctly.

2 Comments

@Colin Price: i want this to point to another object's key,value and also have its orginal set of keys...
I think you need to rethink your object design if you want to have that capability.
1

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.

Comments

1

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

Comments

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.