循环浏览对象中的段落。怎么样?

时间:2018-08-18 04:55:50

标签: javascript jquery html object

以下javascript对象包含2个段落。我很难弄清楚如何动态输出它们。也许是循环?但是这些段落是具有自己的子属性的子对象。我需要连续访问它们,并将它们附加到modal-section-body容器中。

请随时建议重新设计对象。

            // Sample Modal Object
            var modalDefaults = {

              // depth 0
              modalId: 'modal6',
              
              // depth 0
              modalHeader: {
                icon: 'headerIcon',
                iconColor: 'headerIconColor',
                mainTitle: 'headerTitle',
                subTitle: 'headerSubTitle'
              },

              // depth 0  
              modalBody: {
                title: 'bodyTitle',
                abrv: 'bodyAbrv',
                subTitle: 'bodySubTitle',

                //depth 1
                paragraph1: {
                  //depth 2
                  title: 'Paragraph 1 Title',
                  body: 'Lorem ipsum dolor sit amet 1.'
                },

                //depth 1
                paragraph2: {
                  //depth 2
                  title: 'Paragraph 2 Title',
                  body: 'Lorem ipsum dolor sit amet 2.'
                }

              },

              // depth 0
              modalFooter: {

                  //depth 1
                linkOk: {
                  //depth 2
                  color: 'linkOkColor',
                  icon: 'linkOkIcon',
                  label: 'linkOkLabel',
                  href: './'
                },

                //depth 1
                linkCancel: {
                  //depth 2
                  color: 'linkCancelColor',
                  icon: 'linkCancelIcon',
                  label: 'linkCancelLabel',
                  href: './'
                }
              }
            };




            console.log(modalDefaults.modalId);

            console.log(modalDefaults.modalHeader.icon);
            console.log(modalDefaults.modalHeader.iconColor);
            console.log(modalDefaults.modalHeader.mainTitle);
            console.log(modalDefaults.modalHeader.subTitle);

            console.log(modalDefaults.modalBody.title);
            console.log(modalDefaults.modalBody.abrv);
            console.log(modalDefaults.modalBody.subTitle);

            console.log(modalDefaults.modalBody.paragraph1.title);
            console.log(modalDefaults.modalBody.paragraph1.body);

            console.log(modalDefaults.modalBody.paragraph2.title);
            console.log(modalDefaults.modalBody.paragraph2.body);

            console.log(modalDefaults.modalFooter.linkOk.color);
            console.log(modalDefaults.modalFooter.linkOk.icon);
            console.log(modalDefaults.modalFooter.linkOk.label);

            console.log(modalDefaults.modalFooter.linkCancel.color);
            console.log(modalDefaults.modalFooter.linkCancel.icon);
            console.log(modalDefaults.modalFooter.linkCancel.label);

1 个答案:

答案 0 :(得分:2)

您可以按照以下方式进行操作:

template <size_t N>
A::A(std::array<std::string, N>& data)
{
    //static_assert(N <= 10); // if you don't want to discard surplus data silently...
    //std::copy(data.begin(), data.end(), this->data.begin());

    std::copy
    (
        data.data(),
        data.data() + std::min(N, this->data.size()),
        this->data.begin()
    );
}

Online demo (jsFiddle)