getting all values in the row - javascript

var patientInfo = {}
patientInfo.patientID = row.patientID;
patientInfo.firstName = row.firstName;
patientInfo.lastName = row.lastName;
patientInfo.dateOfBirth = row.DateOfBirth;
patientInfo.age = row.age;
patientInfo.gender = row.gender;
When I print I patientInfo, I get only one row's data... how can I get all row's data?

Not quite sure what you mean. But assuming you are having this code in a loop, you are always overwriting the patientInfo data. Add them to an array:
infos = []
for(.....) {
var patientInfo = {}
patientInfo.patientID = row.patientID;
patientInfo.firstName = row.firstName;
patientInfo.lastName = row.lastName;
patientInfo.dateOfBirth = row.DateOfBirth;
patientInfo.age = row.age;
patientInfo.gender = row.gender;
infos.push(patientInfo)
}
It would be good to know here row is coming from. Maybe you could just do:
infos = []
for(.....) {
infos.push(row)
}
or maybe you don't have to do this at all. You have to provide more information.
More information => better answers.

To iterate over properties in an object, use for in.
for (var property in patientInfo) {
alert(property);
}
Keep in mind, however, that if someone has augmented Object, its enumerable properties will be iterated over too.
You can mitigate this by using hasOwnProperty().
for (var property in patientInfo) {
if ( ! patientInfo.hasOwnProperty(property)) {
continue;
}
alert(property);
}
Alternatively, you could specify you own toString() method which formats the values however you want. Be sure to return a string.

Related

How to update an object from an array of objects that is in an object?

My apologies if my question doesn't make sense, and I will try to explain it better...I have an on object and in this object I have an array of objects. I am trying to update one of the found objects in the array of objects, I update the found object but it doesn't update the original object in the array, right now I have this
let vendeeCatalogs = workingVendorImplementorInfo.SVendorCatalogImplementorInfo; // This the array of objects
if (vendeeCatalogs.length > 0) {
for (let i = 0; i < vendeeCatalogs.length; i++) {
foundCatalog = workingVendorImplementorInfo.SVendorCatalogImplementorInfo.find(function (x) { return x.CatalogID == vendeeCatalogs[i].CatalogID });
if (foundCatalog) {
foundCatalog.CatalogGenerationGUID = vendeeCatalogs[i].CatalogGenerationGUID;
foundCatalog.BeginEffectiveDate = vendeeCatalogs[i].BeginEffectiveDate;
foundCatalog.EndEffectiveDate = vendeeCatalogs[i].EndEffectiveDate;
foundCatalog.Multiplier = vendeeCatalogs[i].Multiplier;
foundCatalog.Discount = vendeeCatalogs[i].Discount;
foundCatalog.UOMPrecisionTypeID = vendeeCatalogs[i].UOMPrecisionTypeID;
foundCatalog.IsSelected = vendeeCatalogs[i].IsSelected;
}
}
}
I can see that this is wrong because all it does is updates the foundCatalog, and not the original object that was found.
So how do I find the object and update that object so that the changes are saved in the workingVendorImplementorInfo.SVendorCatalogImplementorInfo?
Why are you not using findIndex() in place of find ?
If you have the index you can then just do something like
vendeeCatalogs[x].CatalogGenerationGUID = ...

Dynamic Array of objects for Javascript

This is what i have so far. Basically every time i make a new Item object i need it to create a specific number of another object called Sensor. I've tried a few other methods and none seem to way the work that i need.
function Item (id,number,operator,numOfSensors){
this.id = id;
this.number = number;
this.operator = operator;
this.numOfSensors = numOfSensors;
var sensor = new Sensor[numOfSensors];
}
var Sensor = {
timeStamp:[],
itemPassed: function(){
timeStamp.push(Date.now());
}
}
Thanks so much for any help :) i'm a bit new to js
EDIT:
Hey guys! Thanks for the help. Basically my issue is neatly making an array of Objects. In the item object i want to make a [numOfSensors] amount of the Sensor object. I cant seem to find a way to do that. SO i guess my question is how would i go about creating an array of objects with a set number of elements?
Not sure when you really want to call itemPassed() and whether this is not rather a function of Item, but here is some code that might get you get started:
var Item = function (id, number, operator, numOfSensors) {
this.id = id;
this.number = number;
this.operator = operator;
this.numOfSensors = numOfSensors;
var sensorArray = [];
for (var i = 0; i<numOfSensors; i++) {
sensorArray.push(new Sensor());
}
}
var Sensor = function() {
this.timeStamps = [];
}
Sensor.prototype.itemPassed = function(){
this.timeStamps.push(Date.now());
}
See:
How to initialize an array's length in javascript?
http://code.tutsplus.com/tutorials/prototypes-in-javascript-what-you-need-to-know--net-24949

Setting dynamically an Object in JavaScript

