-1

I Have a string like

[ "Basic, JavaScript, PHP, Scala" ]

How to convert it like

[ 'Basic', 'JavaScript', 'PHP', 'Scala' ]

code

function loadDataToGridChiefComplaint() {
  var tHRNo = document.getElementById("lblpthrno").value;
  var tOPDNo = document.getElementById("lblptopd").value;
  var localKeyChief = tHRNo + '-' + tOPDNo + '-ChiefComplaint';
  var a = localStorage.getItem(localKeyChief);
  var Item = JSON.stringify(a); // it show like ["Basic, JavaScript, PHP, Scala"]
}
Ivan
  • 34,531
  • 8
  • 55
  • 100
aparna rai
  • 823
  • 10
  • 24
  • Try using `split` – CertainPerformance Jun 28 '18 at 10:38
  • 1
    Possible duplicate of [How do I split a string, breaking at a particular character?](https://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – Ben Fortune Jun 28 '18 at 10:39
  • `localStorage.getItem()` will return `DOMString`, once you have `DOMString` try using `JSON.parse()` and then you can use `string#split` to get the array back. – Hassan Imam Jun 28 '18 at 10:49

2 Answers2

2

JSON.stringify() returns a String so your question is incorrect. Item is not an Array.

const Item = '["Basic, JavaScript, PHP, Scala"]';

Therefore you should not use it. Instead simply return the array a in your function:

function loadDataToGridChiefComplaint() {
  var tHRNo = document.getElementById("lblpthrno").value;
  var tOPDNo = document.getElementById("lblptopd").value;
  var localKeyChief = tHRNo + '-' + tOPDNo + '-ChiefComplaint';
  return localStorage.getItem(localKeyChief); // <-- no use of JSON.stringify()
}

This way loadDataToGridChiefComplaint() is the array ["Basic, JavaScript, PHP, Scala"], it has a single element of type String that you can access with the bracket notation Item[0]:

const Item = ["Basic, JavaScript, PHP, Scala"];
console.log(Item[0]);

So in order to convert the string Item[0] into an array, use the .split method:

String.split(separator)

The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

MDN Web Docs

const Item = ["Basic, JavaScript, PHP, Scala"];
console.log(Item[0].split(', '));

If you can't modify this function you can use the opposite operation of JSON.stringify which is JSON.parse to convert the string back to an array:

const ItemString = '["Basic, JavaScript, PHP, Scala"]';
ItemArray = JSON.parse(ItemString);

And then use .split (like the previous example) to get the array of strings.

Community
  • 1
  • 1
Ivan
  • 34,531
  • 8
  • 55
  • 100
0

Try this :

var Item = ["Basic, JavaScript, PHP, Scala"];

// Do like this

var array = Item[0].split(', '); // Split with Comma and space(for trim whitespace)

// Or Like this

var array = Item[0].split(',').map(i => i.trim()) // Split with comma and use map to trim whitespace

console.log(array);
Ivan
  • 34,531
  • 8
  • 55
  • 100
Jagjeet Singh
  • 1,564
  • 1
  • 10
  • 23
  • 2
    always useful when people just down vote an answer without leaving a comment on why. Your solution seems fine to me considering the question. – mrdeadsven Jun 28 '18 at 10:55
  • Thanks @mrdeadsven – Jagjeet Singh Jun 28 '18 at 11:04
  • In his example, the value of `Item` is not an array, it's a string. So it should be `var Item = '["Basic, JavaScript, PHP, Scala"]';`. That's what makes your answer wrong. – eisbehr Jun 28 '18 at 12:14
  • @eisbehr please check again, `Item` variable is array in his example, you can get value like this `Item[0]` which returns you `Basic, JavaScript, PHP, Scala` – Jagjeet Singh Jun 28 '18 at 12:17
  • What do you think `JSON.stringify` will return? Its even in the name of the function, that it returns a string. ;) Please see: http://jsfiddle.net/8jtvwp72/1/ – eisbehr Jun 28 '18 at 12:24
  • 1
    @eisbehr ok, so in the end it comes down to 2things for me. 1) OP need to formulate his question better 2) you can still simply solve it by Jagjeet's way if you just do a substring first that leaves out the '[]'. – mrdeadsven Jun 28 '18 at 12:28
  • 1
    @eisbehr sure `JSON.stringify` return string, but as question they want to convert this `[ "Basic, JavaScript, PHP, Scala" ]` to array, so i answer it, in the code they use stringify so you can edit my answer and mention it to use JSON.parse or i can – Jagjeet Singh Jun 28 '18 at 12:29
  • So you mean, your answer is wrong and you know it, but the OP could simply change your answer to find the correct way? So why did you post an answer, if this isn't an answer to this question?! Wired. @JagjeetSingh – eisbehr Jun 28 '18 at 12:32
  • Why ... why would you use `substr`? Why not change his function **before** the JSON and make it working?! @mrdeadsven – eisbehr Jun 28 '18 at 12:34
  • @eisbehr I read it wrong, I thought that he had to use JSON.stringify and that is why he used it here. So I was going from the point of view that he had to first become an array like object as a string and that he had to convert it as he stated in his question of 'how to convert it to an array'. But if it's not a question of conversion and he just needs and array he simply shouldn't use the JSON.stringify – mrdeadsven Jun 28 '18 at 12:43
  • @eisbehr when i answer, i did not see the `JSON.stringify` that's why i answer, now leave it @Ivan answered it again :) – Jagjeet Singh Jun 28 '18 at 12:53