console.log(data)
{
"LoginDetails": "{\"BrowserInformation\":\"Mozilla\\/5.0 Chrome\\/76.0.1100.182 Safari\\/537.36\",\"ip_address\":\"180.80.21.172\"}"
}
from above object i want to take only the ip_address, i don't have idea how to get the value from above object
Use JSON.parse(obj)
var obj={
"LoginDetails": "{\"BrowserInformation\":\"Mozilla\\/5.0 Chrome\\/76.0.1100.182 Safari\\/537.36\",\"ip_address\":\"180.80.21.172\"}"
};
console.log(JSON.parse(obj.LoginDetails).ip_address)
Use JSON.parse(data.LoginDetails).ip_address. This will give the required value. For other parameters just need to change the name at last.
Related
I have a form that uses $scope.booking variable composed of several fields and array, all loaded from HTML.
I need to add an array of object from javascript, adding one object per time.
I tryed
$scope.booking.newExternalUsers[$scope.count]= $scope.user.newExternalUser;
and
$scope.booking.newExternalUsers.push=$scope.user.newExternalUser;
but I receive Cannot set property '0' of undefined and Cannot set property of undefined, it is correct because I have instantiated only $scope.booking={}
Maybe is a stupid question, but I am almost new in angularjs, how can I add the $scope.user.newExternalUser one pertime (for each button event)?.
Thanks
You should define the array first before set values to it.
like this:
$scope.booking = {};
$scope.booking.newExternalUsers = [];
or
$scope.booking = {
newExternalUsers: []
};
Then
You can add items to it as you want, like this:
$scope.booking.newExternalUsers[$scope.count]= $scope.user.newExternalUser;
or using Array.prototype.push()
$scope.booking.newExternalUsers.push($scope.user.newExternalUser);
First define the property newExternalUsers in booking array first
$scope.booking={
'newExternalUsers' : []
}
or
$scope.booking.newExternalUsers=[]
Then push the item to an booking array
$scope.booking.newExternalUsers.push($scope.user.newExternalUser);
You have to instantiate $scope.booking.newExternalUsers=[]
I'll assume you have a controller. In that controller you can have this:
$onInit() {
this.booking = { newExternaUsers: [] };
}
Then it's initiated once when the controller starts.
I have made a custom filter in controller but can't get index or size of iterated json. Calling program.length gets me undefined. Any ideas?
$scope.filterFn = function(program){
if(case){
return true;
}
console.log(program.length);//undefined
return false;
};
try to use this code to get object length.
Object.keys(program).length;
try : How to display length of filtered ng-repeat data, so you can access size by $scope.filtered.length like the example.
Are there any more filters before this? Coz previous filters decide input for next filter?
The length() method is only available on an array [].
In this case you are trying to get the size of a json object {}, please note this does not have a length. If you need to see how many items exist in this object, you can try to first convert all the keys into an array by using Object.keys(program) which will give you an array.
If program originally looked like this:
{
foo: 'some value',
bar: 'another value'
}
using Object.keys(program) will give you ['foo', 'bar'] On this array you can now call the length method, so that you have Object.keys(program).length, which in this case would give you 2.
I have a object like as follows
var obj={
userName:sessionStorage["userName"]
}
1st time if I try to access obj.userName i am getting undefined, because there is no value in it.
Now if I am getting undefined even after setting the value for it
sessionStorage["userName"]="name";
obj.userName; //undefined
How to solve this ?
userName takes the value of sessionStorage["userName"] at the time of its creation. Later changes to sessionStorage["userName"] will not be reflected in userName.
If you want to get sessionStorage["userName"] whenever you get value from userName, then you need to define a getter, like this
Object.defineProperties(obj, {
userName: {
get: function() {
return sessionStorage["userName"];
}
}
});
You should do this:
sessionStorage["userName"]="name";
obj.userName = sessionStorage["userName"];
obj.userName;//name
Just changing the value of sessionStorage doesn't reflect the change to the object itself.
In order to change the object, you need to actually change the object.
How can I store the result of a function to a variable?
In navigating an object that contains an array, I am looking for one value, value1, once that is found I want to get the value of one of its properties, property1.
The code I am using below is an example and is incorrect.
function loadSets(id){
clworks.containers.find(function(i){
return i.get('property1')===id;
});
}
My intent is to navigate the object below:
clworks.containers
containers[0].property0.id
containers[1].property1.id
I am trying to determine how to find which item in the array has a property value equal to the id used in the function and then store it as a variable.
Simply:
var myVar = loadSets(id);
EDIT
Ok, as much as I understand your question now, your situation is the following:
You have an array containing objects called containers;
You want to iterate through this array, looking for the property id of the property property1 which equals the one specified in the function called loadSets(id);
Once found, store the object with the requested id in a variable.
Am I right?
If so, this should solve your problem:
// This function iterates through your array and returns the object
// with the property id of property1 matching the argument id
function loadSets( id ) {
for(i=0; i < containers.length; i++) {
if( containers[i].property1.id === id )
return containers[i];
}
return false;
}
After this you just need to do what I said in the first answer to your question, triggering it however you want. I put up a quick JSBin for you. Try to put 10, or 20, in the input field and then hitting the find button; it will return the object you are looking for. Try putting any other number, it will return a Not found.
Currently your function, loadSets, isn't actually returning anything and so you cannot store a result from it.
Try:
function loadSets(id){
return Base.containers.find(function(i){
return i.get('entityid')===id;
});
}
And to get a result into a variable:
var result = loadSets(id);
I am refactoring some code where I grab the values inputted into textboxes and use said values for calculations. The problem is rather than using document.getElementByID multiple times I want to loop through each textbox on the page and assigning each value to this object which I will pass to my calculation functions. I would also like to set the property names of this object by looking at the input textbox IDs and using the ID strings for the object property names.
See code below:
$(document).ready(function() {
var calcObj = new Object();
$("input[id^='txt']").each(function(i, val) {
calcObj[i] = this.value;
});
$.each(calcObj, function(i, val) {
// alert(val);
});
});
As you can see when document is ready, object is created. There is a jquery .each loop to go through every input with id that contains txt. I am assigning this.value to object where index is i.
So I want to some how name the properties of this object and assign value so I can reference object property name elsewhere in the code where I pass this object to another function.
Thanks.
As far as I understand, you want:
calcObj[this.id] = this.value;
I don't exactly get what you're asking for, because it seems like you're already doing what I think you're asking.
Right now, you're doing:
calcObj[i] = this.value;
That's no different from assigning it like:
calcObj['foo'] = this.value;
// and later we can access that via
alert( calcObj.foo ); // or calcObj['foo']
You can be dynamic with that as well, like:
calcObj[this.id] = this.value;