javascript - 如何定位 JSON 文件中的特定对象

标签 javascript jquery arrays ajax json

下面是我的 JSON 文件。数组中的每个对象都属于一个 Id。所以第一个对象是#one,第二个对象是#two。等等。我不知道如何用 JavaScript 编写这个。例如,如何定位带有 object[1] 的数组 number[1] 附加到 #two?另外我需要删除对象的第一行,即计数行?谢谢!

所以要了解更多细节。我接到了一项任务,我将图片放在 html 中并放在图片下方,json 文件中的每个对象(图片的描述)都应该放在它的下方。我是新的 json 和 ajax :( 所以 ids #one、#two... 只是示例。将有 6 个 id。每个对象一个。

[
  [
    {
    "count": "80",
    "countTxt":"Buyer logins",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"523"
    },
    {
    "count": "233",
    "countTxt":"Searches",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"628"
    },
    {
    "count": "533",
    "countTxt":"Views of our member pages",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"2,365"
    }
  ],
  [
    {
    "count": "80",
    "countTxt":"Total vists",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"412"
    },
    {
    "count": "53",
    "countTxt":"Unique visitors",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"731"
    },
    {
    "count": "1:12 min",
    "countTxt":"Total time spent on page",
    "participantsTxt":"Total for all participating member companies:",
    "participantCount":"784.2 mins"
    }
  ]
]

最佳答案

您有一个数组,其中包含包含对象的数组。它们似乎在任何地方都没有这些 ID,并且名称如 #one#two 等,分配这些 ID 很尴尬(您基本上必须有一个等待分配的名称列表)。

一旦你有了这个列表,工作本身就相对简单:

  1. 通过 JSON.parse 将 JSON 解析为对象图。

    var outerArray = JSON.parse(jsonText);
    
  2. 创建一个空白对象,您可以将命名对象保存为属性,例如 map :

    var map = {};
    
  3. 以数组形式输入 ID 列表:

    // This assumes no more than 99 elements
    var ids = [
        "#one", "#two", "#three", /*...and so on...*/, "#ninety-nine"
    ];
    
  4. 有一个以 0 开头的索引变量。

  5. 循环最外层数组;你有lots and lots of options for how to do that ,我可能会使用 forEach:

    outerArray.forEach(function(nestedArray) {
       // ...more code here
    });
    
  6. 在该循环内,循环遍历嵌套数组中的对象:

    outerArray.forEach(function(nestedArray) {
       nestedArray.forEach(function(object) {
           // ...more code here
       });
    });
    
  7. 在内循环中,通过索引变量使用下一个可用 ID 将当前对象添加到 map 中:

    map[ids[index++]] = object;
    
  8. 如果您确实愿意,请从对象中删除 count 属性:

    delete object.count;
    

所以把它们放在一起:

var index = 0;
var outerArray = JSON.parse(jsonText);
var map = {};
var ids = [    // This assumes no more than 99 elements
    "#one", "#two", "#three", /*...and so on...*/, "#ninety-nine"
];
outerArray.forEach(function(nestedArray) {
   nestedArray.forEach(function(object) {
        map[ids[index++]] = object;
        delete object.count; // Doesn't matter whether this is before or after the line above
   });
});

注释:

  • 使用诸如 #one 之类的 ID 来访问这些对象会非常尴尬,但我认为您这样做是有原因的。您可以执行 map["#one"] 等操作来获取对象。

  • 如果您没有删除 count 属性,请不要打扰。

关于javascript - 如何定位 JSON 文件中的特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572094/

相关文章:

javascript - 如何在不关注任何文本框的情况下使用键盘调用函数?

jquery - Bootstrap 3 数据切换 Accordion

javascript - 添加条件时Jquery不执行动画

c - 当放入c中的数组时,字符串会变形

javascript - $.ajax 调用有效,$http.get() 无效(404,未找到)

javascript - 如何在 map 迭代中获取特定对象键

c - 在 C 中对多维数组列进行排序

java - arrayList 存储的不确定性

javascript - 嵌套 javascript 哈希的默认值

javascript - 右键单击元素显示div