473,666 Members | 2,368 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 1994
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
6848
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... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object than its XML representation (so of course may argue).
2
3744
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 considered bad practice because it can disrupt the use of for in loops on Objects. Am I incorrect? Thanks,
15
9034
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
1686
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
1714
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
16122
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 different tutorials and code samples but not a single one, where the server returns an array of entries. Very few samples use Json at all and almost none show the server code. So does anybody know a sample which - uses just a small javascript...
2
6864
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.... thanks phil
9
10866
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
0
5370
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 you an example of how an data class (actually, a list of PHP documentation pages) might be represented, first in XML. and then in JSON. This side-by-side comparison should let you begin to understand how to represent data in JSON. The XML version:...
0
2486
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 object data with a foreach loop and print it out. But I want to do more with the data than just print it. Just to be clear, I want to work with the daily and weekly trends but I am using the simpler current trends for this example. What I want to...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
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 we have to send another system
2
2006
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.