/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Array problem. Help please!

Can anyone explain to me why, in the below code, the store array is changed when the results array is changed? and, more importantly, how to stop it from doing so?

I’ve tried googling various things but I’m not getting anywhere. Any help would be appreciated.

[CODE]<html>
<head>
<script language=”javascript” type=”text/javascript”>

function doit(){

store = document.getElementById(‘string’).innerHTML.split(‘;’)

for (i=0;i<store.length;i++){
store[i] = store[i].split(‘,’)
}

results = new Array();
count = 0;

for (i=0;i<store.length;i++){
for(j=0;j<store.length;j++){
if (store[i][j].indexOf(“STRING1”) != -1){
results[count] = store[i];
count++
}
}
}

for (i=0;i<results.length;i++){
for (j=0;j<results[i].length;j++){
if (results[i][j].indexOf(“0”) != -1) results[i][j] = “This is the test sentence”
}
}

alert(results)

alert(store)

}

</script>
</head>
<body onload=”doit()”>

<span id=”string”> STRING0, test0, test1, test2; STRING1, test0, test1, test2; STRING2,test0,test1,test2; STRING3,test0,test1,test2</span>

</body>
</html>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 26.2009 — As requested, your fixed function:

<i>
</i>
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script language="javascript" type="text/javascript"&gt;

function doit(){

var store = document.getElementById('string').innerHTML.split(';')

for (i=0;i&lt;store.length;i++){
store[i] = store[i].split(',')
}

var results = new Array();
count = 0;

for (i=0;i&lt;store.length;i++){
for(j=0;j&lt;store.length;j++){
if (store[i][j].indexOf("STRING1") != -1){
results[count] = store[i].slice();
count++
}
}
}

for (i=0;i&lt;results.length;i++){
for (j=0;j&lt;results[i].length;j++){
if (results[i][j].indexOf("0") != -1) results[i][j] = "This is the test sentence";
}
}

alert(store);
alert(results);

//alert(store)

}

&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="doit()"&gt;

&lt;span id="string"&gt; STRING0, test0, test1, test2; STRING1, test0, test1, test2; STRING2,test0,test1,test2; STRING3,test0,test1,test2&lt;/span&gt;


&lt;/body&gt;
&lt;/html&gt;



Notice the only difference is the following line:

<i>
</i>results[count] = store[i].slice();


What you were doing is [i]referencing[/i] store[i] by the variable results[count]....results[count] may have been a different variable name, but it still pointed to the same data as store[i] - therefore, any time you changed results[count], then store[i] would also be changed because they pointed to the exact same thing.



What you actually wanted to do, is create a new variable (results[count]) and have that new variable contain the same information (but not point to the same information) as was in store[i]....this is handled by the slice method - it extracts the data from the array store[i] and places it in the new variable.



People who are more knowledgeable in JS than I would probably use the correct terminology in the explanation above (I'm sure I haven't) but I think you get the gist of what I am trying to say.





http://www.webdeveloper.com/forum/archive/index.php/t-20607.html <- This archived thread from webdeveloper may explain it better if you didn't understand.....exact same issue as yours.
Copy linkTweet thisAlerts:
@asparagus_timeauthorNov 26.2009 — That's great!

I believed that was what might be happening but didn't know how to stop it.

Thanks for your help.
×

Success!

Help @asparagus_time spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...