javascript - 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" }
解决方案
推荐阅读
- json - Oracle Rest 数据服务以 JSON 形式返回集合
- java - Android Studio:在ScrollView中调用多个Canvas依次显示1个绘图(canvas)
- python - 未定义全局变量 - Python 和 PyGame
- swift - SwiftUI 2:在新窗口中打开视图的方式
- r - R子集内部级别嵌套数据框
- angular - 角按钮出现两次为什么?
- ios - Swift UI - 图片库上的按钮返回
- javascript - 谷歌浏览器上的休息 API
- node.js - 添加规则时出现 Eslint 错误 @typescript-eslint/interface-name-prefix
- java - 如何创建文件名 + 查询参数请求