HarmonyOS 鸿蒙map转成ValuesBucket方法记录
HarmonyOS 鸿蒙map转成ValuesBucket方法记录
import { ValuesBucket } from ‘@ohos.data.ValuesBucket’;
function mapToValuesBucket(map: Map<string, string | number | boolean>): ValuesBucket {
let valuesBucket: ValuesBucket = { }
map.forEach((value, key) => {
valuesBucket[key] = value
});
return valuesBucket;
}
@Entry
@Component
struct Page067 {
build() {
Column() {
Button(‘测试’).onClick(() => {
const myMap = new Map<string, string | number | boolean>();
myMap.set(‘name’, ‘张三’);
myMap.set(‘age’, 28);
myMap.set(‘isStudent’, false);
<span class="hljs-keyword"><span class="hljs-keyword">const</span></span> valuesBucket = mapToValuesBucket(myMap);
console.info(`valuesBucket:${<span class="hljs-built_in"><span class="hljs-built_in">JSON</span></span>.stringify(valuesBucket)}`)
})
}
.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
打印
valuesBucket:{“name”:“张三”,“age”:28,“isStudent”:false}
2 回复
🌹