gpt4 book ai didi

javascript - 使对象对象作为数组的索引(Javascript)

转载 作者:行者123 更新时间:2023-12-03 04:48:34 27 4
gpt4 key购买 nike

我一直在使用此存储库中的填字游戏应用程序:https://github.com/jweisbeck/Crossword 。问题是该程序使用jquery版本1.6.2,而我的整个项目使用jquery-3.1.1版本。特别是,错误出现在这里:

buildEntries: function() {
var puzzCells = $('#puzzle td'),
light,
$groupedLights,
hasOffset = false,
positionOffset = entryCount - puzz.data[puzz.data.length-1].position; // diff. between total ENTRIES and highest POSITIONS

for (var x=1, p = entryCount; x <= p; ++x) {
var letters = puzz.data[x-1].answer.split('');

for (var i=0; i < entries[x-1].length; ++i) {

light = $(puzzCells +'[data-coords="' + entries[x-1][i] + '"]');

if($(light).empty()){
console.log($(light));
$(light)
.addClass('entry-' + (x-1) + ' position-' + (x-1) )
.append('<input maxlength="1" val="" type="text" tabindex="-1" />');
}
}
}

// Put entry number in first 'light' of each entry, skipping it if already present
console.log(entries);
console.log(puzz.data);
for (var i = 0; i < entryCount; i++) {
$groupedLights = $('.entry-' + i); 
if(!$('.entry-' + i +':eq(0) span').length){
$groupedLights.eq(0)
.append('<span>' + puzz.data[i].position + '</span>');
}
}

util.highlightEntry();
util.highlightClue();
$('.active').eq(0).focus();
$('.active').eq(0).select();
}

错误发生在

light = $(puzzCells +'[data-coords="' + entries[x-1][i] + '"]');

浏览器显示此错误:

Error: Syntax error, unrecognized expression [object Object][data-coords="1,6"]

我相信这与 jQuery 版本有关。或者程序可能使用 [object Object] 作为索引。不确定,因为我是 jQuery 新手。我尝试使用 jQuery Migrate,但没有帮助。另外,我尝试使用 jQuery 1.6.2,但是 Web 浏览器根本找不到 jQuery,因为我使用的是 Typescript,并且必须通过 .d.ts 文件安装 jQuery。有什么提示或建议吗?提前致谢

最佳答案

正如标题所说:

Make object Object as index of array

这对于标准对象/数组来说是不可能的,但您可以使用 Map 来实现:

let map = new Map(),
key = {id: '##'};

map.set(key, [1,2,3,4,5]);

console.log(map.get(key)); //[1,2,3,4,5]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

关于javascript - 使对象对象作为数组的索引(Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42760451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com