在json树中查找值并返回路径(Find value in json tree and return the path)
我有孩子的数据,我必须按名称找到元素并打印元素的路径。 我写了搜索功能,找到了元素,但无法处理PATH。 我应该在哪里处理它? 如何在搜索时添加或删除路径?
let data = { type: "folder", name: "animals", children: [ { type: "folder", name: "cat", children: [ { type: "folder", name: "images", children: [ { type: "file", name: "cat001.jpg" }, { type: "file", name: "cat002.jpg" } ] }, { type: "folder", name: "images", children: [ { type: "file", name: "cat001.jpg" }, { type: "file", name: "cat002.jpg" } ] } ] }, { type: "folder", name: "dog", children: [ { type: "folder", name: "images", children: [ { type: "file", name: "dog001.jpg" }, { type: "file", name: "dog002.jpg" } ] } ] }, { type: "file", name: "horse.png" }, { type: "file", name: "bear.png" }, { type: "file", name: "horse.png" } ]
};
search(data, name, path = "") { if (data.name == name) { let path = [data.name]; return path; } else if (data.children != null) { let result = null; for (let i = 0; result == null && i < data.children.length; i++) { result = this.search(data.children[i], name); } return result; } return null; }
I have data with children, i have to find element by name and print the path to element. I wrote search fucntion that finds the element, but can't handle with PATH. Where i should handle it? How to add or remove path while searching?
let data = { type: "folder", name: "animals", children: [ { type: "folder", name: "cat", children: [ { type: "folder", name: "images", children: [ { type: "file", name: "cat001.jpg" }, { type: "file", name: "cat002.jpg" } ] }, { type: "folder", name: "images", children: [ { type: "file", name: "cat001.jpg" }, { type: "file", name: "cat002.jpg" } ] } ] }, { type: "folder", name: "dog", children: [ { type: "folder", name: "images", children: [ { type: "file", name: "dog001.jpg" }, { type: "file", name: "dog002.jpg" } ] } ] }, { type: "file", name: "horse.png" }, { type: "file", name: "bear.png" }, { type: "file", name: "horse.png" } ]
};
search(data, name, path = "") { if (data.name == name) { let path = [data.name]; return path; } else if (data.children != null) { let result = null; for (let i = 0; result == null && i < data.children.length; i++) { result = this.search(data.children[i], name); } return result; } return null; }
原文:https://stackoverflow.com/questions/37460822
最满意答案
没有索引是疯狂的。
这将有所帮助,但索引键长度只能是900字节。
但是,很可能您已经有重复项目,因为第二个EXISTS可能会在第一个EXISTS之后但第一个INSERT之前运行。
索引创建会告诉你,并随后防止这种情况发生。
但是,您可以在重负载下获得错误。
我喜欢的高插入/低重复的方法是JFDI模式。 高度并发
BEGIN TRY INSERT etc END TRY BEGIN CATCH IF ERROR_NUMBER() <> 2627 RAISERROR etc END CATCH
It's madness not to have an index.
It would help but the index key length can only be 900 bytes.
However, it's likely you already have duplicates because the potential for a 2nd EXISTS to run after the 1st EXISTS but before the 1st INSERT.
The index creation will tell you, and subsequently protect against this.
However, you can get errors under heavy load.
My favoured approach for high inserts/low duplicates is the JFDI pattern. Highly concurrent
BEGIN TRY INSERT etc END TRY BEGIN CATCH IF ERROR_NUMBER() <> 2627 RAISERROR etc END CATCH
相关问答
更多-
SQL Server TIMESTAMP数据类型不是类似日期的数据类型。 它是服务器生成的值,用于帮助实现数据一致性。 如果要存储日期时间数据,请避免使用TIMESTAMP数据类型。 因此,请勿转换为JDBC Timestamp对象。 数据可以作为字符串值插入。 请注意,您指定的格式是可以的,但是对于SQL Server DATETIME数据类型, fff...必须是3位数,不超过3位数。 A SQL Server TIMESTAMP data type is not a date-like data ty ...
-
唯一的约束是在幕后实现的唯一索引,因此您如何指定它并不重要。 我倾向于简单地实现: ALTER TABLE dbo.foo ADD CONSTRAINT UQ_bar UNIQUE(bar); 有些人创建一个唯一的索引,例如 CREATE UNIQUE INDEX IX_UQ_Bar ON dbo.foo(bar); 区别在于意图 - 如果要创建强制执行唯一性/业务规则的约束,则创建一个约束,如果您这样做以帮助查询性能,创建唯一索引可能更合乎逻辑。 再次,在封面下,这是相同的实现,但你走到那里的路可能有 ...
-
如何在我的列(SQL Server 2008 R2)上创建唯一的约束?(How can I create a unique constraint on my column (SQL Server 2008 R2)?)[2023-05-01]
要通过GUI创建这些约束,您需要“索引和密钥”对话框,而不是检查约束条件。 但是在你的情况下,你只需要运行你已经拥有的代码。 完全不需要输入表达式对话框。 To create these constraints through the GUI you need the "indexes and keys" dialogue not the check constraints one. But in your case you just need to run the piece of code you al ... -
如何使用SQL Server 2008打开和关闭IDENTITY_INSERT?(How to turn IDENTITY_INSERT on and off using SQL Server 2008?)[2022-06-23]
根据MSDN通过SQL SET IDENTITY_INSERT sometableWithIdentity ON INSERT sometableWithIdentity (IdentityColumn, col2, col3, ...) VALUES (AnIdentityValue, col2value, col3value, ...) SET IDENTITY_INSERT sometableWithIdentity OFF 完整的错误消息告诉你什么是错误的... 当IDENTITY_INSER ... -
UNIQUE CONSTRAINT或UNIQUE索引。 任何人都会这样做。 ALTER TABLE dbo.MyTable WITH CHECK ADD CONSTRAINT UQ_MyTable_BookTitle UNIQUE (BookID, Title) 要么 CREATE UNIQUE INDEX IXU_MyTable_BookTitle ON dbo.MyTable (BookID, Title) UNIQUE CONSTRAINT or UNIQUE index. Either ...
-
SQL Server 2008:INSERT如果不退出,则维护唯一的列(SQL Server 2008: INSERT if not exits, maintain unique column)[2022-07-15]
没有索引是疯狂的。 这将有所帮助,但索引键长度只能是900字节。 但是,很可能您已经有重复项目,因为第二个EXISTS可能会在第一个EXISTS之后但第一个INSERT之前运行。 索引创建会告诉你,并随后防止这种情况发生。 但是,您可以在重负载下获得错误。 我喜欢的高插入/低重复的方法是JFDI模式。 高度并发 BEGIN TRY INSERT etc END TRY BEGIN CATCH IF ERROR_NUMBER() <> 2627 RAISERROR etc END C ... -
在SQL Server 2008中维护“设置”的最有效方法?(Most efficient way to maintain a 'set' in SQL Server 2008?)[2021-11-28]
并非所有列出的解决方案在功能上都是等效的,因此如果不了解您想要或需要完成的内容,很难说哪种解决方案最合适。 您可能会丢失您想要或需要保留的数据。 根据您提到的表模式,这应该是合理的。 这仅在您单独执行每个INSERT时才有效。 我根据可用的信息建议[2]。 Not all of your listed solutions are functionally equivalent, so without more knowledge about what you want or need to accompli ... -
错误消息说明了一切 列名称“StateID”无效。 无效的列名称“State”无效的列名称“StateCode”无效的列名称“CountryId” INSERT语句中的列名不应包含在单引号(') 。 您应按原样编写列名称或将其括在[]如下所示 $query ="INSERT INTO State (StateID, State, StateCode, CountryId) VALUES (203, 'Dehli', 'DH', 100)"; 要么 $query ="INSERT INTO [Sta ...
-
newsequentialid主要用于解决表格由唯一标识符聚簇时页面碎片的问题。 您的表由一个整数列聚簇。 我设置了两个测试表,其中一个是newsequentialid列是主键,另一个是不是的(像你的那样),在主键中,GUID始终是顺序的。 另一方面,他们不是。 我不知道内部结构/技术原因为什么会出现这种情况,但是看起来很清楚,当你的表被聚集在一起时,newsequentialid()只是真正的顺序。 否则,它的行为似乎与newid()/ RowGuid类似。 另外,我很好奇你为什么要在不需要的时候使用ne ...
-
SQL Server 2008 - 将多值列拆分为具有唯一值的行(SQL Server 2008 - split multi-value column into rows with unique values)[2022-12-03]
With SampleInputs As ( Select 'key:value;key2:value;blah;foo;bar;A sample value:whee;others' As [Data] Union All Select 'key:value;blah;bar;others' Union All Select 'A sample value:whee' ) , XmlCte As ( Select Cast( '' + ...