Answer the question
In order to leave comments, you need to log in
How to name a property in an object?
The essence of the question is that I need to make a cycle that, depending on the id, will display data from the object.
Roughly speaking, there is an array:
var names = [
{ id: 1, name: 'Филиппов А. В.' },
{ id: 2, name: 'Иванова А. М.' },
{ id: 3, name: 'Петров В. В.' }
];
how can you refer to any of the array objects not by index, but by id ? var names = {1: "Антон", 2: "Маша"}
, it doesn’t work, because you can’t refer tonames.1
Answer the question
In order to leave comments, you need to log in
I see three options:
1. There may be a "sparse" array in js, however, it will not be so convenient to work with it (array traversal will be different, for example).
x = []
x[5] = {...}
x[10] = {...}
names = {
1: {...}, 2: {..}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question