javascript - Fetch localstorage value in array - Stack Overflow

I am saving value in localstorage as shown belowkey = profskill , value = "a,b,c"In my test.t

I am saving value in localstorage as shown below

key = profskill , value = "a,b,c"

In my test.ts file, I have declared array but I am unable to fetch the result in it. Code shown below:

 getskills: Array<string> = [];
this.getskills  = localStorage.getItem("profskill");

but this is giving error:

Type 'string' is not assignable to type 'string[]'

I want to fetch value like this:

console.log(this.getskills[0]);

I am saving value in localstorage as shown below

key = profskill , value = "a,b,c"

In my test.ts file, I have declared array but I am unable to fetch the result in it. Code shown below:

 getskills: Array<string> = [];
this.getskills  = localStorage.getItem("profskill");

but this is giving error:

Type 'string' is not assignable to type 'string[]'

I want to fetch value like this:

console.log(this.getskills[0]);

Share Improve this question edited Feb 8, 2020 at 9:17 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Aug 31, 2017 at 9:27 user2828442user2828442 2,5257 gold badges67 silver badges120 bronze badges 4
  • value needs to be an array containing 'a','b','c' – FAISAL Commented Aug 31, 2017 at 9:30
  • i am saving this way this.service.profskills_value = this.skills + "," + this.skills2 + "," + this.skills3; , please advise how will you save it ? – user2828442 Commented Aug 31, 2017 at 9:32
  • localStorage.setItem( 'profskill', JSON.stringify( ['a', 'b', 'c'] ) ) – fedetibaldo Commented Aug 31, 2017 at 9:33
  • @user2828442 you can tick the answer if if was helpful to you – Ankit Agarwal Commented Aug 11, 2018 at 6:01
Add a ment  | 

4 Answers 4

Reset to default 3

The LocalStorage can only store strings, not objects or arrays. If you try to store an array, it will automatically be converted to a string. You need to parse it back to an array :

JSON.parse( localStorage.getItem("profskill") )

Since, you want the ma separated value to be represented as a array of strings for this.getskills use split on the value of the localStorage

Here is a sample example

 //say we get the value 'a,b,c' from localStorage into the temp variable
 //var temp = localStorage.getItem(profskill);
  var temp= 'a,b,c';
 
 this.getskills = temp.split(',');
 console.log(this.getskills[0]);
 

localStorage only supports strings. Use JSON.stringify() to set the data in storage and JSON.parse() to get the data from storage and then use split(",") to split the ma separated data.

var obj = "a,b,c";
localStorage.setItem("profskill", JSON.stringify(obj));

var getskills = [];

getskills = JSON.parse(localStorage.getItem("profskill")).split(",");

console.log(getskills[0]);

First get the data from the LocalStorage:

var DataTableValue = JSON.parse(localStorage.getItem('dataTableValue'));

Then, store in an Array:

var tempArray = new Array();
for (var i = 0; i < DTarray.length; i++) {
    tempArray.push(DTarray[i]);
}

All data will be stored in the variable tempArray.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744339502a4569319.html

相关推荐

  • javascript - Fetch localstorage value in array - Stack Overflow

    I am saving value in localstorage as shown belowkey = profskill , value = "a,b,c"In my test.t

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信