简体   繁体   English

从此js代码制作后缀的简单方法是什么?

[英]what is the easy way to make suffix from this js code?

Note the code below shows the array in the console, not in the snippet output 请注意,下面的代码在控制台中而不是在代码段输出中显示了数组

 var nodes = ["maria", "mary", "marks", "michael"]; function insert_word(split_nodes) { var rest = []; for (var i = 0; i < split_nodes.length; i++) { //console.log(current); var word = split_nodes[i]; var letters = word.split(""); var current = rest; console.log(current); for (var j = 0; j < letters.length; j++) { var character = letters[j]; var position = current[character]; if (position == null) { current = current[character] = j == letters.length - 1 ? 0 : {}; } else { current = current[character]; } } } } insert_word(nodes); 

Above outputs this 以上输出此

M :{a : {r :{i :{a :0},
    k :0,   
    y :
    },
},
    i :{c :{h :{a :{e :{l :0}}}}}}}

but I want to output this : 但我想输出这个:

M :{ar:{ia:0,
    k :0,   
    y :0
    },
 ichael :0
}

can anyone help me to find out this output from my code? 谁能帮助我从我的代码中找到此输出? how could i make suffeix from this code? 我怎样才能从此代码后缀?

This solution take a sightly changed object structure for the end indicator with a property isWord , because the original structure does not reflect entries like 'marc' and 'marcus' , because if only 'marc' is uses, a zero at the end of the tree denotes the end of the word, but it does not allowes to add a substring, because the property is a primitive and not an object. 该解决方案为带有属性isWord的结束指示符采用了isWord改变的对象结构,因为原始结构不反映诸如'marc''marcus''marc'条目,因为如果仅使用'marc' ,则末尾为零。 tree表示单词的结尾,但不允许添加子字符串,因为该属性是原始类型,而不是对象。

Basically this solution creates first a comlete tree with single letters and then joins all properties which have only one children object. 基本上,此解决方案首先创建一个具有单个字母的comlete树,然后将仅具有一个子对象的所有属性连接起来。

 function join(tree) { Object.keys(tree).forEach(key => { var object = tree[key], subKeys = Object.keys(object), joinedKey = key, found = false; if (key === 'isWord') { return; } while (subKeys.length === 1 && subKeys[0] !== 'isWord') { joinedKey += subKeys[0]; object = object[subKeys[0]]; subKeys = Object.keys(object); found = true; } if (found) { delete tree[key]; tree[joinedKey] = object; } join(tree[joinedKey]); }); } var node = ["maria", "mary", "marks", "michael"], tree = {}; node.forEach(string => [...string].reduce((t, c) => t[c] = t[c] || {}, tree).isWord = true); console.log(tree); join(tree); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 


A recursive single pass approach with a function for inserting a word into a tree which updates the nodes. 一种具有将单词插入树中以更新节点的功能的递归单遍方法。

It works by 它的工作原理

  • Checking the given string with all keys of the object and if string start with the actual key, then a recursive call with the part string and the nested part of the trie is made. 用对象的所有键检查给定的string ,如果string以实际键开头,则使用部分字符串和trie的嵌套部分进行递归调用。

  • Otherwise, it checks how many characters are the same from the key and the string. 否则,它将检查键和字符串中有多少个相同的字符。

    Then it checks the counter and creates a new node with the common part and two nodes, the old node content and a new node for the string. 然后,它检查计数器,并创建一个具有公共部分和两个节点的新节点,即旧节点内容和该字符串的新节点。

    Because of the new node, the old node is not more necessary and gets deleted, as well as the iteration stops by returning true for the update check. 由于有了新节点,因此不再需要旧节点并将其删除,并且通过为update检查返回true来停止迭代。

  • If no update took place, a new property with string as key and zero as value is assigned. 如果未进行更新,则分配一个新属性,该属性以字符串为键,值为零。

 function insertWord(tree, string) { var keys = Object.keys(tree), updated = keys.some(function (k) { var i = 0; if (string.startsWith(k)) { insertWord(tree[k], string.slice(k.length)); return true; } while (k[i] === string[i] && i < k.length) { i++; } if (i) { tree[k.slice(0, i)] = { [k.slice(i)]: tree[k], [string.slice(i)]: 0 }; delete tree[k]; return true; } }); if (!updated) { tree[string] = 0; } } var words = ["maria", "mary", "marks", "michael"], tree = {}; words.forEach(insertWord.bind(null, tree)); console.log(tree); insertWord(tree, 'mara'); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

相关问题 js,将json对象设为字符串-简单方法? - js, make a json object a string - the easy way? 有没有一种简单的方法可以使重新加载事件中的变量不重置? discord.js - Is there an easy way to make a variable in a reloading event not reset? discord.js 从JS代码中包含JS文件的最佳方法是什么? - What is the best way of including a JS file from JS code? 在数组Angular JS中转换对象的简单方法是什么? - What is easy way to convert object in array Angular JS? 有没有一种简单的方法可以找出操作DOM元素的js代码? - Is there an easy way to find out by which js code a DOM element is manipulated? 在代码中调试或查找错误的简单方法是什么? - What is the easy way to debug or find some typo in your code? 画这条线的简单方法是什么? - What is easy way to draw this lines? 在“ <input> 无法访问。 是否有一种简单的方法使其可访问(JS),还是我必须放弃它? - Using “required” on an <input> isn't accessible. Is there an easy way to make it accessible (JS) or do I have to abandon it? 在 JS 中将提示转换为整数的最佳方法是什么? - What is the best way to make a prompt into an integer in JS? 有没有简单的方法可以将MooTools代码转换为JQuery代码? - Is there an easy way to convert MooTools code into JQuery code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM