简体   繁体   English

动态地将属性分配给JavaScript对象(trie)

[英]Dynamically assigning properties to a JavaScript object (trie)

I'm trying to implement a variation of a trie in JavaScript. 我正在尝试在JavaScript中实现特里的变体。 Basically, it's an efficient data storage object in which the characters in keys are not repeated. 基本上,这是一个有效的数据存储对象,其中键中的字符不重复。 In other words, if I have the keys "abe" and "ann," only one instance of the shared letter "a" should appear: 换句话说,如果我具有键“ abe”和“ ann”,则应该只显示一个共享字母“ a”的实例:

{
    a: {
        b: {
            e: {
                0: 'lincoln'
            }
        },
        n: {
            n: {
                0: 'mcgee'
            }
        }
    }
}

Here is the desired implementation and a few usage examples: 这是所需的实现和一些用法示例:

function Trie () {
    // The top level of the trie. 
    var root = {}; 

    return {
        write: function (key, value) {
        },
        read: function (key) {
        }
    };
}

// Sample usage 
var trie = new Trie(); 

trie.write('abe', 'lincoln'); 
trie.write('ann', 'mcgee'); 

trie.read('abe'); // returns 'lincoln'
trie.read('ann'); // returns 'mcgee' 

I've run into a blocker with respect to the write method. 关于write方法,我遇到了一个障碍。 Given a string key such as "abe," I need to assign a property to root['a']['b']['e'] . 给定诸如“ abe”之类的字符串键,我需要为root['a']['b']['e']分配一个属性。 I can't find a way to assign a value to an object property several layers deep when the number of keys and the values of the keys are unknown. 数量和键的值未知时,我找不到一种将值分配给几层深的对象属性的方法

The only solution that comes to mind is, I think, a bad one: placing the path to the value into a string and using eval . 我认为,想到的唯一解决方案是一个不好的解决方案:将值的路径放入字符串中并使用eval For example: eval("root['a']['b']['e'] = 'lincoln'"); 例如: eval("root['a']['b']['e'] = 'lincoln'");

Is there a better solution for dynamically assigning the values? 动态分配值是否有更好的解决方案? (I realize that this is a bit of complicated problem, so I'm happy to clarify by providing extra information.) (我意识到这是一个有点复杂的问题,因此我很乐意通过提供其他信息来进行说明。)

a very naive approach (given the requirements,though i would write a different implementation) 一个非常幼稚的方法(考虑到需求,尽管我会写一个不同的实现)

given a string of keys and a pointer to the root,and a value to assign; 给定一串键和指向根的指针,以及要分配的值;

function write(root,path,value){
    var a = path.split(''); // 'abc'->['a','b','c']
    var pointer = root;
    var i=0;
    while(i<a.length-1){
        if(pointer[a[i]] == undefined){
           pointer[a[i]]={};
        }
        pointer = pointer[a[i]];
        i++;
    }
    pointer[a[i]]=value;
    return root;
}

EDIT : i'm assuming all the keys exist on their respective object. 编辑:我假设所有键都存在于它们各自的对象上。 I added a if condition in case some keys are not defined. 我添加了if条件,以防某些键未定义。

EDIT:2 split corrected, correcting a little bug right now ;) 编辑:2分裂纠正,现在纠正一个小错误;)

EDIT:3 should work now. 编辑:3应该现在工作。

usage : write({},'abc',1) // yields {a:{b:{c:1}}}

what you're looking for is a double array trie. 您正在寻找的是双数组特里。 you can do a github search for that, but the two main libraries listed are: 您可以对此进行github搜索,但是列出的两个主要库是:

var doublearray = require('./doublearray.js'); var doublearray = require('./ doublearray.js');

var words = [ var words = [

{ k: 'a', v: 1 }, {k:'a',v:1},

{ k: 'abc', v: 2 }, {k:'abc',v:2},

]; ]。

var trie = doublearray.builder().build(words); var trie = doublearray.builder()。build(words);

trie.contain('a'); trie.contain( 'A'); // -> true //-> true

trie.lookup('abc'); trie.lookup( 'ABC'); // -> 2 //-> 2

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

相关问题 分配Javascript对象属性 - Assigning Javascript Object Properties 扩展Javascript对象而不向原型分配属性? - Extending a Javascript object without assigning properties to the prototype? 用javascript动态创建具有属性的对象 - dynamically creating object with properties in javascript 使用for循环在javascript中动态创建按钮,并分配属性。 - Dynamically creating buttons in javascript using for loop, and assigning properties. 根据其他对象数组的值动态分配 object 属性 - Dynamically assigning object properties based on the value of an array of other objects 将字符串数组的动态值分配给 typescript 中 object 的属性 - Assigning dynamically values of string array to properties of an object in typescript Javascript:遍历对象的属性并将其分配给新对象 - Javascript: Iterating through properties of object and assigning them to a new object 在JavaScript / TypeScript中将对象属性动态地“直接”添加到“ this” - Adding object properties dynamically “directly” to `this` in JavaScript/TypeScript 如何将属性和值动态分配给 Javascript 中的 object - How to dynamically assign properties and values to an object in Javascript JavaScript动态创建具有多个属性的对象 - JavaScript Dynamically Create Object with Multiple Properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM