Reputation: 117
Now i have two array like this
$arr1 = [{
fullname: 'Foo1',
info: {
age: 18,
country: 'HN'
}
},
{
fullname: 'Foo2',
info: {
age: 18,
country: 'HN'
}
}]
$arr2 = [{
fullname: 'Bar1',
info: {
age: 18,
country: 'HN'
}
},
{
fullname: 'Bar2',
info: {
age: 18,
country: 'HN'
}
}]
Now i want swap $arr1[0] with $arr2[1] but itsn't work. See my code
temp = $arr1[0];
$arr1[0] = $arr2[1];
$arr2[1] = temp;
when i debug $arr1[1] change before and after set = $arr2[1], but itsn't update, when i just set $arr1[0].fullname="ABC", it's update. I can't fix this bug. Please help me.
Thanks for help!
Upvotes: 0
Views: 60
Reputation: 1431
I tried the below code. It is working fine for me.
$arr1 = [
{
fullname: 'Foo1',
info: {
age: 18,
country: 'HN'
}
},
{
fullname: 'Foo2',
info: {
age: 18,
country: 'HN'
}
}];
$arr2 = [{
fullname: 'Bar1',
info: {
age: 18,
country: 'HN'
}
},
{
fullname: 'Bar2',
info: {
age: 18,
country: 'HN'
}
}];
console.log($arr1[0]);
console.log($arr2[1]);
var temp = $arr1[0];
$arr1[0] = $arr2[1];
$arr2[1] = temp;
console.log($arr1[0]);
console.log($arr2[1]);
Output
sandeep@pc:~/Documents/Angular/Snippets$ node snippets.js
{ fullname: 'Foo1', info: { age: 18, country: 'HN' } }
{ fullname: 'Bar2', info: { age: 18, country: 'HN' } }
{ fullname: 'Bar2', info: { age: 18, country: 'HN' } }
{ fullname: 'Foo1', info: { age: 18, country: 'HN' } }
Upvotes: 0
Reputation: 8241
According to your original code snippet, you did not access array in an index based manner. $arr1[1]
should be $arr1[0]
. The following code should work.
temp = $arr1[0];
$arr1[0] = $arr2[1];
$arr2[1] = temp;
Technically speaking, swapping two variables with a temporary variable tempVar
should work. Please try to debug your variable value with console.log(angular.toJson($arr1));
Upvotes: 3