Getting an object within an object within an object - javascript

In Javascript I have an array of clients. Each client is an object that has inside an array of vehicles which are also objects. Now I need to get another array of objects named trabajos that should be nested inside the vehicles. All should be stored in the localStorage, but I am not sure how I should start getting the trabajos inside the vehicles. Here is how I gotten the first two parts.
function getClientes(){
let listaClientes = JSON.parse(localStorage.getItem('listaClientesLS'));
let clientes = [];
if (listaClientes == null) {
clientes = [];
} else{
listaClientes.forEach(obj =>{
let objCliente = new Cliente(obj.nombre, obj.apellido1, obj.apellido2, obj.cedula, obj.telefono, obj.email);
obj.listaVehiculos.forEach(objVehiculoTemp => {
let objVehiculo = new Vehiculo(objVehiculoTemp.matricula,objVehiculoTemp.marca,objVehiculoTemp.modelo,objVehiculoTemp.anno,objVehiculoTemp.capacidad,objVehiculoTemp.kilometraje);
objCliente.agregarVehiculo(objVehiculo);
});
clientes.push(objCliente);
})
}
return clientes;
}

The github repository (the question is not minimal) suggests jobs ("trabajos") for a vehicle should be stored in the listaTrabajos array property of a vehicle object, placed there by the add job method (agregarTrabajo) of a the vehicle class, called from the job registration function regTrabajo.
But the vehicle class does not define the agregarTrabajo method. I expect regTrabajo throws an error when called. (Note errors on the console should be solved before anything else! You can ask a question about an error if you can't solve it.)
Add the agregarTrabajo method to the Vehiculo class, e.g.
class Vehiculo {
constructor(....
//... constructor function
}
agregarTrabajo (pObjTrabajo) {
this.listaTrabajos.push(pObjTrabajo);
}
}
Now the list of jobs can be accessed using the listaTrabajos property of the objVehiculoTemp argument of the function processing each vehicle in the post. I don't know what processing is required:
obj.listaVehiculos.forEach(objVehiculoTemp => {
let objVehiculo = new Vehiculo(objVehiculoTemp.matricula,objVehiculoTemp.marca,objVehiculoTemp.modelo,objVehiculoTemp.anno,objVehiculoTemp.capacidad,objVehiculoTemp.kilometraje);
// process objVehiculoTemp.listaTrabajos
objVehiculo.listaTrabajos = objVehiculoTemp.listaTrabajos; // FOR EXAMPLE
objCliente.agregarVehiculo(objVehiculo);
});

Related

Why does this getter method has been called several times in React?

I have a Store which will be provided to the component. In this Store file, there are several getter function. But I find only this getter function will be executed three times since this.rawMonthlyImpacts will be only changed once when the api get response from backend. I am so confused because other getter function in this file will be only executed once. During every execution, this.rawMonthlyImpacts is always same. Because this function is time-consuming, so I want to figure out why this happens. Hope you can give me some advice. Thanks!
get Impacts(){
const monthlyImpacts = new Map<string, Map<string, number>>();
if (this.rawMonthlyImpacts) {
this.rawMonthlyImpacts.forEach((impact) => {
if (impact.Impact > 0) {
const month = TimeConversion.fromTimestampToMonthString(impact.Month);
const tenantId = impact.TenantId;
const tenantImpact = impact.Impact;
if (!monthlyImpacts.has(month)) {
const tenantList = new Map<string, number>();
monthlyImpacts.set(month, tenantList.set(tenantId, tenantImpact));
} else {
const tenantWithImpactMap = monthlyImpacts.get(month);
if (!tenantWithImpactMap.has(tenantId)) {
tenantWithImpactMap.set(tenantId, tenantImpact);
} else {
tenantWithImpactMap.set(tenantId, tenantWithImpactMap.get(tenantId) + tenantImpact);
}
monthlyImpacts.set(month, tenantWithImpactMap);
}
}
});
}
return monthlyImpacts;
},
Update: I have find that there are other two functions use this.Impacts. If I remove these two functions, the getter function will only be executed only once. I think the getter function uses the cache to store data, so once the data is calculated for the first time, subsequent calls to the getter function should not be re-executed, only the value in the cache needs to be retrieved. So I am very confused about why this getter function will be executed 3 times.
getImpactedTenants(month: string): string[] {
return Array.from(this.Impacts.get(month).keys());
},
get overallMonthlyImpactedTenants(): Map<string, number> {
return new Map<string, number>(
Array.from(this.Impacts)?.map((monthEntries) => {
const month = monthEntries[0];
const impactedTenants = monthEntries[1].size;
return [month, impactedTenants];
})
);
}
Hard to tell what exactly is happening without more context, but remember that with a get function, every single time you reference that property (.Impacts in this case) the get function will be called.
Assuming that each impact stored in this.rawMonthlyImpacts which you loop through is an instance of the class with this getter, then as far as I'm aware, you are calling the get function each time you reference impact.Impacts, such as in the conditional:
if (impact.Impact > 0) {
I might be way off though; I'm unfamiliar with React and so my answer is based only on my experience with vanilla JS.

I am trying to call displayUsers with an inline array containing the new user object that i added to users array

//here is displayUsers
const displayUsers = (users) => {users.forEach((user) => {
const option = document.createElement("OPTION"); const name =
document.createTextNode(user.name);
option.value = user.id;
option.appendChild(name);
document.querySelector('select').appendChild(option);
});
};
//My user object added to users array
var[user]=(results);
console.log(user);
users.push(user);
console.log(users);`
//My main problem. This does not work
displayUsers(new [users]);`
I expect the below result but I am not getting it. Any pointers, please?
Add user object to our users array and then call displayUsers with an inline array containing the new user object.
according to your instructions just call the function and make the parameter an array. so you will convert the object user to an array. So you will call the function this way:
displayUsers([user]); // converts user object to array and pass it to function
Destructuring works when creating a variable - here, you'd have to use users[0]:
displayUsers(new users[0]());
This also assumes that users[0] is a constructor function - if not, then just do:
displayUsers(users[0]);

How to include or detect the name of a new Object when it's created from a Constructor

I have a constructor that include a debug/log code and also a self destruct method
I tried to find info on internet about how to detect the new objects names in the process of creation, but the only recommendation that I found was pass the name as a property.
for example
var counter = {}
counter.a =new TimerFlex({debug: true, timerId:'counter.a'});
I found unnecessary to pass counter.a as a timerId:'counter.a' there should be a native way to detect the name from the Constructor or from the new object instance.
I am looking for something like ObjectProperties('name') that returns counter.a so I don't need to include it manually as a property.
Adding more info
#CertainPerformance What I need is to differentiate different objects running in parallel or nested, so I can see in the console.
counter.a data...
counter.b data...
counter.a data...
counter.c data... etc
also these objects have only a unique name, no reference as counter.a = counter.c
Another feature or TimerFlex is a method to self desruct
this.purgeCount = function(manualId) {
if (!this.timerId && manualId) {
this.timerId = manualId;
this.txtId = manualId;
}
if (this.timerId) {
clearTimeout(this.t);
this.timer_is_on = 0;
setTimeout ( ()=> { console.log(this.txtId + " Destructed" ) },500);
setTimeout ( this.timerId +".__proto__ = null", 1000);
setTimeout ( this.timerId +" = null",1100);
setTimeout ( "delete " + this.timerId, 1200);
} else {
if (this.debug) console.log("timerId is undefined, unable to purge automatically");
}
}
While I don't have a demo yet of this Constructor this is related to my previous question How to have the same Javascript Self Invoking Function Pattern running more that one time in paralel without overwriting values?
Objects don't have names - but constructors!
Javascript objects are memory references when accessed via a variables. The object is created in the memory and any number of variables can point to that address.
Look at the following example
var anObjectReference = new Object();
anObjectReference.name = 'My Object'
var anotherReference = anObjectReference;
console.log(anotherReference.name); //Expected output "My Object"
In this above scenario, it is illogical for the object to return anObjectReference or anotherReference when called the hypothetical method which would return the variable name.
Which one.... really?
In this context, if you want to condition the method execution based on the variable which accesses the object, have an argument passed to indicate the variable (or the scenario) to a method you call.
In JavaScript, you can access an object instance's properties through the same notation as a dictionary. For example: counter['a'].
If your intent is to use counter.a within your new TimerFlex instance, why not just pass counter?
counter.a = new TimerFlex({debug: true, timerId: counter});
// Somewhere within the logic of TimerFlex...
// var a = counter.a;
This is definitely possible but is a bit ugly for obvious reasons. Needless to say, you must try to avoid such code.
However, I think this can have some application in debugging. My solution makes use of the ability to get the line number for a code using Error object and then reading the source file to get the identifier.
let fs = require('fs');
class Foo {
constructor(bar, lineAndFile) {
this.bar = bar;
this.lineAndFile = lineAndFile;
}
toString() {
return `${this.bar} ${this.lineAndFile}`
}
}
let foo = new Foo(5, getLineAndFile());
console.log(foo.toString()); // 5 /Users/XXX/XXX/temp.js:11:22
readIdentifierFromFile(foo.lineAndFile); // let foo
function getErrorObject(){
try { throw Error('') } catch(err) { return err; }
}
function getLineAndFile() {
let err = getErrorObject();
let callerLine = err.stack.split("\n")[4];
let index = callerLine.indexOf("(");
return callerLine.slice(index+1, callerLine.length-1);
}
function readIdentifierFromFile(lineAndFile) {
let file = lineAndFile.split(':')[0];
let line = lineAndFile.split(':')[1];
fs.readFile(file, 'utf-8', (err, data) => {
if (err) throw err;
console.log(data.split('\n')[parseInt(line)-1].split('=')[0].trim());
})
}
If you want to store the variable name with the Object reference, you can read the file synchronously once and then parse it to get the identifier from the required line number whenever required.

Calculated Class property from another Class Property (ES6)

I have a class with a constructor and as below:
class Bookshelf {
constructor(author, publisher) {
this.books = [],
this.numBooks = this.books.length
this.author = author,
this.publsiher = publisher
}
}
I am not getting back the value of the book array which is what I truly want to do. I know this is causing the value not to be grabbed but I really want to understand why?
I interpret this question, based on the title, to be asking why numBooks doesn't update after you add an entry to books within a Bookshelf you create.
When you run the statement this.numBooks = this.books.length, it does set a property called numBooks to the calculated value this.books.length, which is 0. Later, you might add a value to books, but that doesn't affect numBooks.
The problem isn't about calculating one "class property" from another so much as understanding that numBooks stores a number, and that number doesn't update automatically. Your constructor runs once, and that's it.
Compare with this:
class Bookshelf {
constructor(author, publisher) {
this.books = [];
this.author = author;
this.publisher = publisher;
}
getNumBooks() {
return this.books.length;
}
}
let myBookshelf = new Bookshelf("Isaac Asimov", "Bantam Press");
myBookshelf.books.push("I, Robot");
console.log(myBookshelf.getNumBooks()); // prints 1
Because your call to getNumBooks() runs code, it can calculate the dynamic value this.books.length, which is why it gives you an up-to-date value where the property numBooks doesn't update automatically. If you'd rather keep numBooks as a property, you also have the option of making the books property private, and instead exposing an addBook function that adds a book and resets numBooks accordingly.
As Patrick reminds me in the comments, since you specified ES6, you can use getter syntax to write a function that acts like a property. This works because it runs code when you refer to numBooks, even though the syntax hides that detail away.
class Bookshelf {
constructor(author, publisher) {
this.books = [];
this.author = author;
this.publisher = publisher;
}
get numBooks() {
return this.books.length;
}
}
let myBookshelf = new Bookshelf("Isaac Asimov", "Bantam Press");
myBookshelf.books.push("I, Robot");
console.log(myBookshelf.numBooks()); // prints 1

Passed-in Values Not Available at Run-time of Function in Angular App

I realize there is something I'm missing in terms of how and specifically when the products of certain functions are available in JavaScript.
In my Angular app, in order to get a user's initials, I am parsing data being returned from the API, and retrieving the first letter of the firstName, as well as the first letter of lastName in two different functions. These two functions are working as expected, and I can see the correct results in the console:
getFirstNameFirstLetter() {
if (this.authenticationService.isAuthenticated()) {
const userObj = JSON.parse(sessionStorage.getItem('currentUser'));
const userInfo = userObj.data;
const firstName = userInfo.name.first;
const firstNameFirstLetter = firstName.trim().charAt(0);
console.log(firstNameFirstLetter);
return firstNameFirstLetter;
}
}
getLastNameFirstLetter() {
if (this.authenticationService.isAuthenticated()) {
const userObj = JSON.parse(sessionStorage.getItem('currentUser'));
const userInfo = userObj.data;
const lastName = userInfo.name.last;
const lastNameFirstLetter = lastName.trim().charAt(0);
console.log(lastNameFirstLetter);
return lastNameFirstLetter;
}
}
Now comes the part I'm not fully understanding. When I then pass the returned values of these two functions, in order to get the initials, like this:
getInitials(firstNameFirstLetter, lastNameFirstLetter) {
if (this.authenticationService.isAuthenticated()) {
if (!this.firstNameFirstLetter || !this.lastNameFirstLetter) {
console.log('Names not ready!');
return;
} else if (this.firstNameFirstLetter && this.lastNameFirstLetter) {
console.log(firstNameFirstLetter + lastNameFirstLetter);
return firstNameFirstLetter + lastNameFirstLetter;
}
}
}
... I get "Names not ready!" printed to the console each time.
By the way, I am running these functions within Angular's ngOnInit life cycle hook, like this:
ngOnInit() {
this.getFirstNameFirstLetter();
this.getLastNameFirstLetter();
this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}
I know this has something to do with what's available when, because I get 'undefined' when I use break points and debug the two values being passed into the "getInitials()" function. In other words, the function doesn't have access to the returned values of the other two functions at the time it's run -- hence I'm getting 'Names not ready!' printed to the console. My question is, what am I missing, architecturally, to resolve this kind of issue?
So what is happening here is that JavaScript doesn't think you are using the return values for getFirstNameFirstLetter and getLastNameFirstLetter, so when it makes the call, instead of waiting for that call to finish, it goes on to the next one, which introduces a race condition. if you simply change it to
ngOnInit() {
let temp1 = this.getFirstNameFirstLetter();
let temp2 = this.getLastNameFirstLetter();
this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}
then it will wait for the previous functions to finish before calling the next.
Also, I don't use const very often, so I could be wrong and it could follow different scope rules, but by normal scope rules, setting a variable in that function, it is only available in that function, you would need to set it as
this.firstNameFirstLetter = firstName.trim().charAt(0);
to have access to it outside the function.
Or, so as to kill two birds with one stone, you could do
ngOnInit() {
this.firstNameFirstLetter = this.getFirstNameFirstLetter();
this.lastNameFirstLetter = this.getLastNameFirstLetter();
this.getInitials(this.firstNameFirstLetter, this.lastNameFirstLetter);
}
or
ngOnInit() {
let firstNameFirstLetter = this.getFirstNameFirstLetter();
let lastNameFirstLetter = this.getLastNameFirstLetter();
this.getInitials(firstNameFirstLetter, lastNameFirstLetter);
}
depending on if you need the variables again or just for that function.

Categories

Resources