473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

json in array -> overlap problem

hi to all!

i have the following code (i stripped down the code to a short
example, but still it does reproduce the problem):

var objs = new Array();
obj = {
parameter : " "
}

objs[0] = obj;
objs[0].parameter = "apple";

objs[1] = obj;
objs[1].parameter = "banana";

alert(objs[0].parameter);

i create a json object (obj) and insert into 2 items of a previously
created array (objs[0] and objs[1]). inspecting the array, i expect
objs[0].parameter to be "apple", not "banana", which is what i keen on
getting.
any idea?
tia, gustavo

Jul 19 '07 #1
4 1985
d d
pistacchio wrote:
i create a json object (obj) and insert into 2 items of a previously
created array (objs[0] and objs[1]). inspecting the array, i expect
objs[0].parameter to be "apple", not "banana", which is what i keen on
getting.
objs[0] and objs[1] are both pointers to obj. You're not creating new
instances of obj, you're creating new references to the obj object.

Define obj like this:

obj=function(){this.parameter="";}

and in your array, do this:

objs[0] = new obj();
objs[0].parameter = "apple";

objs[1] = new obj();
objs[1].parameter = "banana";

~dd
Jul 19 '07 #2
pistacchio wrote:
hi to all!

i have the following code (i stripped down the code to a short
example, but still it does reproduce the problem):

var objs = new Array();
obj = {
parameter : " "
}

objs[0] = obj;
objs[0].parameter = "apple";

objs[1] = obj;
objs[1].parameter = "banana";

alert(objs[0].parameter);

i create a json object (obj) and insert into 2 items of a previously
created array (objs[0] and objs[1]). inspecting the array, i expect
objs[0].parameter to be "apple", not "banana", which is what i keen on
getting.
any idea?
var objs = [
{parameter: 'apple'},
{parameter: 'banana'}
];
Jul 19 '07 #3
d d ha scritto:
pistacchio wrote:
>i create a json object (obj) and insert into 2 items of a previously
created array (objs[0] and objs[1]). inspecting the array, i expect
objs[0].parameter to be "apple", not "banana", which is what i keen on
getting.

objs[0] and objs[1] are both pointers to obj. You're not creating new
instances of obj, you're creating new references to the obj object.

Define obj like this:

obj=function(){this.parameter="";}

and in your array, do this:

objs[0] = new obj();
objs[0].parameter = "apple";

objs[1] = new obj();
objs[1].parameter = "banana";

~dd
hmmm.. so a js array can store 'objectified functions' but not json objects?
Jul 19 '07 #4
pistacchio wrote:
hmmm.. so a js array can store 'objectified functions' but not json
objects?
A JavaScript array can hold anything. Boolean values, Objects,
Functions, numeric and string primitives, et cetera.

I don't believe there is a limit to what an array can hold, but I could
be wrong.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jul 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
6808
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
3731
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
15
9023
by: hobosalesman | last post by:
Is there a function or library somewhere that merges 2 JSON data structures? For instance this: { "one": } plus { "one": } equals
9
1678
by: eggie5 | last post by:
eval is not working on this JSON string: {"top":} {"car":} {"man":} data = eval('(' + json + ')'); What's wrong with the json string, is it 'cause it has no root?
2
1704
by: eggie5 | last post by:
Is this JSON valid? I would like to access it like this in my javascript: var json=eval ('('+json+')'); json.devices.modelNumber and json.devices.image Would this work?
5
16111
by: Otto Wyss | last post by:
I've now been looking for a week for a simple but useful sample on how to get a list of entries (persons) via an XMLHttpRequest using Json/PHP on the server. So far I've found about a thousend...
2
6851
by: phil.swenson | last post by:
I'm using Prototype.js and would like to convert the contents of an HTML table to JSON. Converting to an array first is fine too. Any thoughts on this? I haven't seen anyone do anything this.......
9
10852
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
0
5366
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing...
0
2474
by: JonTwend | last post by:
I am working with the Twitter API and finding it great fun and generally easy to use. But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.