javascript object instantiation to include an array as one of the properties - javascript

I am trying to create an object which includes an array stPoints as one of its properties.
I am getting an error message saying that stPoints is undefined. What is the proper way to declare an array property within an object?
this is my code:
var temp={stName:"#states.stateid#",stPoints:[]};

This is correct. You must be referencing it incorrectly.
temp.stPoints

var temp={
'stName':"#states.stateid#",
'stPoints':[]
};
However, if the property name is a valid javascript identifier, as in this case, you should not have problem.

Related

How do I access the 'str' value in my object?

I am trying to return the value under the key 'str' in an Object but I am having trouble accessing the value.
This is what is returned in the console:
Currently I am using a map function to go over the array and just return the _str value like so:
let idx = currentArray.map(function(x) {
return x._id._str;
});
However it is still returning the value as an object. How can I get just the value of the _str key?
Here is the full array without specifying the id field. This is what is returned if you jsut return 'x' in the map function.
You've clarified that the screenshot is of x._id. So to access _str, you'd use x._id[0]._str: The _str property is in the object referenced by the 0 property (the first entry in the array x._id refers to).
Note that in general, _-prefixed properties are meant not to be accessed by code outside the code responsible for the objects in question. You don't seem to be responsible for them, so accessing those properties is likely to make your code rely on undocumented properties that may change in the next "dot" release of whatever lib you're using. It's just convention, but it's a very common convention.
If you right click on the property, most browser consoles offer the ability to copy property path.
Based on this SO post and the docs, it appears that you can probably use x._id.str.
If I understand correctly, you are receiving the str value but it is an object instead of the string literal. In other words, you are getting _str: "598..." instead of "598....". A possible solution would be to use the mongo javascript function to convert the str value to a string.
In your case, I think something like return x._id.str; may work as _id is a MongoID.ObjectID.
I've also linked the documentation below for reference.
https://docs.mongodb.com/manual/reference/method/ObjectId/
Here's a relevant SO answer as well: Convert ObjectID (Mongodb) to String in JavaScript
I think you should write x[_id]._str because _id is one of the array objects.

What is the Type of a javascript object property?

Javascript objects use a key value system (like a hash, map, or dictionary in other programming languages). The key is referred to as a property and within an object is written like this:
var object = { property01: value01, property02: value02 }
Within objects we can access the value of the property using two access methods.
object.property01
object['property01']
In the example above property01 and property02:
What is the type of the variable holding the property name, and how is it stored in memory?
I'm not sure if I understand your question correctly, but the closest answer I could reference is this:
Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
It's a variable, but instead of being attached to the window object, it's attached to another variable.

How to access array of objects inside $scope

I am trying to access an array of objects within $scope.
If I console.log($scope) (see image below) it displays the pedigree object that I would like to access. However, if I console.log($scope.pedigree) I get undefined. If I console.log(typeof $scope.pedigree) I get object.
Can someone help me access pedigree?
Pedigree is an array of objects, not a single object. As a result, you need to access the appropriate instance by index to get your object.
var pedigree = $scope.pedigree[0];
There is no "array" type in Javascript. As a result, when you use typeof on your $scope.pedigree property, it will tell you it is of type object, even though happens to be an array of objects.
You can access to scope via jquery or jqlite: $('.element').scope();
Accessing via console.log is not possible, because angular doesn't use global scope for storing scopes. More information here.

How to remove the inner properties from JSON object

