简体   繁体   English

将成对的对象链接在一起,而无需在javascript中触摸它们

[英]Link pairs of objects together without touching them in javascript

I have simple problem and Im looking for elegant solution. 我有一个简单的问题,我正在寻找优雅的解决方案。

There is list in my html. 我的html中有列表。 I used jquery to map its items into array. 我使用jquery将其项目映射到数组中。 So I have array of objects. 所以我有对象数组。 Then I made (again with jquery) buttons for each item one. 然后,我为每一项设置了(再次使用jquery)按钮。 So I have pairs, item and button is in each pair. 所以我有一对,每对都有项目和按钮。

Now lets assume, I want to make structure, that will link somehow these pairs together. 现在假设,我想做一个结构,将这些对以某种方式链接在一起。 So if I use jquery to select any element, then I will be able to find element associated with it. 因此,如果我使用jquery选择任何元素,那么我将能够找到与其关联的元素。 I still have some ideas, but they are looking too complicated. 我仍然有一些想法,但是它们看起来太复杂了。 Im looking for design pattern or something very close to it. 我正在寻找设计模式或与其非常接近的东西。

HTML structure: HTML结构:

<ul>
    <li>item 1</li>
    <li>item 2</li>
    <li id="active">item 3</li>
</ul>

So far my idea that didn't work in javascript/jquery (it is very simplified): 到目前为止,我的想法在javascript / jquery中不起作用(它非常简化):

var list = [];

$('li').each(function() {
    list.push($(this));
    });

// this should be object, that links list items with buttons
var linker = {};

for (var item in list) {
    var button = $('<button>Do something!</button>');

    // this part is crazy O.o
    linker[list[item]] = button;
    linker[button] = list[item];
    // it doesnt work, becouse toString() function make from any of these objects "[object Object]"
}


// Now code like this should be able to find button that belongs to item 3

var active = $('#active')[0];

var result = linker[active];

Please show me the most easy way, how to link two object together without touching its properties. 请告诉我最简单的方法,如何将两个对象链接在一起而又不影响其属性。 Thanks for any help. 谢谢你的帮助。

how to link two object together without touching its properties. 如何在不触摸其属性的情况下将两个对象链接在一起。

As you have seen, an object as a key-value-map doesn't work because its keys are stringified. 如您所见,作为键值映射的对象不起作用,因为它的键是字符串化的。 In future JavaScript, you will be able to use a Map (or even a WeakMap which fits better for you), but such is not possible yet. 在将来的JavaScript中,您将能够使用Map (甚至更适合您的WeakMap ),但这还不可能。 However, you can use a Map shim that works by storing key and value objects in arrays with correspondent indices. 但是,可以使用通过将键和值对象存储在具有相应索引的数组中而起作用的Map填充程序。 If you implement your own, you can also do a reverse-lookup on the same arrays. 如果自己实现,则还可以在相同的数组上进行反向查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:从 object 中提取键值对并将它们分组到一个对象数组中 - Javascript: Extracting key value pairs from an object and grouping them together in an array of objects 递归访问对象内对象的键/值对并将它们组合成一个平面数组 - Recursively accessing key/value pairs of object within objects & combining them together into a flat array 如何创建两个日历并使用JavaScript将它们链接在一起 - How to create Two calendars and link them together using JavaScript JavaScript 对象 - 我在访问嵌套多个级别的对象属性数组并将它们组合在一起时遇到问题 - JavaScript Objects - I'm having issues accessing an array of objects properties nested multiple levels & combining them together 将 javascript 对象 ANDing 在一起 - ANDing javascript objects together 如何有效地匹配两个不同的javascript对象上的id并将它们合并在一起? - How to efficiently match ids on two different javascript objects and merge them together? JavaScript对象:销毁它们 - JavaScript objects: Destroying them Javascript验证,与表单链接在一起? - Javascript validation, link together with the form? 在JavaScript中将相似的对象组合在一起 - Combining similar objects together in JavaScript 将两个javascript对象附加在一起 - append two javascript objects together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM