如何使用循环在javascript数组对象中用单个键插入多个值。我正在创建事件日历以将事件添加到日历。我的代码是
var aColor:uint;
var aRadius:int;
var aShape:Shape;
// Subscribe for MOUSE_DOWN event.
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
function onDown(e:MouseEvent):void
{
// Set up the new color.
changeColor();
// Set up the new circle and assign its reference to the variable.
// The previous shape, if any, doesn't get destroyed,
// just no longer accessible via the same variable.
aShape = new Shape;
aShape.x = stage.stageWidth / 2;
aShape.y = stage.stageHeight / 2;
aShape.graphics.lineStyle(0, 0x000000);
// Put it UNDER all the other circles.
addChildAt(aShape, 0);
// Turn ENTER_FRAME handler on.
addEventListener(Event.ENTER_FRAME, onFrame);
// Prepare to intercept the MOUSE_UP event.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onFrame(e:Event):void
{
// Increase the radius.
aRadius++;
// Draw the circle inside the last added shape.
aShape.graphics.clear();
aShape.graphics.beginFill(aColor);
aShape.graphics.drawCircle(0, 0, aRadius);
aShape.graphics.endFill();
}
function onUp(e:MouseEvent):void
{
// Turn ENTER_FRAME handler OFF.
removeEventListener(Event.ENTER_FRAME, onFrame);
// Stop intercepting the MOUSE_UP event.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}
function changeColor():void
{
aColor = 0x1000000 * Math.random();
}
使用此代码,我得到了类似的东西,
<script type="text/javascript">
var a=<?php echo(json_encode($da)); ?>;
var ev= <?php echo(json_encode($eve)); ?>;
var codropsEvents ={};
for(var i=0;i<a.length;i++)
{
codropsEvents[a[i]] = '<span>'+[ev[i]]+'</span>';
}
</script>
但是我的日历插件不支持在同一天的多个事件。它仅支持以下内容
var codropsEvents={[date1]:[event1],[date1]:[event2]}
请任何人都可以帮助我 注意:$ da是日期数组,$ ev是每个日期中的事件
答案 0 :(得分:0)
您可以在数组上使用.push
方法。 https://www.w3schools.com/jsref/jsref_push.asp
例如:
如果您有类似您的对象:
var codropsEvents = {[date1]: [], [date2]: []}
您想在这些日期中添加事件
codropsEvents[date1].push("event1")
这会将event1
推到date1
,因此请在循环中执行以下操作:
for (var i=0; i<a.length; i++){
codropsEvents[a[i]].push(ev[i]);
}