¿Cómo escribir una función en JavaScript que devolverá una matriz?
Frecuentes
Visto 88 equipos
-1
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.
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
2 Respuestas
0
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:
No such keyword as
int
.push()
()
no,[]
as it's a function callVariable types are not explicit in Javascript. No need for
[]
after a variable name to define an array.
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
0
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 javascript jquery or haz tu propia pregunta.
¿Cuál es tu pregunta? - JJJ
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 - user663724
You have several syntactical errors, you should really use a debugger:
var retdata[] = returnValues(value);
debiera servar retdata = returnValues(value);
,for (int i = 0; i < retdata.length; i++)
debiera serfor (var i = 0; i < retdata.length; i++)
... - Korikulum@Korikulum , initially i tried with taht approach only , but it threw an error in console saying undefined . - user663724
What exactly was undefined, which variable? - Korikulum