7
votes

Tableau d'objets JSON

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: xxx

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. .

Puis-je créer un tableau d'objets JSON ou y a-t-il autre chose avec JSON qui accomplit cet objectif?

Merci beaucoup pour votre aide.


2 commentaires

Oui, vous pouvez créer un tableau d'objets.


Techniquement, un tableau 2D est 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 () et Nouvelle image () ) pourrait être qualifié de JSON . Mais vous prenez vraiment la meilleure approche en utilisant des objets avec des valeurs nommées.


4 Réponses :


7
votes

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" } 
] }


1 commentaires

Comment faire pouvez-vous s'il vous plaît dites-moi?



3
votes
[
    { "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" }
]

1 commentaires

La question initiale demandait comment manipuler le champ distance puis trier sur cette valeur.



7
votes
// 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; });

0 commentaires

7
votes

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));


0 commentaires