Reputation: 241
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?
Upvotes: 0
Views: 402
Reputation: 3575
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);
}
}
Upvotes: 1
Reputation: 6043
using jQuery
you can achieve it so easily:
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);
Upvotes: 0
Reputation: 2628
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'
Upvotes: 4
Reputation: 179
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++;
}
}
Upvotes: 2