Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript关联数组不适用于Jquery对象_Javascript_Jquery - Fatal编程技术网

Javascript关联数组不适用于Jquery对象

Javascript关联数组不适用于Jquery对象,javascript,jquery,Javascript,Jquery,我正在尝试构建一个关联数组parentTds,但是它没有按照我想要的方式工作 var parentTds = {}; var index = 0; $.each(clone, function () { var $currentItem = $(selectedActivities[index]); var $currentItemTd = $currentItem.closest('td'); console.log($currentItemTd.get(0));

我正在尝试构建一个关联数组
parentTds
,但是它没有按照我想要的方式工作

var parentTds = {};
var index = 0;
$.each(clone, function () {
    var $currentItem = $(selectedActivities[index]);
    var $currentItemTd = $currentItem.closest('td');
    console.log($currentItemTd.get(0));
    var borderLeft = 0;
    console.log(parentTds[$currentItemTd.get(0)]);
    console.log(parentTds[$currentItemTd]);
    if (typeof parentTds[$currentItemTd.get(0)] === "undefined") {
        console.log('NOT OK');
        borderLeft++;
        parentTds[$currentItemTd.get(0)] = $currentItemTd;
    } else {
        console.log('OK');
    }
    index++;
});
由于某些原因,
parentTds[$currentItemTd.get(0)]
总是返回存储的第一个项目。我在循环第一次运行时得到了
不正常
,而我应该得到
不正常
几次。我怀疑问题出在parentTds[$currentItemTd.get(0)]本身


有什么想法吗?

Javascript不像PHP那样宽容。使用此选项时:

if (typeof parentTds[$currentItemTd.get(0)] === "undefined") {
$currentItemTd.get(0)
可能被计算为0,因此引用总是
parentTds[0]

在这些情况下,我使用分解代码块并执行如下操作:

var cig = $currentItemTd.get(0);
if (typeof parentTds[cig] === "undefined") {

提示:JavaScript对象(不是“关联数组”)属性名(键)总是字符串<代码>$currentItemTd.get(0)不是字符串,但当您尝试将其用作键时,它会被强制为字符串。请您解释一下“克隆”和“选定活动”包含的内容。此外,此函数的目的是什么,您是否试图将父“td”元素获取到DOM中的对象。一个对象只能有一个父对象,因此列表应该只包含一个项。除非你想把所有的“td”都放在一个表的一行中?这个问题需要对你想要达到的目标做进一步的澄清。@nnnnnn:你是对的。我最终使用了数组
[]
,而不是关联数组,并且工作方式与现在完全相同。