首页 > 解决方案 > Javascript重复对象填充数组

问题描述

尝试将每个新创建的对象推送到一个数组中,以便我能够在其他地方使用这些值,但是,在循环运行后,整个数组被创建的第一个对象填充,并坚持想法,我尝试了映射, for 循环和类似的,但问题仍然存在。

虽然创建的对象都是不同的,但当循环完成时却没有。

const callEvents = getAllCallEvents(skypeConversation)
            let callInformationArray = []
            callEvents.forEach(call => {
                let callObject = createCallInformationObject(call, callInfo)
                if (callObject) {
                    // /console.log(callObject)
                    callInformationArray.push(callObject)

                }
            })

正在创建的对象

let callInfo = {
    whoStartedCall: false,
    participentOne: false,
    participentTwo: false,
    duration: false,
    timeStarted: false,
    timeEnded: false,
    callId: false,
    callDate: false,
    callType: false,
}

我能想到的最接近的可能是为循环的每个部分创建的对象,在创建时,第一个没有被破坏,尽管我认为这可能是因为 callObject 在控制台中登录时是不同的时间。看了一下这个页面并尝试了这里的东西,但同样的问题仍然存在

如果需要,这是整个文件(在重构之前先让这个工作)

/**
 * Declaring Objects
 */
let callInfo = {
    whoStartedCall: false,
    participentOne: false,
    participentTwo: false,
    duration: false,
    timeStarted: false,
    timeEnded: false,
    callId: false,
    callDate: false,
    callType: false,
}
//DURATION IS IN SECONDS

window.addEventListener('load', function () {
    document.getElementById('tar-upload-input').addEventListener('change',function (e) {

        let reader = new FileReader();
        reader.onload = function(event) {
            let skypeJsonObject = JSON.parse(event.target.result),
                skypeConversation = skypeJsonObject.conversations

            // Get all the events that are calls, discard the rest
            // And then create the information from this object
            console.time('test')
            const callEvents = getAllCallEvents(skypeConversation)
            let callInformationArray = []
            callEvents.forEach(call => {
                let callObject = createCallInformationObject(call, callInfo)
                if (callObject) {
                    // /console.log(callObject)
                    callInformationArray.push(callObject)
                }
            })
            console.log(callInformationArray)
            console.timeEnd('test')
        }
        reader.readAsText(e.target.files[0]);
    })
})

function addToArray(array, object) {
    array.push(object)
    console.log(array)
    return array
}

/**
 * Gets all the calls from every conversation
 * @param {Array} conversations
 */
function getAllCallEvents(conversations) {
    let callEvents = []
    conversations.forEach(conversation => {
        conversation.MessageList.forEach(message => {
            if (message.messagetype === 'Event/Call') {
                callEvents.push(message)
            }
        })
    })

    return callEvents
}

/**
 * Creates the call information object from the Skype Import data
 * @param {Object} call
 * @param {Object} CallInformationObject
 * @returns CallInformationObject with filled data
 */
function createCallInformationObject(call, CallInformationObject) {
    let parser = new DOMParser(),
        callInformation = parser.parseFromString(call.content, 'text/xml'),
        callParticipents = callInformation.getElementsByTagName('name'),
        callIdentifier = callInformation.getElementsByTagName('partlist')[0],
        callType = callIdentifier.attributes['type'],
        callDate = new Date(call.originalarrivaltime),
        callEventId = callIdentifier.attributes['callId'],
        callDurationArray = callInformation.getElementsByTagName('duration'),
        callDuration = null

    if (!callType) {
        return
    }
    if (!callEventId) {
        return
    }
    if (callDurationArray.length !== 0) {
        callDuration = callDurationArray[0].innerHTML
    }

    CallInformationObject.callType = callType.value
    CallInformationObject.callId = callEventId.value
    CallInformationObject.whoStartedCall = call.displayName
    CallInformationObject.callDate = callDate
    CallInformationObject.callType = callType.value
    CallInformationObject.duration = callDuration

    callParticipents = getCallParticipents(callParticipents)
    CallInformationObject.participentOne = callParticipents[0]
    CallInformationObject.participentTwo = callParticipents[1]

    if (!CallInformationObject.whoStartedCall) {
        CallInformationObject.whoStartedCall = CallInformationObject.participentOne
    }

    return CallInformationObject
}

function getCallParticipents(callGuests) {
    let callParticipents = [
        callGuests[0].innerHTML,
        callGuests[1].innerHTML,
    ]

    return callParticipents
}

编辑:这是正在创建的对象的格式

 { whoStartedCall: "Samuel Durrant-Walker", participentOne: "Samuel Durrant-Walker", participentTwo: "Yiannis Papadopoulos", duration: null, timeStarted: false, timeEnded: false, callId: "a15882cb-3a28-4d6d-a301-f8e8acb014a3", callDate: Date Wed Apr 22 2020 13:16:54 GMT+0100 (British Summer Time), callType: "started" }

标签: javascriptarraysloopsobject

解决方案


推荐阅读