[英]How do I update an array property in a Palantir Foundry Ontology edit Function?
I want to modify an array property on an Object using an Ontology Function (aka FoO), but I'm seeing the following error:我想使用本体函数(又名 FoO)修改对象上的数组属性,但我看到以下错误:
[typescript] Property 'push' does not exist on type 'readonly string[]'.
[typescript] 类型“readonly string[]”上不存在属性“push”。
Looking at the generated TypeScript definition for my Object type, it looks like my array property has type ReadonlyArray<string> | undefined
查看为我的 Object 类型生成的 TypeScript 定义,看起来我的数组属性的类型为
ReadonlyArray<string> | undefined
ReadonlyArray<string> | undefined
How can I update this array from my Function?如何从我的函数更新这个数组?
You need to assign a new value to the property rather than manipulate the existing array in-place.您需要为属性分配一个新值,而不是就地操作现有数组。
Array properties on an object type have immutable values to make the semantics for editing an array property clear: the only way to modify the values of an array property is to assign an entirely new array value.对象类型的数组属性具有不可变的值,以使编辑数组属性的语义清晰:修改数组属性值的唯一方法是分配一个全新的数组值。
If you want to manipulate the values of an array property, make a copy of it and update that (as described in the Foundry docs ):如果要操作数组属性的值,请对其进行复制并更新(如Foundry 文档中所述):
// Copy to a new array
let arrayCopy = [...myObject.myArrayProperty];
// Now you can modify the copied array
arrayCopy.push(newItem);
// Then overwrite the property value
myObject.myArrayProperty = arrayCopy;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.