首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从数组ColdFusion中删除关联的对象

从数组ColdFusion中删除关联的对象
EN

Stack Overflow用户
提问于 2012-02-23 03:25:26
回答 3查看 1.9K关注 0票数 2

我有一个购物车数组,它保存购物车中物品的产品信息。添加的一些产品,如果与“主要项目”一起购买,将会有特别折扣。

如果有人添加了一个与特价商品相关联的商品,我在商品结构中设置了一个名为mainitem的值为yes的键,所有与主商品相关联的后续特价商品都将关键字mainitem设置为no,并具有另一个名为mainitemid的密钥(这是mainitem uid)。

如果有人删除了主要项目,我需要确保任何相关的特别优惠项目也被删除。这就是我遇到麻烦的地方,我不太清楚如何找到它们。

我使用form.itemID来提供要删除的产品的项目id。我需要确保要删除的项目是主项目;如果是,则遍历购物车的其余部分,找到任何mainitemid等于form.itemid的项目并删除它们,然后删除mainitem

mainitem定义为session.mycart[i].mainitem维护is定义为session.mycart[i].mainitemid

代码语言:javascript
运行
复制
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">

</cfloop>

我需要创建两个循环吗?或者我可以只创建一个循环吗?我不确定我的条件语句。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-23 07:23:56

针对运维特定问题的解决方案

经过修改以提供更完整的解决方案

代码语言:javascript
运行
复制
<!--- First of all, will need to go 1 to ArrayLen() to find the main item id.  Doesn't matter what order you go here, but main items should be inserted before children so may as well start at the bottom --->
<cfloop index="i" from="1" to="#ArrayLen(session.mycart)#">
    <!--- Is this the main item? --->
    <cfif session.mycart[i].id EQ form.itemID>
        <!--- It's found the item, is it a main item?  We've found the item so entering here to break the loop --->
        <cfif session.mycart[i].mainitem>
            <!--- Go from  ArrayLen() to 1, as if you go 1 to ArrayLen() when deleting you confuse Coldfusion --->
            <cfloop index="j" from="#ArrayLen(session.mycart)#" to="1" step="-1">
                <cfif session.mycart[j].id EQ form.itemID OR session.mycart[j].mainitemid EQ form.itemID>
                    <cfset ArrayDeleteAt(session.mycart,j)>
                </cfif>
            </cfloop>
        </cfif>
        <!--- This loop is done, so break out --->
        <cfbreak>
    </cfif>
</cfloop>

在您的帖子中,您声明您正在从索引1循环到索引ArrayLen(...)。但是,如果您要从数组中删除项,Coldfusion就有点简单,并不会真正关注它,因此当您删除一个5元数组中的元素2时,数组变成了4个元素长度(因为您删除了一个),而索引为3的元素现在是索引2,因此它被遗漏了。

绕过这一点的方法是从最后开始,然后向后工作。只要你一次最多删除一条记录,那么这是完全有效的,因为它将继续减少它当前检查的索引,直到你降到1,这将是第一个元素。

这样,你可以遍历元素5,4,3,2,删除元素2,然后它将检查索引1,它现在仍然是与你开始循环时相同的项,因此没有跳过。

一些关于如何处理此的格式回复信息

我误读了问题,或者在写这篇文章的时候它被编辑了,但是下面的内容是适用的,所以还是留在这里吧

您是否考虑过将特殊优惠项目作为主要项目的子项,因为它会将整个优惠封装在一起,删除父项会删除子项。我有一个类似的问题,项目有关联的选项,为了观察层次结构,我决定创建一个与引用数组相结合的树。作为演示该原理的粗略示例,请看以下内容

代码语言:javascript
运行
复制
<cfscript>
    // Create a main item structure
    stcMainItem = {
        id = CreateUUID(),
        somekey = somevalue,
        someotherkey = someothervalue,
        cost = 123
    };

    // Create some child item structures, special offers for ex
    stcSpecialOfferItem1 = {
        id = CreateUUID(),
        parent = stcMainItem.id,
        cost = 45
    };

    stcSpecialOfferItem2 = {
        id = CreateUUID(),
        parent = stcMainItem.id,
        cost = 45
    };



    // Each is added to a reference array
    arrItemReference = [];
    arrItemRefernce.add(stcMainItem);
    arrItemRefernce.add(stcSpecialOfferItem1);
    arrItemRefernce.add(stcSpecialOfferItem2);

    // Now you decide to delete the main item and direct parents
    strIDToDelete = stcMainItem.id;
    for (i=ArrayLen(arrItemReference);i>=1;i--) {
        if (
            arrItemReference[i].id == strIDToDelete
            ||
            arrItemReference[i].parent == strIDToDelete
        ) {
            ArrayDeleteAt(arrItemReference,i);
        }
    }
</cfscript>

在我的实际代码中,我已经通过创建一个Item.cfc的方式来实现这一点,该方法具有处理上述问题的方法,并在树上层叠删除孙子对象等,但原理是合理的。本质上,您拥有的方法允许将项同时公开为平面数组和父项、子项和兄弟项的层次结构。

信息点

顺便说一句,当" array“和"structure”与PHP等语言不同时,您需要不断地交换“array”和“structure”。在PHP中,array用于引用这两个术语。数组包含的值具有从1到n的数值索引,而结构是保存与任意键相关的值的对象。有区别的原因是它们不是建立在相同的基础java对象上,所以一些在一个上有效的方法在另一个上就不起作用了。

票数 2
EN

Stack Overflow用户

发布于 2012-02-23 03:31:33

代码语言:javascript
运行
复制
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
    <cfif session.mycart[i].mainitem eq "no" AND session.mycart[i].mainitemid EQ form.itemid>
        <cfset arrayDeleteAt(session.myCart,i) />
        <cfset i = i - 1 /><!--- this is necessary to keep you from running out of the end of the loop --->
    </cfif>
</cfloop>

当然,这还没有经过测试,您可能需要使用它来输入确切的变量名,但我认为它应该可以帮助您实现这一点。如果我误解了这个问题,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2012-02-23 03:56:47

我没有看到在一个循环中做到这一点的方法,因为我认为这是一个两步的过程。首先,您必须查看您删除的项id是否是主项,然后返回数组并删除其余相关项。

根据需要添加StructKeyExists()。

代码语言:javascript
运行
复制
<!--- Find Main Item --->
<cfset isMainItem = false />
<cfloop from='1' to='#ArrayLen( Session.MyCart )#' index='item'>
    <cfif Session.MyCart[ item ].ItemID EQ Form.ItemID AND Session.MyCart[ item ].MainItem EQ 'Yes'>
        <cfset isMainItem = true />
        <cfbreak />
    </cfif>
</cfloop>

<cfif isMainItem>
    <!--- Clean up special offer items --->
    <cfloop from='1' to='#ArrayLen( Session.MyCart )#' index='item'>
        <cfif Session.MyCart[ item ].MainItemID EQ Form.ItemID AND Session.MyCart[ item ].MainItem EQ 'No'>
            <cfset ArrayDeleteAt( Sesion.MyCart, Item ) />
            <cfset item-- />
        </cfif>
    </cfloop>
</cfif>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9401558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档