10

I am trying to re-implement a page using JSON instead of some 2-dimensional arrays.

What I am hoping to accomplish is get an array of objects. The objects would look like this:

{ // Restaurant
  "location" : "123 Road Dr",
  "city_state" : "MyCity ST",
  "phone" : "555-555-5555",
  "distance" : "0"
}

I want to create an array of these restaurant objects and populate the distance field with some logic, then sort the array based on the distance field.

Can I create an array of JSON objects or is there something else with JSON that accomplishes this goal?

Thanks very much for your help.

2
  • 1
    Yes, you can create an array of objects. Commented Jul 22, 2011 at 14:27
  • Technically, a 2D array is valid JSON. In fact, pretty much any object that doesn't have special abilities (ie. DOM objects and things like new Date() and new Image()) could qualify as JSON. But you are definitely taking the better approach by using objects with named values. Commented Jul 22, 2011 at 14:37

4 Answers 4

10
// 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; });
Sign up to request clarification or add additional context in comments.

Comments

8

First of all, this is not JSON at all, you are just using Javascript objects. JSON is a text format for representing objects, there is no such thing as a "JSON object".

You can create a constructor for your objects like this:

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

Comments

7

Sure you can. It would look something like this:

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

The outer field name of "restaurants" is not necessary of course, but it may help if you are including other information in the data you're transferring.

1 Comment

How to do it can you please tell me?
3
[
    { "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 Comment

The original question was asking about how to manipulate the distance field and then sort on that value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.