J'essaie de ré-implémenter une page à l'aide de JSON au lieu de des matrices à 2 dimensions.
Ce que j'espère accomplir est d'obtenir un tableau d'objets. Les objets ressembleraient à ceci: p> Je souhaite créer un tableau de ces objets de restaurant et peupler le champ de distance avec une certaine logique, puis trier la matrice en fonction du champ de distance. . P> Puis-je créer un tableau d'objets JSON ou y a-t-il autre chose avec JSON qui accomplit cet objectif? P> Merci beaucoup pour votre aide. P> P>
4 Réponses :
sûr de votre pouvoir. Cela ressemblerait à ceci:
{ "restaurants": [
{ "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" } ,
{ "location" : "456 Fake St", "city_state" : "MyCity ST", "phone" : "555-123-1212", "distance" : "0" }
] }
Comment faire pouvez-vous s'il vous plaît dites-moi?
[
{ "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" },
{ "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" },
{ "location" : "123 Road Dr", "city_state" : "MyCity ST", "phone" : "555-555-5555", "distance" : "0" }
]
La question initiale demandait comment manipuler le champ code> distance code> puis trier sur cette valeur.
// You can declare restaurants as an array of restaurant objects
restaurants =
[
{
"location" : "123 Road Dr",
"city_state" : "MyCity ST",
"phone" : "555-555-5555",
"distance" : "1"
},
{
"location" : "456 Avenue Crt",
"city_state" : "MyTown AL",
"phone" : "555-867-5309",
"distance" : "0"
}
];
// Then operate on them with a for loop as such
for (var i = 0; i< restaurants.length; i++) {
restaurants[i].distance = restaurants[i].distance; // Or some other logic.
}
// Finally you can sort them using an anonymous function like this
restaurants.sort(function(a,b) { return a.distance - b.distance; });
Tout d'abord, ce n'est pas du tout JSON, vous utilisez simplement des objets JavaScript. JSON est un format de texte pour représenter des objets, il n'existe pas d'objet "JSON".
Vous pouvez créer un constructeur pour vos objets comme celui-ci: p>
function Restaurant(location, city_state, phone, distance) {
this.location = location;
this.city_state = city_state;
this.phone = phone;
// here you can add some logic for the distance field, if you like:
this.distance = distance;
}
// create an array restaurants
var restaurants = [];
// add objects to the array
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
restaurants.push(new Restaurant("123 Road Dr", "MyCity ST", "555-555-5555", 0));
Oui, vous pouvez créer un tableau d'objets.
Techniquement, un tableau 2D est i> Valide JSON . En fait, à peu près n'importe quel objet qui n'a pas de capacités particulières (c.-à-d. Des objets DOM et des objets tels que
nouvelle date () code> etNouvelle image () code>) pourrait être qualifié de JSON . Mais vous prenez vraiment la meilleure approche en utilisant des objets avec des valeurs nommées.