It seems complicated for me.
First, I have this list:
liste_path_categories.push(
{ index: null
, letter: "letter1"
, type: key
, picture_url: "url1"
, id_categ: null
, response: "Answer here"
});
What I want is to extract from this big list an object in this form:
data["String1"]["String2"]= String3
With :
String1=list_path_categories[i].letter
String2=list_path_categories[i].id_categ
String3=list_path_categories[i].response
example:
data['A']['12'] : "A_answer"
To declare the data i make this:
var data = new Object(new Object);
How I can set all the values in data?
You can use the Array.forEach method to iterate through liste_path_categories and construct your data object.
Example:
var liste_path_categories = [];
var data = {};
liste_path_categories.push(...);
...
liste_path_categories.push(...);
liste_path_categories.forEach(function(element) {
data[element.letter] = {};
data[element.letter][element.id_categ] = element.response;
});
jsFiddle example : http://jsfiddle.net/3ZvNf/
Your question is pretty vague but do you mean something like this?
Setting a dynamic property in an object wich belongs to another object?
data['A']['12'].answer = "A_answer"
Instead of using strings, you have to use the variables in your property access:
var data = {};
if (!data[String1]) {
data[String1] = {}; // make sure that data[String1] exists and is an object
}
data[String1][String2] = String3;
If you want to do this for elements in the array, you have to iterate over the array.
P.S.: I recommend to use more expressive variable names than StringX.
first create the constructor (in OOP terminology):
var ctor_object = function(letter,id_categ,response)
{
this.letter = letter;
this.id_cated = id_categ;
this.response = response;
}
(in genereal you should omit the ctor_ syntax and name it directly after the name of the class of your object)
then use your constructor upon your list of categories:
var length = liste_path_categories.length,
element = null;
for (var i = 0; i < length; i++)
{
element = liste_path_categories[i];
my_obj = new ctor_object(element.letter,element.id_categ,element.reponse)
// Do something with my_obj
}

Find specific key value in array of objects

This is the code:
var groups = {
"JSON":{
"ARRAY":[
{"id":"fq432v45","name":"Don't use me."},
{"id":"qb45657s","name":"Use me."}
]
}
}
I want to get the name value where the id is "qb45657s" how could this be accomplished? I figured the obvious loop through all of the array and check if it's equal but is there an easier way?
Edit: I cannot change "Array" to an object because I need to know the length of it for a different function.
You can simply filter on the given id:
groups["JSON"]["ARRAY"].filter(function(v){ return v["id"] == "qb45657s"; });
This will return [{"id":"qb45657s","name":"Use me."}]
Assuming you had a valid JSON string like this (note I say valid, because you need an enclosing {} or [] to make it valid):
var json = '{"JSON":{
"ARRAY":[
{"id":"fq432v45","name":"Don't use me."},
{"id":"qb45657s","name":"Use me."}
]
}
}';
You would just parse it into an actual object like this:
var jsonObj = JSON.parse(json); // makes string in actual object you can work with
var jsonArray = jsonObj.JSON.ARRAY; // gets array you are interested in
And then search for it like:
var needle = 'qb45657s';
var needleName;
for (var i = 0; i < jsonArray.length; i++) {
if (jsonArray[i].id === needle) {
needleName = jsonArray[i].name;
}
}

javascript removing items from array

I have "scene" with graphics "objects"...
Scene.prototype.objects=new Array();
Scene.prototype.add=function(obj){
var last=this.objects.length;
this.objects[last]=obj}
Scene.prototype.remove=function(obj){
this.objects.splice(obj.id,1)}
Scene.prototype.advance=function(){
for (var id in this.objects){
var obj=this.objects[id];
obj.id=id;
obj.advance();
}
}
Scene.prototype.paint=function(context){...}
each time creating and deleting many objects. Array.prototype.splice re-index array right? Does anyone know a better technique (adding and removing on javascript Array)?
In my opinion, is another possibility to do that something like
Scene.prototype.remove=function(obj){
delete this.objects[obj.id]; // don,t care about this.objects.length
delete obj; // not necessary...
}
I have not tried it yet...
I need a good book about JavaScript :)
Your delete method wouldn't work, because objects is an Array, and obj.id is the id of the object reference stored in an element in that Array. splice would be the method to use, but you'll have to know the index of the element in the Array. Maybe you should 'remeber' it this way:
Scene.prototype.add=function(obj){
var last=this.objects.length;
obj.objectsIndex = last;
this.objects[last]=obj
}
After which you can:
Scene.prototype.remove=function(obj){
this.objects.splice(obj.objectsIndex,1)};
//reindex the objects within the objects Array
for (var i=0; i<this.objects.length;i++){
this.objects[i].objectsIndex = i;
}
}
Note: Adding the objects Array to the prototype of your Scene constructor means it will be the same for all instances (static), is that what you want?
It seems you don't actually need an array for your objects. You can use another object has hash table:
(function() {
var i = 0;
Scene.prototype.objects = {};
Scene.prototype.add = function(obj) {
var id = i++;
this.objects[id] = obj;
obj.id = id;
};
Scene.prototype.remove = function(obj){
if(obj.id in this.objects) {
delete this.objects[obj.id];
}
};
Scene.prototype.advance=function(){
for (var id in this.objects){
var obj=this.objects[id];
obj.id=id;
obj.advance();
}
};
Scene.prototype.paint=function(context){...}
}());
I'd rather avoid using new Array() instead use [] (page 114 of book I mentioned in comment - 'JavaScript: The Good Parts' by Douglas Crockford ;)).
What's more, I don't think adding objects array to Scene prototype will work as you expect. You probably want to achieve some Object-Oriented structure, which requires some acrobatic skills in JavaScript, as it uses prototypal inheritance as it is class-free.
Try something like this:
var Scene = function(initial_objects) {
this.objects = initial_objects || [];
};
Scene.prototype.add = function(obj) {
this.objects.push(obj);
};
Scene.prototype.remove_by_index = function(index) {
this.objects.splice(index, 1);
};
Scene.prototype.remove = function(obj) {
var index = this.objects.indexOf(obj);
if (index > -1) {
this.objects.splice(index, 1);
}
};
Read also this: http://javascript.crockford.com/inheritance.html

Categories