Answer the question
In order to leave comments, you need to log in
How to organize select for JSON?
Hello!
There is data in JSON format as a file.
Example: there are many vehicle objects (V), each of which has its own manufacturer, color, etc. For each vehicle object, you can select the type: motorcycle or car. Each type of vehicle has its own properties that hypothetically do not change, for example, the maximum speed. So in JS I would write something like this:
var type_1 = {
max_speed: 300
},
type_2 = {
max_speed: 260
},
transport_1 = {
color: "red",
type: type_2
},
transport_2 = {
color: "blue",
type: type_1
}
}
Answer the question
In order to leave comments, you need to log in
var data = {
types: {
type_1: {
max_speed: 300
},
type_2: {
max_speed: 260
}
},
TC: {
transport_1: {
color: "red",
type: "type_2"
},
transport_2: {
color: "blue",
type: "type_1"
}
}
}
var tc = data.TC.transport_1;
console.log("Car:", tc.color); //red
console.log("Speed:", data.types[tc.type].max_speed); //260
{
"types": {
"type_1": {
"max_speed": 300
},
"type_2": {
"max_speed": 260
}
},
"TC": {
"transport_1": {
"color": "red",
"type": "type_2"
},
"transport_2": {
"color": "blue",
"type": "type_1"
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question