简体   繁体   English

如何在JSON中的对象内部加载数组的元素?

[英]How do I load the elements of an array inside of an object in JSON?

I asked a similar (but different) question before. 我之前问过类似(但不同)的问题

I've since changed how I'm approaching the problem. 此后,我改变了解决问题的方式。

This is my JSON file. 是我的JSON文件。

Here's my javascript so far: 到目前为止,这是我的JavaScript:

$(document).ready(function () {
    var loadArticle = function (articleID, articleH) {
        var article = $("#article").clone();
        var thelist = '<dl id="article' + articleID + 'sections"></dl>';

        $(article).attr("id", "article" + articleID);
        $(article).append(articleH);
        $(article).html(thelist);

        return article;
    }
    var loadSection = function (articleID, sectionID, sectionContent) {
        var section = "#article" + articleID + "sections";

        $(section).append("<dt>Section " + sectionID + "</dt>");
        $(section).append("<dd>" + sectionContent + "</dd>");

        return section;
    }
    var loadConstitution = function (d) {
        $.each(d, function (i) {
            var articleID = i + 1;
            var articleH = "<p class='lead' id='article" + articleID + "'><strong>Article " + articleID + "</strong></p>";
            $("#constitutionHolder").append(loadArticle(articleID, articleH));

            //$.each(d.sections, function(j){
            //  var sectionID = j + 1;
            //  var theSection = d.sections[j];
            //$("#article" + articleID + "sections").append(articleID, sectionID, mySection);
            //})
        })
    }
    $.getJSON('data/stuff.json', loadConstitution);
})

Basically, I go through each data point and make a new <div> with the id="article1" (or whatever the number of the article may be). 基本上,我遍历每个数据点并使用id="article1" (或任何文章编号)创建一个新的<div> Then, since each article object is filled with one array of "sections" , I need to access the data in this array and append it to the <dl></dl> I have. 然后,由于每个article对象都填充有一个"sections"数组,因此我需要访问该数组中的数据并将其附加到我拥有的<dl></dl>中。

In my HTML, this is what I'm .clone() ing: 在我的HTML中,这就是我的.clone()

<div id="constitutionHolder">
    <div class="alert text-black" id="article">
        <p class="lead" id="articleHeader"><strong>Article 1</strong>
        </p>
        <dl id="sectionList"> <dt id="article_section">Section 1</dt>
            <dd id="article_sectionINFO">this stuff belongs to section 1</dd>
        </dl>
    </div>
</div>

I have 2 problems so far: 到目前为止,我有2个问题:

  1. $(article).append(articleH); isn't working 没用

  2. I can't load the sections into sectionList . 我无法将这些部分加载到sectionList

How do I fix this? 我该如何解决?

There are lots of ways to approach this, but here's an example that you might be able to modify to suit your application: http://jsfiddle.net/DgU3w/ . 有很多方法可以解决此问题,但以下示例可以根据您的应用程序进行修改: http : //jsfiddle.net/DgU3w/ (Note that I stripped out some details to try to understand the problem) (请注意,我删除了一些详细信息以尝试理解问题)

A few things I noticed in your example: 我在您的示例中注意到了一些事情:

  • loadArticle() clobbers some work with this statement: $(article).html(thelist); loadArticle()使此语句变得有些有用: $(article).html(thelist);
  • loadArticle() clones a block of HTML, but then it appends new elements that match cloned ones. loadArticle()克隆HTML块,但随后会添加与克隆的元素匹配的新元素。
  • It doesn't hurt to wrap jQuery objects (eg $(jQueryObj) ), but it's not necessary either. 包装jQuery对象(例如$(jQueryObj) )并没有什么坏处,但是也没有必要。
  • Some IDs were initialized to unique values, but others weren't. 一些ID被初始化为唯一值,而其他则没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何创建一个函数来更改位于数组内的对象内的属性,在 JSON 中 - How do i create a function that will change the property inside an object that is located inside of an array, in a JSON 在 React 中获取请求:如何在对象数组中映射对象的 JSON 数组? - Fetch request in React: How do I Map through JSON array of object inside of array of objects? 如何从对象内部的对象获取/寻址元素? - How do I get/address elements from an object inside an object? 我想 JSON 解析对象内的对象数组 - 正文中的文章,我该怎么做? - I want to JSON Parse an array of objects inside an object - articles from the body, how can I do that? 如何在 JS 中从这个 json 访问 json object 元素? - How do I access json object elements from this json in JS? 如何在数组内部的数组中搜索 object? - How do I search the object inside the array inside the array? 如何从firebase中json内部的数组中获取元素? - How can i get the elements from an array inside of json in firebase? 如何设置 state 以切换 boolean 值,该值位于 JSON ZA8CFDE6331149EB2AC96B8 数组中? - How do I set state to toggle a boolean value that sits inside an array of JSON object? 如何在数组内部获取 JSON 的 JSON 文件的内容 - How do I get the contents of a JSON file with JSON inside of the array 如何在 Angular 中删除 JSON 对象内的对象? - How do I remove an object inside a JSON object in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM