Answer the question
In order to leave comments, you need to log in
Variable in object call in javascript?
There is an object of the form
var book = {
author__1: {
name: 'John',
surname: 'Smith'
},
author__2: {
name: 'John',
surname: 'Smith'
}
};
console.log(book.author__1.name);
book.author__1.name
? The meaning in the loop goes through all author__{число}
of them, pulling data out of them somehow like this:for (var i=1; i<max; i++) {
var currentauthor // генерированный каким-то образом
console.log(book.currentauthor .name);
}
Answer the question
In order to leave comments, you need to log in
var book = {
author__1: {
name: 'John',
surname: 'Smith'
},
author__2: {
name: 'John',
surname: 'Smith'
}
};
var max = Object.keys(book).length;
for (var i = 1; i <= max; i++) {
console.log(book['author__' + i].name);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question