小编Phi*_*hil的帖子

为什么ListAppend是非破坏性的,而ArrayAppend和StructInsert都具有破坏性?

我花了差不多一个小时试图找出一个总是返回一个空字符串的列表的问题.我正在使用ListAppend就像使用ArrayAppend或StructInsert,但显然ListAppend的工作方式不同.如果有的话,ListAppend的工作原理与其他一切有什么不同?

<cfset ListAppend(list, item)>
Run Code Online (Sandbox Code Playgroud)

list =''

<cfset ArrayAppend(array, item)>
Run Code Online (Sandbox Code Playgroud)

array [1] = item

<cfset StructInsert(struct, 'key', item)>
Run Code Online (Sandbox Code Playgroud)

struct.key = item

coldfusion

9
推荐指数
2
解决办法
393
查看次数

创建结构数组的问题

我正在制作一个博客API,并且在尝试在coldfusion中创建一个结构数组时遇到一些非常奇怪的问题.顶级数组将包含作为结构的帖子,其中.comments是该帖子下所有注释的数组,也是结构.

以下代码中的每个部分都单独工作.但是,不知何故,当我把它们放在一起时,我最终会得到一个无限嵌套的结构数组,其中包含一个结构数组等...所有这些只是帖子顶级数组中的最后一个元素.

<cfset posts = VARIABLES.postDao.getBlogPosts(argumentCollection=arguments) />
<cfset result = arraynew(1) />

<cfloop index="i" from="1" to="#arrayLen(posts)#">
  <cfset post = posts[i].getInstance()>
  <cfset StructInsert(post, 'comments', getComments(post.postId))>
  <cfset ArrayAppend(result, post)>
</cfloop>
Run Code Online (Sandbox Code Playgroud)

getBlogPosts返回一个Post bean数组.
bean.getInstance()返回一个包含bean中所有数据的结构.
getComments(id)返回一个数组post [id]的所有注释(结构).

这些中的每一个都按预期工作并且在其他地方使用而没有问

无限嵌套数组的结构如下:

Array containing Post  
.  Post.comments containing array of comments + Post on end  
.  .  Post.comments containing array of comments + Post on end
.  .  . etc...
Run Code Online (Sandbox Code Playgroud)

coldfusion multidimensional-array

4
推荐指数
1
解决办法
3988
查看次数

标签 统计

coldfusion ×2

multidimensional-array ×1