0
var ce_info=[
{location:"inchannel",name:"Jae Jung",volume:"50",url:"img/jae.jpeg",group:1},
{location:"inchannel",name:"Houston",volume:"50",url:"img/houston.jpeg",group:1},
{location:"inchannel",name:"Jun kuriha..",volume:"50",url:"img/jun.jpeg",group:1},
{location:"inchannel",name:"Andrea Melle",volume:"50",url:"img/andrea.jpeg",group:0},
{location:"inchannel",name:"Tomoaki Ohi..",volume:"50",url:"img/ohira.jpeg",group:0},
{location:"inchannel",name:"Woosuk Cha..",volume:"50",url:"img/woosuk.jpeg",group:0},
{location:"inchannel",name:"Luca Rigaz..",volume:"50",url:"img/luca.jpeg",group:0},
{location:"inchannel",name:"SooIn Nam",volume:"50",url:"img/sooin.jpeg",group:0}
];
var inch_info=[{location:"ichat",name:"",volume:"50",url:""}];

for(i=0;i<ce_info.length;i++)
{   

    if(ce_info[i].location=="inchat")
    {       

        inch_info[inchat_count].name=ce_info[i].name;
        inch_info[inchat_count].url=ce_info[i].url;
        inchat_count++;

    }
}

I am trying to copy ce_info to inch_info. It seems like it does not work.It occurs an error when I try to copy ce_info to inch_info Any thought?

3
  • where do you initialise the variable inchat_count ? Commented Mar 28, 2012 at 7:25
  • it is global variable I forgot to put it Commented Mar 28, 2012 at 7:39
  • This code wont do anything because there is no location inchat in the ce_info Commented Mar 28, 2012 at 7:51

4 Answers 4

4

Copying a native JS Array is easy. Use the Array.slice() method which creates a copy of part/all of the array.

var foo = ['a','b','c','d','e'];
var bar = foo.slice();

now foo and bar are 5 member arrays of 'a','b','c','d','e'

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

Comments

2

inchat_count seems to be uninitialized.

var inch_info=[{location:"icha",name:"",volume:"50",url:""}];
var inchat_count = 0; // You forgot this line!!
for(i=0;i<ce_info.length;i++)
{   

    if(ce_info[i].location=="inchat")
    {       

        inch_info[inchat_count].name=ce_info[i].name;
        inch_info[inchat_count].url=ce_info[i].url;
        inchat_count++;

    }
}

4 Comments

wat about using variable i itself ? instead of using one more variable inchat_count
it gives me thie error Uncaught TypeError: Cannot set property 'name' of undefined
@mithunsatheesh I also thought of the 'i' variable, but there is a 'if'-statement, which means probably not all of entries 'ce_info' will be copied, so we need another count-variable.
@dradford You're right, I overlooked that... You'll need to add inch_info[inchat_count] = {}; before setting name and url. But I think Rick Hoving said it better.
1

You still have a typeO in your code

var inch_info=[{location:"ichat",name:"",volume:"50",url:""}];

shouldnt location be inchat instead of ichat? Because that is what you check for in this line

if(ce_info[i].location=="inchat")

Furthermore in ce_info there is no location named inchat so the result of this piece of code will not be executed. But if there where an element in ce_info that has the location inchat this will be the code you need to add the location and name to the inch_info array.

Change your for loop in this:

for(var eElem in ce_info)
{   
    if(ce_info[eElem].location=="inchannel")
    {
        var temp = {};
        temp.name=ce_info[eElem].name;
        temp.url=ce_info[eElem].url;
        inch_info.push(temp);

    }
}

2 Comments

I just want to copy name and url into inch_info i do not need to copy a whole array into inch_info
Edited my answer, is this more like you need?
0

using jQuery you can achieve it so easily:

See a working demo here

following jQuery code makes sure that you do not have a by reference object of ce_info.

jQuery code:

var inch_info= jQuery.extend(true, {}, ce_info);

alert("inch_info: before changing text: "+inch_info[0].location);

inch_info[0].location = "hello world"

alert("inch_info: after changing text: "+inch_info[0].location);
alert("ce_info: after changing text: "+ce_info[0].location);

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.