提问者:小点点

如何使用。each构建数组


所以我们在这个系统中工作,建立我们自己的页面。 我们构建了一个表单来使用。xwd文件插入时间线数据。 我们使用javascript来检索数据,并将其填充到一个变量中存储。 主页(title:)只有单个值,但实际事件应该在数组中。

我想用V来填充数组。

$(x_currentPageXML).children().each(function(index, elem){

});

现在,我要使用上面显示的foreach填充“events”数组。 将.each放入var中是行不通的,我也不知道该怎么做。

    var SictTimeline = new function() {     
            this.loadJS = function () {
        $.getScript(x_templateLocation + 'common_html5/js/timeline.js')
            .done(function (script, textStatus) {
                var make_the_json = $(x_currentPageXML).children().map(function (element) {
                    return {
                        title: {
                            media: {
                                url: element.getAttribute("url"),
                                caption: element.getAttribute("tip"),
                            },
                            text: {
                                headline: element.getAttribute("name"),
                                text: '<p>' + element.getAttribute("text") + '</p>'
                            }
                        },
                        events: {
                            media: {
                                url: element.getAttribute("url"),
                                caption: element.getAttribute("tip"),
                            },
                            start_date: {
                                month: '8',
                                day: '9',
                                year: '1963'
                            },
                            text: {
                                headline: element.getAttribute("name"),
                                text: element.getAttribute("text")
                            }
                        }
                    }
                })

                var timeline_json = make_the_json; // replace make_the_json() with the JSON object you created
                // two arguments: the id of the Timeline container (no '#')
                // and the JSON object or an instance of TL.TimelineConfig created from
                // a suitable JSON object
                window.timeline = new TL.Timeline('timeline-embed', timeline_json);
            })
            .fail(function (jqxhr, settings, exception) {
                console.log('Failed to load the script for the timeline');
            });
    }
    // function called every time the page is viewed after it has initially loaded
    this.pageChanged = function() {

    }

    // function called every time the size of the LO is changed
    this.sizeChanged = function() {

    }

    this.init = function() {
        this.loadJS();
        // call this function in every model once everything's loaded
        x_pageLoaded();
    }
}

一个XML文件示例,其中包含

<?xml version="1.0"?>
<learningObject editorVersion="3" targetFolder="Nottingham" name="Learning Object Title" language="en-GB" navigation="Linear" textSize="12" theme="default" displayMode="fill window" responsive="true">

<SictTimeline linkID="PG1592486441661" name="My page" media="SictTimeline" text="&lt;p&gt;Text for my page&lt;/p&gt;&#10;" url="FileLocation + 'media/https___images.genius.com_53c4575fa3f97a8d4b18d69a600afaf0.900x900x1.jpg'" tip="Description for Image 1"></SictTimeline></learningObject>

共2个答案

匿名用户

我不确定我完全理解这个问题,但是你想从循环中的每个元素中提取东西,对吗? 将X_CurrentPageXML替换为循环内的Elem


var result = []

$(x_currentPageXML).children().each(function (index, elem) {
    var make_the_json = {
        title: {
            media: {
                url: elem.getAttribute("url"),
                caption: elem.getAttribute("tip"),
            },
            text: {
                headline: elem.getAttribute("name"),
                text: '<p>' + elem.getAttribute("text") + '</p>'
            }
        },
        events: [
            {
                media: {
                    url: elem.getAttribute("url"),
                    caption: elem.getAttribute("tip"),
                },
                start_date: {
                    month: '8',
                    day: '9',
                    year: '1963'
                },
                text: {
                    headline: elem.getAttribute("name"),
                    text: elem.getAttribute("text")
                }
            }
        ]
    };

    result.push(make_the_json)
})

匿名用户

我猜您试图实现的是基于$(X_CurrentPageML)内部元素的数量(和属性)生成一个对象数组。 为此,需要使用.map()方法:

    events: $(x_currentPageXML).children().map(function (index, element) {
        return {
            media: {
                url: element.getAttribute("url"),
                caption: element.getAttribute("tip"),
            },
            start_date: {
                month: '8',
                day: '9',
                year: '1963'
            },
            text: {
                headline: element.getAttribute("name"),
                text: element.getAttribute("text")
            }
        }
    }).get()