0

In a nutshell I am making a REST call to my database and getting a list of ingredients back. The JSON looks like:

[{"SubItemID":1,"MainItemID":1,"SubName":"2%","MainName":"Milk"},
{"SubItemID":2,"MainItemID":1,"SubName":"Skim/Fat Free","MainName":"Milk"},
{"SubItemID":3,"MainItemID":2,"SubName":"Chedder","MainName":"Cheese"}]

What I want to do is transform this into a useful array that is organized as:

  1. Milk
    1. 2%
    2. Skim/Fat Free
  2. Cheese
    1. chedder

In addition to it being ordered like that, I want to maintain the ID associated with each item. So cheese would have "2" and chedder would have "3".

I have been able to feed the unique values of Milk and Cheese into an array but I am not sure how to proceed. Advise would be appreciated!

Here's what I have so far:

        $.ajax({
            url: "../api/IngredientChoices",
            contentType: "json",
            success: function (data) {
                var _subCategories = {};
                var _mainCategories = [];
                $.each(data, function (index, item) {
                    if ($.inArray(item.MainName, _mainCategories) === -1) {
                        _mainCategories.push(item.MainName);
                    }
                });
                $.each(_mainCategories, function () {
                    alert(this);
                });
            }
        });
2
  • Are you using JSON parse()? Commented Jan 14, 2013 at 5:44
  • @ATOzTOA I added my current Ajax code Commented Jan 14, 2013 at 5:48

3 Answers 3

1

Detailed working answer. The data is added to an associative array.

obj = JSON.parse(data);

console.log(obj);

d = {}

$.each(obj, function (index, item) {
    if (!(item.MainItemID in d)) {
        d[item.MainItemID] = [];
        d[item.MainItemID][0] = item.MainName;
        d[item.MainItemID][1] = {}
    }

    d[item.MainItemID][1][item.SubItemID] = item.SubName;
});

console.log(d);

Output:

Object {1: Array[2], 2: Array[2]}
1: Array[2]
    0: "Milk"
        1: "2%"
        2: "Skim/Fat Free"
2: Array[2]
    0: "Cheese"
        3: "Chedder"
Sign up to request clarification or add additional context in comments.

1 Comment

I think OP is asking how to refactor the JSON, not how to parse it.
1

You might want to edit your question. The 'useful' array you want is actually not an array. You might want this :
[
  {
   name : 'Milk',
   subTypes : [ '2%', 'Fat Free'],
  },
  {
   name : 'Cheese',
   subTypes : [ 'Cheddar' ],
  }
]

Comments

1

jsfiddle example

var data = [{"SubItemID":1,"MainItemID":1,"SubName":"2%","MainName":"Milk"},
{"SubItemID":2,"MainItemID":1,"SubName":"Skim/Fat Free","MainName":"Milk"},
{"SubItemID":3,"MainItemID":2,"SubName":"Chedder","MainName":"Cheese"}];

var ordered = {};

for(var i = data.length; i--;) {
  var mainItemID = data[i].MainItemID;
  if(!ordered[mainItemID]) {
    ordered[mainItemID] = {};
    ordered[mainItemID].MainName = data[i].MainName;
    ordered[mainItemID].MainItemID = mainItemID;
  }
  ordered[mainItemID][data[i].SubItemID] = data[i];
}

console.log(ordered);

And you can have a array with your object collection, that way you will be able to sort it:

var dataArray = [];

for(var k in ordered) {
  dataArray.push(ordered[k]);
}

console.log(dataArray);

If you plan more complex data parsing, or manipulation, I would recommend some model/collection framework, like: Backbonejs, SpineJS, or even use webSQL or IndexedDB.

1 Comment

You are not getting MainName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.