3

I have got the array as following:

[
    {
        "name": "847_KK_0_KK_0tab1_checkbox",
        "cost": 10,
        "additionname": "Topping mango1",
        "addtionid": 100
    },
    {
        "name": "847_KK_1_KK_0tab1_checkbox",
        "cost": 12,
        "additionname": " Topping mango2",
        "addtionid": 101
    },
    {
        "name": "847_KK_1_KK_0tab2_checkbox",
        "cost": 12,
        "additionname": " Topping mango2",
        "addtionid": 111
    },
    {
        "name": "847_KK_1_KK_0tab2_checkbox",
        "cost": 12,
        "additionname": " Topping mango2",
        "addtionid": 111
    }
]

In the above array the name tab1_checkbox refers to the first quantity. Similarly the name tab2_checkbox refers to the second quantity.

I need to categorize the data as:

for (var i = 0; i < toppcrustsdata.length; i++) {
            var name = toppcrustsdata[i].name;
            var additionname = toppcrustsdata[i].additionname;

             toppings_quantitywise.push({
                        'name': 'Quantity'+,
                        'value': values
                    });

        }

My fiddle: http://jsfiddle.net/7ku720du/7/

The desired output i am looking for is

[
    {
        "name": "Qty1",
        "values" : ["Topping mango1", "Topping mango2"]
    },
    {
        "name": "Qty2",
        "values" : ["Topping mango2", "Topping mango2"]
    }

]

I want to create the following javascript array with Jquery

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 1
    What's the use of plus(+) in `'Quantity'+` – Satpal Mar 23 '15 at 07:38
  • What are you trying to do? Can you show the result you want after processing the data. – Huy Hoang Pham Mar 23 '15 at 08:06
  • What is the expected JSON structure? – ArinCool Mar 23 '15 at 08:10
  • Dude, OP, your question started off great with that nice clean array of objects and then turned in to nonsensical nothing when you listed the 2nd block of code. Instead of the entire 2nd block of code, just list another nice clean array of objects that you DESIRE. We'll take care of telling you the code to convert 'Array 1' into desired 'Array 2'. – FactoryAidan Mar 23 '15 at 08:15
  • The desired output i am looking is [ { "name": "Qty1", "values" : "Topping mango1" , "Topping mango2" }, { "name": "Qty2", "values" : "Topping mango2" , "Topping mango2" } ] – Pawan Mar 23 '15 at 08:28
  • @ArinCool , i have pasted the desired JSON structure i am looking for , please see my edited question . – Pawan Mar 23 '15 at 08:30
  • What you pasted is not JSON. It is a javascript object – Kirill Slatin Mar 23 '15 at 08:38
  • While this question is flagged, take a look at this [post](http://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – Kirill Slatin Mar 23 '15 at 08:44
  • http://jsbin.com/nelijosemi/2/edit – Klaster_1 Нет войне Mar 23 '15 at 09:05
  • I pasted your code in jsfiddle , but it is not working in fiddle , i am getting an error Uncaught ReferenceError: _ is not defined could you pleaselet me know how to resolve it . – Pawan Mar 23 '15 at 09:08

0 Answers0