Object 值方法
Object 字段应写入 JSON 兼容的普通对象。读取属性前,先判断未知入参是否为对象。
赋值
const location = {
address: String(payload.input.address || '').trim(),
city: String(payload.input.city || '').trim(),
};
await context.form.update({
recordId: 'record-id',
data: {
delivery_location: location,
},
});
可接受入参示例:
| 入参 | 归一化结果 |
|---|---|
{ address: '...', city: 'Shanghai' } | 保持对象结构 |
| JSON 文本 | 写入前先解析 |
null | 字段允许清空时用于清空 |
常用方法
| 方法 | 用途 |
|---|---|
Object.keys(value) | 获取属性名 |
Object.values(value) | 获取属性值 |
Object.entries(value) | 遍历属性键值对 |
{ ...value, key: next } | 生成更新后的对象 |
value?.property | 读取可选嵌套属性 |
const raw = record.data.delivery_location;
const location =
raw && typeof raw === 'object' && !Array.isArray(raw)
? (raw as Record<string, unknown>)
: {};
Object.keys(value)
获取属性名。
const hasAddress = Object.keys(location).includes('address');
Object.values(value)
获取属性值。
const filledParts = Object.values(location).filter((value) =>
String(value || '').trim()
);
Object.entries(value)
遍历属性键值对。
const normalizedLocation = Object.fromEntries(
Object.entries(location).map(([key, value]) => [
key,
typeof value === 'string' ? value.trim() : value,
])
);
{ ...value, key: next }
生成更新后的对象,不直接修改原对象。
const city = String(location.city || '');
const normalized = {
...location,
city: city.trim(),
};
value?.property
读取可选嵌套属性。
const coordinates =
location.coordinates &&
typeof location.coordinates === 'object' &&
!Array.isArray(location.coordinates)
? (location.coordinates as Record<string, unknown>)
: null;
const longitude = Number(coordinates?.lng);