Lodash Pull not working - javascript

If I run the following code, the length does not change:
console.log(files.length);
_.pull(files, [files[0], files[1]]);
console.log(files.length);
where files is an Array object. Shouldn't pull definitely find matches to remove given that I'm constructing its second arguments with values directly from the array?

for some reason trying to change the length property or doing anything that would cause the engine to implicitly change the length, such as calling files.splice, will throw a RangeError
Why not create a new array, using without() to remove the unwanted files?
var newFiles = _.without(files, files[0], files[1]);

I don't think you should pass in an array of values you want to 'pull'. The documentation suggests they should be separate arguments like so:
_.pull(files, files[0], files[1]);
https://lodash.com/docs#pull

Related

firestore add/append data to field without overwrite using update with variables

I would like to add new medication that contains "medc name, medc dose, medc dur"
I used update but the problem is the first argument. Im giving it a non-existing name so it adds it to the existing medications
the right syntax is "medication.avamys : medArr 'that contains the dose and dur'"
but this part "medication.avamys" is varying
so what can I do?
You will want to use the JavaScript syntax to use an expression as the property of an object.
...update({
["medication."+medcName]: medArr
})
Note the expression is in square brackets, and becomes the name of the field written to the document.
Note also that you are not required to build the object within the call to update(). You can build the object in a different statement, and pass it to update(). This is effectively the same thing, and probably more readable if you have lots of properties to assign:
const data = {}
data["medication."+medcName] = medArr
...update(data)

Access Array with String key

I have two variables with JSON files. The first is a list of keys looks like this:
keylist = ["key1","key2","key3"]
The second one is generated from a database and looks like this:
data = {
"key1"{
#further data
},
"key2"{
#further data
},
"key3"{
#further data
}
}
Now I want to access the second element of the database with the key from the keylist
data.keylist[1];
Which doesn't work because the return of keylist[1] is a String? I did some research and the use of the window function was proposed. So I tried this:
window["data." + keylist[1]]();
Which leads to a "is not a function" error. What can I do to solve this problem?
As simple as that:
const mydata = data[ keylist[1] ];
Also, your code is correct from the point of syntax, but it tells completely different than you expect it to tell.
data.keylist[1];
tells JS that you expect to have an object called data which has a property called keylist and which is (most likely) type of array, and you want to get the second element of this array.
PS: And one more point here. Your question title is not completely correct because of the difference between Arrays and Object in JS.
There is no "string keys" for arrays in JS, so you cannot "access array with a string key". Well, truly speaking there are, but not for items of array. Array items only have numeric index, which you can use to access it. Objects, in contrast to arrays, may have named properties. So when you see something like that: data = myVar['data'], you can tell that you're dealing with an object, while data = someVar[0] can be both, an Array (most likely) or also an Object with key named '0'.
I don't think the issue you're having with your first example is because it returns a key. I believe the issue is because data doesn't have a property called keylist. Instead of that, try it as
data[keylist[1]]
and see if that works for you. The reason this one should work is that, in this situation, Javascript will evaluate the string return of keylist[1] and then use it as a string index for the data variable. Let me know if this works out for you :D
You can try using using something like this.
data[keylist[1]]

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.

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

How can I get the length of a deferred Model in CanJS?

I am getting a model deferred object with a structure like this:
How can I find out how many objects are present inside this object (in this case, three). If I use Object.keys(myObject).length, it includes the object observer and other data also like _computeBindings,_bindings etc. I have even tried to use hasOwnProperty while calculating the length but its not giving me desired result.
One way I can figure out is to iterate over the index and get the last index value like this:
can.each(myObject,function(myObject,index){
// Get the last index value and put it into some variable
});
Is there an API for this?
can.Map has a keys function that will give you an Array of the keys in your Map and from that you can get how many Objects by checking that Array's length.
Using a can.List as your data structure would also work. The keys in your data are numeric and you need to check the length, all things that can.List is built for.
Try using myObject.attr('length')
The model _data attribute contains a copy of just the model without the bindings. The easiest way to do this is to use:
Object._data.length

Categories