Create temp values in AngularJS - angularjs

In angular the following happened to me
function(){
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = $scope.varObj;
$scope.tempVar2 = $scope.varObj ;
}
Now if i change tempVar2 automatically the value of tempVar1 is changed.
function(){
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = $scope.varObj;
$scope.tempVar2 = $scope.varObj;
$scope.tempVar2.name = "mathews";
console.log($scope.tempVar1.name);
}
The output should be "john" because i am changing the value of tempVar2 not the tempVar1. But to the surprise the output is "mathews".
I tried both angular.copy and angular.extend but both produce the same output.
Can someone please help

I don't see any reason for it to not work with angular.copy
Heres' a fiddle with
demo

It's hapening becuase of two way binding
Try like this
$scope.tempVar1 = angular.copy($scope.varObj);

Use angular.copy() for it
By default only reference copy with assignment operator = which mean sharing the same memory space so if you change in any of the two object it will reflect into second one as well.
angular.copy() will create deep copy of the object
DOC:-
Creates a deep copy of source, which should be an object or an array.
$scope.varObj = {"name" : "john" , "age" : "20" };
$scope.tempVar1 = angular.copy($scope.varObj);
$scope.tempVar2 = angular.copy($scope.varObj);
$scope.tempVar2.name = "mathews";
console.log($scope.tempVar1.name);

Related

How to remove one of item in aray object and update state after that?

I have a state "Bill" and I want to delete one of item in "Product" with "ProductID". So how can I do it?
Bill = {
"Bill_ID" : "a00231",
"Products" : [
{
"ProductID" : "P0203",
"ProductName" : "ABCD"
},
{
"ProductID" : "P023243",
"ProductName" : "ZYZ"
}
]
}
How can I update the state after delete product item?
I think you could use thepop() method. You can pop the last element from your products array and look at the popped element. Something like:
var poppedElement = Bill.Products.pop();
console.log(Bill) //Bill withoug last Products element
console.log(poppedElement) //just the last element of Products
Not sure if this will work completely off the top of my head, but hopefully will give you a starting place at least :)
Check out this link for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
Let me know how it goes!
You can use filter on bill.products array, like following (note it must be != not ==)
bill.products.filter(x => x != yourDesiredToBeDeletedId)
Treat this.state as if it were immutable
Since your state contains an array within an object, I suggest to use lodash's cloneDeep.
let newBill = _.cloneDeep(this.bill);
newBill.products = newBill.products.filter(x => x != yourDesiredToBeDeletedId);
this.setState(bill: newBill);

MongoDB add values to array if not exists

I have this collection :
{
username : "user1",
arr : [
{
name : "test1",
times : 0
},
{
name : "test2",
times : 5
}
]
}
I have an array with some object. This objects have a name and the value times. Now I want to add new objects, if my array doesn't contain them. Example:
I have this two objects with the name "test1" and "test2" already in the collection. I want now to insert the objects "test2", "test3" and "test4". It should only add the object "test3" and "test4" to the array and not "test2" again. The value times doesn't do anything in this case, they should just have the value 0 when it gets insert.
Is there a way to do this with one query?
If you can insert test1, test2,... one by one, then you can do something like this.
db.collection.update(
{username : "user1", 'arr.name': {$ne: 'test2'}},
{$push: {
arr: {'name': 'test2', 'times': 0}
}
})
The $ne condition will prevent the update if the name is already present in arr.
You can now use the addToSet operator that is built just for that: adds a value to an array if it does not exist.
Documentation: https://docs.mongodb.com/manual/reference/operator/update/addToSet/

update field of array object inside nested object

I have following db collection of users.
[{
name : "abc",
obj:{ id : 123 , arr[{fid:"a123",field:"0"},{fid:"b123",field:"0"}]}
},
{
name : "pqr",
obj:{ id : 456 , arr[{fid:"a456",field:"0"},{fid:"b456",field:"0"}]}
}]
I want to update field value of fid : b456 to 1 in mongodb.
How to write query for same
Use $
db.users.update({obj.arr.fid:"b456"},{$set: {"obj.arr.$.field":"1"}})
You can as the below:
db.users.update({"obj.arr.fid": b456 }, {$set: { "obj.arr.$.fid": 1 }})
The positional $ operator acts as a placeholder for the first element
that matches the query document.
Have you tried something?
Maybe this can help you:
db.users.update({name:"pqr"},{$set: {"obj.arr[1].fid":"1"}})
For more info take a look here $set

Using JSON with an object

I have an object like
data: Array[1];
which can be accessed like
data[0].name
data[0].place
I am trying to convert this to JSON using like
var arr = JSON.stringify(data);
which returns
var arr = [{"name": "blah", "place": "ca"}]
But Im confused how to use this arr now its stringified ? How do I access for example the "name" value ? I tried arr.name but that doesn't seem to work ?
The array is
arr == [{"name": "blah", "place": "ca"}];
That object is the first item in the array
arr[0] == {"name": "blah", "place": "ca"}
and its properties...
arr[0].name == "blah"
your "name" is inside the hash/associative array which is inside an array
so u need to grab hash/associative array first, using
arr[0]
and then u can access your attributes.

Sorting an arraycollection with a numeric value coming from 2 diffrent fields

Hi guys I need to sort an arraycollection. the problem is that my array is combined from two diffrent arrays, in one the age field is called "AGE", and in the other the age is called "MYAGE".
Now I need to sort the combined array according to age but I got two fields with diffrent names. I can go through the array and change all "MYAGE" to "AGE" but i was wondering if theres maybe a way to sort the array in its current status?
Thanks ahead
Say you have an ArrayCollection called myCollection. I hope the following will solve your request for that:
private function compareItems(a:Object, b:Object, fields:Array = null):int
{
var firstValue:Number = "AGE" in a ? a["AGE"] : a["MYAGE"];
var secondValue:Number = "AGE" in b ? b["AGE"] : b["MYAGE"];
if (firstValue == secondValue)
return 0;
return firstValue > secondValue ? 1 : -1;
}
…
var sort:ISort = new Sort();
sort.compareFunction = compareItems;
myCollection.sort = sort;
myCollection.refresh();

Categories

Resources