Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

set storage of an object of arrays in Ionic 5

I have an ionic 5 app to be build for browser. I have installed @ionic/Storage, etc. I have a function in my service the current iteration is as follows:

async setStorageObj( key: string, values: any) {
console.log(key, values);
return this.storageCtrl.set( key, values );

}

"key" is a single string "product attributes" and values is an object of arrays(I think). It looks like this in the developer console: Product Attributes: [sportsdemo_x_xxx: [{key:value pairs}, {key:value pairs}, {key:value pairs}], sportsdemo_y_yyyyy: [{key:value pairs}, {key:value pairs}], ...etc]

The console log above shows correct in the console so I know the values are there. However the its showing up in storage as "ProductAttributes: []. I am simply unable to figure out the correct syntax. I've been at it for a awhile and its late... maybe I am just tired. Your help would be appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I came up with two solution to this question:

  1. loop through the object of arrays and set each array

    for (k in object) { 
       this.storageCtrl.set (k, object(k)); }
    

or

  1. the solution I went with:

    async setStorageObj( values: any[]) {
        return await this.storageCtrl.set("productAttributes", JSON.stringify(values));
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...