I have two JSON stringified objects,
var first='{"AdmissionId":254625,"PhysicianId3":null,"VerbalStartOfCareDate":"","PhysicianId2":null,"LatestStatusCode":"A01","ReferralSource":97664,"PhysicianId1":97670,"$id":"2","LatestAdminSetId":90,"LastModified":"2015-03-16T10:13:55.827","AdmitDate":"2015-03-01T00:00:00","TeamId":153,"ProspectAdmitDate":null,"TerminationDate":null,"PatientId":154112,"PatientStatus":[{"Notes":null,"PpsTherapyVisits":null,"SyncEpisodes":null,"StatusCode":"A01","StatusId":0,"PpsEpisodeNo":1,"StatusEndDate":null,"PrimaryDiagnosisId":870428,"Sequence":2,"PpsAssessDate":null,"PatientId":154112,"Acuity":"Pend","SyncStatus":4,"LastModified":null,"BranchId":null,"PpsAssessRfa":null,"PrintDischargeSummary":null,"OasisMatchingKey":null,"PpsScicOverride":null,"PpsKnownLupa":null,"DataSetId":26,"AdminSetId":90,"MsaCode":"50094","GenerateFinalClaimOnly":null,"PpsEpisodeNoModified":"N","AssociatedFacilityRoleId":null,"ClassEndDate":null,"PpsHippsCode":null,"PpsHippsValidity":null,"PatientClass":"HH","ClientId":0,"PpsAssessmentId":null,"StatusTime":null,"HpcanysCareLocation":null,"PpsHhrgId":null,"EpsHhrgId":null,"Id":0,"$id":"3","StatusDate":"2015-08-17T00:00:00","AdmissionId":254625,"PpsNrsSeverityId":null,"AssociatedFacilityId":null}],"CaregiverCode":"SP","ReferralSourceRoleId":33149,"ClinicalSetId":93,"PatientAdmissionCtiDef":null,"Id":0,"SnfIndicator":null,"ClientId":0,"AdmissionCode":null,"ProspectTermDate":null,"SyncStatus":2,"Sequence":1}';
var second = '{"AdmissionId":254625,"PhysicianId3":null,"VerbalStartOfCareDate":null,"PhysicianId2":null,"LatestStatusCode":"A01","ReferralSource":97664,"PhysicianId1":97670,"$id":"2","LatestAdminSetId":90,"LastModified":"2015-03-16T10:13:55.827","AdmitDate":"2015-03-01T00:00:00","TeamId":153,"ProspectAdmitDate":null,"TerminationDate":null,"PatientId":154112,"PatientStatus":[{"Notes":null,"PpsTherapyVisits":null,"SyncEpisodes":null,"StatusCode":"A01","StatusId":802577,"PpsEpisodeNo":1,"StatusEndDate":null,"PrimaryDiagnosisId":870428,"Sequence":1,"PpsAssessDate":null,"PatientId":154112,"Acuity":"Pend","SyncStatus":2,"LastModified":"2015-03-16T10:11:26.857","BranchId":null,"PpsAssessRfa":null,"PrintDischargeSummary":null,"OasisMatchingKey":null,"PpsScicOverride":null,"PpsKnownLupa":null,"DataSetId":26,"AdminSetId":90,"MsaCode":"50094","GenerateFinalClaimOnly":null,"PpsEpisodeNoModified":"N","AssociatedFacilityRoleId":null,"ClassEndDate":null,"PpsHippsCode":null,"PpsHippsValidity":null,"PatientClass":"HH","ClientId":0,"PpsAssessmentId":null,"StatusTime":null,"HpcanysCareLocation":null,"PpsHhrgId":null,"EpsHhrgId":null,"Id":0,"$id":"3","StatusDate":"2015-03-01T00:00:00","AdmissionId":254625,"PpsNrsSeverityId":null,"AssociatedFacilityId":null}],"CaregiverCode":"SP","ReferralSourceRoleId":33149,"ClinicalSetId":93,"PatientAdmissionCtiDef":null,"Id":0,"SnfIndicator":null,"ClientId":0,"AdmissionCode":null,"ProspectTermDate":null,"SyncStatus":2,"Sequence":1}';
I need to remove the following properties from the above mentioned JSON objects and the result of that should be compared:
I used underscore's _.omit () functionality it removed only the outer collection property but it failed to remove the inner collection property
eg: PatientStatus.StatusId in the above JSON
I also tried using delete keyword of javascript it is working, but is there any better way to do it ?
Try to JSON.parse the string, and then delete the property you want to remove..
JS Delete - Mozilla MDN

Javascript Objects and keys

The following command shows the properties of an Object.
var keys = Object.keys(result);
Output: [requester.client.id,request.id]
When I try to print an alert(result[request.id]) or alert(result.request.id) I dont get the values. Is there something I am missing?
In JavaScript objects keys are strings, though they can have periods. What you probably getting as the output is ['requester.client.id','request.id'], so it should be accessed as result['requester.client.id'].
Your result object has properties named "requester.client.id" and "request.id".
You need to do alert(result["request.id"]).
result[request.id] does not work because request here is treated as a variable name, and you probably have no variable named request.
result.request.id is closer, but it also fails because the property name has a period in it, so the parser treats this as the the id property of the request property of result.

Categories