0

Javascript (using jQuery):

var paragraphs = [
    ['This is my first paragraph content of the first array', 'This is my second paragraph content of the first array', 'This is my third paragraph content of the first array'],
    ['This is my first paragraph content of the second array', 'This is my second paragraph content of the second array', 'This is my third paragraph content of the second array']
],
text_box_value,
unused_paragraphs = null;

$(document).ready(function(){
    $('input#text_box').keyup(function(){
        text_box_value = $(this).val(); 
    });

    $('input#submit_button').click(function(){
        if(unused_paragraphs === null) {
            unused_paragraphs = paragraphs; 
        }

        for(var i = 0; i < unused_paragraphs.length; i++) {
            if(unused_paragraphs[i].length == 0)
                unused_paragraphs[i] = paragraphs[i];

            while(unused_paragraphs[i].length != 0) {
                var rand = Math.floor(Math.random() * unused_paragraphs[i].length);
                if(unused_paragraphs[i][rand].search(text_box_value) !== -1) {
                    $("#paragraphs_container").append('<p>' + unused_paragraphs[i][rand] + '</p>');
                    break;
                }

                unused_paragraphs[i].splice(rand, 1);
            }   
        }

        console.log(unused_paragraphs);
        console.log(paragraphs);

    });

});

My question is why when I use splice method on unused_paragraphs variable it also remove the value from the paragraphs variable

Later edit JSFiddle

1
  • 1
    Sounds like you only have one array but two variables referencing it. Commented Dec 17, 2012 at 11:04

2 Answers 2

1

javascript objects/array are stored by reference.

If you want a copy that's a trick:

if(typeof unused_paragraphs == "undefined") {
        var unused_paragraphs = [];
        for(var i = 0; i<paragraphs.length; i++) {
            unused_paragraphs[i] = paragraphs[i].slice(0);  
        }
}

and

unused_paragraphs[i] = paragraphs[i].slice(0);
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried this but now the problem is that splice method is not working anymore.. The found value is not removed from the array.. Why's that?
This solution works for me @MateiMihai, did you change both lines? which version of jQuery are you using?
For an advanced copy function, you can see also this answer (stackoverflow.com/questions/2294703/…).
Thank you for your answer, it was what I've expected but I have to review my code because now there is something else which is not working. Thank you
1

to copy the obect to new object..

try this..

var unused_paragraphs= jQuery.extend(true, {}, paragraphs);

here is just an example of copied objects.. check it out

http://jsfiddle.net/5ExKF/3/

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.