Reputation: 13
I'm trying to make a dictionary of other dictionaries work but just work "sometimes". I have this dictionaries that i call "books" in each of them there are a group of objects of the same model.
So I turn this arrays of objects of the same model into this "books"/dictionaries and then I gather the books inside a "library"/dictionary.
The idea is being able to access every book from the same place like: "library.book[object key]"
When I console log the whole library every book is there..
Object { } education: Object { } : Object { … } experience: Object { "L&A Construcciones": {…}, Aegis: {…}, test123: {…}, … } matt: Object { id: 1, first_name: "Rodrigo", last_name: "Luna", … } projects: Object { test: {…}, "this is another test": {…}, "yet another test": {…} } skills: Object { } : Object { … } app.component.ts:88:12
..but when i try to access a book(or console.log it) gives me a error that says that the book is undefined.
Im totally missing the problem and i need some help
Edit. This is a part of the complete code. So It works like this on init first calls a get"book" that gets the data from a spring data base. This data comes as an array. So in gatherMuseLibrary i turn this array into a dictionary object and put it inside another dictionary using the "bookName" as the key and then i access the book inside of museLibrary to make some arrays that i need for another part of the programm. This part as no trouble at all. I can actually access the books at this point and make the arrays like "codingSkills".
The problem is when i call "getSubjects". When i console.log the whole library inside of "getSubjects" there is no problem, i can see every book. But when i try to access each book to get some info the books are undefined. And I'm using the same method to access the books that in the get"Book" with "keys()".
I hope this helps :/
codingSkills: Array<SkillModel> = [];
writingSkills: Array<SkillModel> = [];
lifeSkills: Array<SkillModel> = [];
//Muse Needs
museLibrary: {[key: string]: {[key: string]: any}}= {};
ngOnInit(): void {
this.getSkills();
this.getSubjects();
}
getSkills(){
this.skillService.getAllSkills().subscribe(data =>{
this.gatherMuseLibrary("skills", data)
if (this.museLibrary.skills !== undefined){
for (let key of Object.keys(this.museLibrary.skills)){
let skill = this.museLibrary.skill[key]
switch(skill.type){
case "Coding": this.codingSkills.push(skill); break;
case "Writing": this.writingSkills.push(skill); break;
case "Living": this.lifeSkills.push(skill); break;
}
}
}
})
}
gatherMuseLibrary(bookName: string, data: any){
const book: {[key: string]: any} = {};
for (let element of data){
book[element.name] = element;
}
this.museLibrary[bookName] = book;
}
getSubjects(){
console.log(this.museLibrary);
console.log(this.museLibrary.skills);
for (let bKey of Object.keys(this.museLibrary)){
console.log(this.museLibrary[bKey])
let book = this.museLibrary[bKey];
console.log(book);
for (let key of Object.entries(book)){
let value = book.key
console.log(`${key} -> ${value}`);
}
}
}```
Upvotes: 0
Views: 206
Reputation: 707
This loop doesn't work, unless you define an iterator on your book objects (which doesn't seem to be the case)
for (let key of obj){}
Try to console.log(this.museLibrary[bKey])
to make sure the book is being saved correctly.
Upvotes: 1