Array Slice Returning [object Object] instead of value

放肆的年华 提交于 2019-12-23 21:36:25

问题


I am trying to get the position of a specific div when it is dropped. With some help i have put together the code bellow. I added in the last bit to try and get the specific values but it simply returns [object Object] instead of something like [0,0] or [0,120].

So the question is how do i get the actual values from the array?

Here is a jsFiddle

$(function() {
    $('.AppList').droppable({
        accept: ".App",
        tolerance: 'fit',
        drop: function(event, ui) {
            var apps = $(".App"),
            positions = [];

            $.each(apps, function (index, app) {
                var positionInfo = $(app).position();

                positions.push(positionInfo);
            });
            var Time = positions.slice(0,1);
            var x=document.getElementById("posThis");
            x.innerHTML=Time;
            console.log(positions);
        }
    }); 
});

回答1:


The problem here is that positionInfo is an object, not an array. Time is an array with one such object inside.

I believe you want something like this:

var Time = positions[0];
var x=document.getElementById("posThis");
x.innerHTML= '[' + Time.left + ',' + Time.top + ']';


来源:https://stackoverflow.com/questions/16245137/array-slice-returning-object-object-instead-of-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!