如何更新 X++ 地图

How to update X++ map

我想更改地图中键的值。我该怎么做? 可能吗?

我只找到方法 insert(_key,_value) 但我不想用值创建新键,而是更改现有键的值。

如何编辑密钥

只需移除旧密钥,然后用新密钥重新插入。

map = new Map(Types::String,Types::Real)
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
map.remove("b");     // remove key
map.insert("y", 2);  // reinsert new key with value

如何编辑值

只需用 insert 重新插入值。密钥不能重复,而是覆盖。

Return Value
Type: boolean
true if the key did not already exist in the map and has been inserted; otherwise, false.
Remarks
If the key already exists in the map, the value is updated.

例如,手动汇总项目组的行金额分组:

Map map = new Map(Types::String,Types::Real);
SalesLine sl;
while select sl where sl.SalesId == "123"
{
    map.insert(sl.ItemGroup, sl.LineAmount + (map.exists(sl.ItemGroup) ? map.lookup(sl.ItemGroup) : 0);
}

等同于但在性能方面不如:

select sum(LineAmount) sl group ItemGroup where sl.SalesId == "123";

在插入另一个更新键值的值之前,无需删除键。您只需再次调用 insert() 即可。

'insert()' 在 SQL 术语中更准确地称为 'upsert()'。

Map mymap;
mymap.insert('a',1.0);
mymap.insert('a',1.1);
info(strFmt('%1',mymap.lookup('a'))); // will return '1.1'