¿Cómo escribir una función en JavaScript que devolverá una matriz?

I have the following JSON which I am parsing using a generic function and expecting a return value.

{
    "items":
    {
        "item":
        {
            "Beverages":
                [

                    {
                        "id": "0004",
                        "name": "Thums Up",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Can":[ "250ml", "300ml","330ml", {"image":"images/0001.jpg" }],
                        "Bottle":[ "350ml",  "600ml", {"image":"images/0001.jpg" } ],
                        "Fountain":["small", "large", {"image":"images/0001.jpg" }]
                    },
                    {
                        "id": "0005",
                        "name": "Pepsi",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Can":[ "250ml", "300ml","330ml", {"image":"images/0001.jpg" }],
                        "Bottle":[ "350ml",  "600ml", {"image":"images/0001.jpg" } ],
                        "Fountain":["small", "large", {"image":"images/0001.jpg" }]
                    }
                ],

            "Snacks":
                [
                    {
                        "id": "0010",
                        "name": "Puffs",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Veg":["Veg puff", {"image":"images/0001.jpg" }],
                        "Egg":["Egg puff", {"image":"images/0001.jpg" }],
                        "Chicken":["Chicken puff", {"image":"images/0001.jpg" }]
                    },
                    {
                        "id": "0011",
                        "name": "Samosa",
                        "image":{"url": "images/0001.jpg","width": 200,"height": 200},
                        "Aloo Samosa":["small", "big", {"image":"images/0001.jpg" }],
                        "Corn Samosa":["small", "big", {"image":"images/0001.jpg" }]
                    }
                ]

        }
    }
}

In my program, I am passing the value and expecting an array from that.

This is my complete program. I am using jQuery for this.

http://jsfiddle.net/DH8K2/

I am doing it this way:

$(document).ready(function (jsondata) {
    var value = 'Beverages';
    var retdata[] = returnValues(value);
    for (int i = 0; i < retdata.length; i++) {
        alert(retdata[i]);
    }
});

function returnValues(value) {
    var data = [];
    $.each(jsondata.items.item, function (i, v) {
        if (i == value) {
            data.push[i];
        }
    });

    return data;
}

I am a Java developer with very minimal JS knowledge so if this way is completely wrong also please let me know, so that I can change that accordingly.

sorry if this is incomlete question ,and made you confusing .

my question is I mean if you see the json above ,and if i pass the value Pepsi value directly , can i get image , Can , Bottle and Fountain values ?? and similarly if i pass the value Beverages , i should get all the child elemnts of Beverages

preguntado el 28 de mayo de 14 a las 12:05

¿Cuál es tu pregunta? -

I mean if you see the json above ,and if i pass the value Pepsi value directly , can i get image , Can , Bottle and Fountain values ?? and similarly if i pass the value Beverages , i should get all the child elemnts of Beverages -

You have several syntactical errors, you should really use a debugger: var retdata[] = returnValues(value); debiera ser var retdata = returnValues(value);, for (int i = 0; i < retdata.length; i++) debiera ser for (var i = 0; i < retdata.length; i++)... -

@Korikulum , initially i tried with taht approach only , but it threw an error in console saying undefined . -

What exactly was undefined, which variable? -

2 Respuestas

It looks like you wrote this in Java, then tried to change a few keywords to get it to work in your browser.

Problems you had:

  1. No such keyword as int

  2. .push() ()no, [] as it's a function call

  3. Variable types are not explicit in Javascript. No need for [] after a variable name to define an array.

Here's a JSFiddle that works.

contestado el 28 de mayo de 14 a las 12:05

thanks for the answer , incase i pass Beverages , is it possible that it returns me the child element names of it , that is pepsi and thumps up in this case ?? - user663724

If you do another loop to go through each Beverages object properties, then yes. - Sam P

Thanks Sam , instead of this way , is it possible to write a generic function so that on passing the name it will return me the array of that ?? - user663724

A generic function to deep search an object for arrays matching a given name? - Sam P

maybe this is what you're looking for:

var obj = $.parseJSON(dataJson);

contestado el 28 de mayo de 14 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.