我正在将通讯录保存到storage。界面如下:
interface AddressBook {
a?: Contact[];
b?: Contact[]
// ...
}
我正在使用的实现是:
try {
const entries = this.addressBook.entries(); //<-- correct entries
await this.contactStorage.set('contacts', Array.from(entries));
const store: AddressBook = await this.contactStorage.get('contacts');
console.log('store', store); //<-- shows an array of only one object
} catch (error) {
console.log('e-->', error);
}
console.log在商店显示:
(3) [Array(2), Array(2), Array(2)]
0: (2) ["M", Array(1)]
1: (2) ["P", Array(1)]
2: (2) ["Z", Array(1)]
length: 3
"M"
在数组中应该有两个对象,但是保存时决定删除其中一个对象。
任何帮助都很棒
编辑:这是const entries
的控制台:
MapIterator {"M" => Array(1), "P" => Array(1), "Z" => Array(1)}
__proto__: Map Iterator
[[IteratorHasMore]]: true
[[IteratorIndex]]: 0
[[IteratorKind]]: "entries"
[[Entries]]: Array(3)
0: {"M" => Array(2)} //<-- both objects are there
1: {"P" => Array(1)}
2: {"Z" => Array(1)}
答案 0 :(得分:0)
在我们的项目中,我们正在使用这个:
https://github.com/typestack/class-transformer
您可以定义隐藏@expose的内容,然后可以将stringify json转换为class并再次返回
答案 1 :(得分:0)
我最终解决了这个问题,它正在从中编辑我的代码:
private async _createAddressBook(contactsArr: Contact[] = contacts): Promise<void> {
this._sortedArray = await contactsArr.sort(this._compare);
this._sortedArray.forEach(async (record: Contact): Promise<void> => {
const char = this._replaceFirstCharAccentMark(record.lastNames[0].toUpperCase());
if (this.addressBook.has(char)) {
const bucket: Contact[] = this.addressBook.get(char) as Contact[];
const sortedBucket: Contact[] = bucket.concat(record).sort(this._compare);
this.addressBook.set(char, sortedBucket);
} else {
this.addressBook.set(char, [record]);
}
});
}
对此:
private _createAddressBook(contactsArr: Contact[] = contacts): void {
this._sortedArray = await contactsArr.sort(this._compare);
this._sortedArray.forEach((record: Contact): void => {
const char = this._replaceFirstCharAccentMark(record.lastNames[0].toUpperCase());
console.log('char', char);
if (this.addressBook.has(char)) {
const bucket: Contact[] = this.addressBook.get(char) as Contact[];
const sortedBucket: Contact[] = bucket.concat(record).sort(this._compare);
this.addressBook.set(char, sortedBucket);
} else {
this.addressBook.set(char, [record]);
}
});
}
我不知道为什么异步/等待将其弄乱了。因为随着越来越多的联系,我得到的时间就越长。那么为什么要阻止它直到完成排序会产生这些副作用