【问题标题】:searching for a specific key in json and finding the previous and next similar keys在 json 中搜索特定键并找到上一个和下一个相似键
【发布时间】:2014-04-10 05:01:19
【问题描述】:

与 brnwdrng 的问题类似,我正在寻找一种方法来搜索类似 JSON 的对象并找到最后一个相似键和下一个键。 假设我的对象的结构是这样的:

TestObj = {
"Categories": [{
    "Products": [{
        "id": "a01",
        "name": "Pine",
        "description": "Short description of pine."
    },
    {
        "id": "a02",
        "name": "Birch",
        "description": "Short description of birch."
    },
    {
        "id": "a03",
        "name": "Poplar",
        "description": "Short description of poplar."
    }],
    "id": "A",
    "title": "Cheap",
    "description": "Short description of category A."
},
{
    "Product": [{
        "id": "b01",
        "name": "Maple",
        "description": "Short description of maple."
    },
    {
        "id": "b02",
        "name": "Oak",
        "description": "Short description of oak."
    },
    {
        "id": "b03",
        "name": "Bamboo",
        "description": "Short description of bamboo."
    }],
    "id": "B",
    "title": "Moderate",
    "description": "Short description of category B."
}]

};

现在我想找到一个名为“oak”的标题,因为我还想找到“oak”之前的最后一个标题,在这种情况下是“maple”,下一个标题是“bamboo”。

如果有人能指出正确的方向或给我一个伪代码,我将不胜感激。

谢谢

【问题讨论】:

  • 如果您使用 for 循环遍历一个类别中的所有产品,这很容易实现。你试过了吗?
  • 我一定会去看看谢谢。
  • 嘿,nikhil,我被困在这里了,你能帮我看看怎么做吗?我是 javascript 的新手,几乎没有经验。

标签: javascript jquery json


【解决方案1】:

做这样的事情

i = 0;
foreach obj in TestObj
   if obj.title = Oak,
      last_title = TestObj[i].title;
      next_title = TestObj[i+2].title;
      i++;
endfor

【讨论】:

  • 我会在我的代码中尝试,如果它产生我想要的结果,我会告诉你
  • 因为我对这很陌生jason 文件名 master.js,其中存储了所有 json 数据。
【解决方案2】:

好的,这里有一个快速代码。

TestObj = {
"Categories": [{
    "Products": [{
        "id": "a01",
        "name": "Pine",
        "description": "Short description of pine."
    },
    {
        "id": "a02",
        "name": "Birch",
        "description": "Short description of birch."
    },
    {
        "id": "a03",
        "name": "Poplar",
        "description": "Short description of poplar."
    }],
    "id": "A",
    "title": "Cheap",
    "description": "Short description of category A."
},
{
    "Products": [{
        "id": "b01",
        "name": "Maple",
        "description": "Short description of maple."
    },
    {
        "id": "b02",
        "name": "Oak",
        "description": "Short description of oak."
    },
    {
        "id": "b03",
        "name": "Bamboo",
        "description": "Short description of bamboo."
    }],
    "id": "B",
    "title": "Moderate",
    "description": "Short description of category B."
}]
};

var oak, beforeOak, afterOak;

for(var i=0;i<TestObj.Categories.length;i++){
    var products = TestObj.Categories[i].Products;
       for(var j=0; j< products.length;j++){
        var product = products[j];

        if(product.name === 'Oak'){
            oak = product;
            beforeOak = products[j-1];
            afterOak = products[j+1];
            break;

        }
    }
    if(oak) break;
}

alert("Oak : "+JSON.stringify(oak));
alert("Before Oak : "+JSON.stringify(beforeOak));
alert("After Oak :" + JSON.stringify(afterOak));

在这里查看它的实际效果: http://jsfiddle.net/ejDk8/4/

这很容易理解。如果您还没有,JSON 是 JavaScript 的原生对象结构。因此,您可以使用 dot 表示法访问它。

Starter on JSON in JavaScript

顺便说一句,您的 json 有点不一致。第二个类别中数组“产品”的键 是“产品”而不是“产品”。所以我冒昧地纠正它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多