获取数组中的父索引

时间:2013-03-06 15:47:39

标签: jquery json

我有一个数组

[
{
    "code": "MENU000014",
    "syskey": 2012071310410973022,
    "description": "Jiang Nan Dim Sum & Xiao Long Bao",
    "price": 0.0,
    "images": "kp-1.jpg",
    "imageb": "kp-1.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 0,
    "menutype": 1,
    "priceoperator": 0,
    "chineseDescription": ""
},
{
    "code": "MENU000019",
    "syskey": 2012071310571230035,
    "description": "Mix &  Match Multi-flavoured Xiao Long Bao",
    "price": 0.0,
    "images": "",
    "imageb": "",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012071310410973022,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "雪碧é…香è‰é›ªç³•"
},
{
    "code": "MENU000020",
    "syskey": 2012071310571230036,
    "description": "Immediate preparation for Jiang Nan Speciality",
    "price": 0.0,
    "images": "",
    "imageb": "",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012071310410973022,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "雪碧é…香è‰é›ª"
},
{
    "code": "MENU000002",
    "syskey": 2012071305045623001,
    "description": "Jiang Nan Appetizer",
    "price": 0.0,
    "images": "kp-4.jpg",
    "imageb": "kp-4.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 0,
    "menutype": 1,
    "priceoperator": 0,
    "chineseDescription": ""
},
{
    "code": "MENU000024",
    "syskey": 2012111809055570004,
    "description": "Soup",
    "price": 0.0,
    "images": "kp-5.jpg",
    "imageb": "kp-5.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 0,
    "menutype": 1,
    "priceoperator": 0,
    "chineseDescription": ""
},
{
    "code": "MENU000003",
    "syskey": 2012071305062942004,
    "description": "Double-Boiled Soup",
    "price": 0.0,
    "images": "0207_s.jpg",
    "imageb": "0207.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012111809055570004,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "原盅炖汤"
},
{
    "code": "MENU000004",
    "syskey": 2012071305080136007,
    "description": "Soup & Thick Soup",
    "price": 0.0,
    "images": "kp-5.jpg",
    "imageb": "kp-5.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012111809055570004,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "羹。生滚汤"
},
{
    "code": "MENU000022",
    "syskey": 2012111809055570002,
    "description": "Dishes",
    "price": 0.0,
    "images": "kp-6.jpg",
    "imageb": "kp-6.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 0,
    "menutype": 1,
    "priceoperator": 0,
    "chineseDescription": ""
},
{
    "code": "MENU000001",
    "syskey": 2012071304555547001,
    "description": "Pork",
    "price": 0.0,
    "images": "0403_s.jpg",
    "imageb": "0403.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012111809055570002,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "肉类。猪"
},
{
    "code": "MENU000005",
    "syskey": 2012071306020332001,
    "description": "Prawn",
    "price": 0.0,
    "images": "0801_s.jpg",
    "imageb": "0801.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012111809055570002,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "海鲜。虾"
},
{
    "code": "MENU000006",
    "syskey": 2012071306033011004,
    "description": "Beef",
    "price": 0.0,
    "images": "0503_s.jpg",
    "imageb": "0503.jpg",
    "plucode": "",
    "style": "",
    "option": "",
    "stocksyskey": 0,
    "parentID": 2012111809055570002,
    "menutype": 2,
    "priceoperator": 0,
    "chineseDescription": "肉类。牛"
},

我有一个数组 2012071304555547001 的值现在如何获取数组的索引而不是此值的索引而不是我需要数组的索引。 jquery

中需要此代码

1 个答案:

答案 0 :(得分:2)

您是否尝试过基本的for循环?但是,您需要将syskeyparentID转换为字符串 - 这个数字对于JavaScript的某些实现来说太大了。

var index = -1;
for (var i=0; i<arr.length; i++) {
    if (arr[i].parentID === "2012071304555547001") { // use strings, not numbers
        index = i;
    };
};