11 Replies - 3509 Views - Last Post: 12 April 2013 - 07:47 AM

#1 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:25 PM

I am using the following code:

    // History Information Array
    var historyInfo = localStorage.getItem("history");
    if (!historyInfo) {
        historyInfo = [];
    } else {
        historyInfo = JSON.parse(historyInfo);
    }
    historyInfo.push("You travelled " + distance + " mile(s) in " + duration + " on " + timestamp + "." + "<br>");
	
    localStorage.setItem("history", JSON.stringify(historyInfo));



This works fine!

However, when I return the values of the array to my web page using:

document.getElementById("history").innerHTML = localStorage.getItem("history");


The results appear like so:

["You travelled 0 mile(s) in 00:00 on 11 April 2013 20:24:32.
","You travelled 0 mile(s) in 00:00 on 11 April 2013 20:24:42.
","You travelled 0 mile(s) in 00:00 on 11 April 2013 20:24:53.
"]

How can I remove the square brackets and quotation marks?

Is This A Good Question/Topic? 0
  • +

Replies To: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:37 PM

View PostLiam O, on 11 April 2013 - 09:25 PM, said:

How can I remove the square brackets and quotation marks?

that depends on how you want to display those 3 results.
Was This Post Helpful? 0
  • +
  • -

#3 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:42 PM

I want to display them as shown (minus the square brackets and quotation marks).
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:45 PM

then join the array with a HTML line break.
Was This Post Helpful? 0
  • +
  • -

#5 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:52 PM

I believe I have done this. Please review my code to ensure I have. If I have, then what?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 12:54 PM

all you did was loading the serialised array from localStorage. and that (brackets and quotes) is what a serialised array looks like.
Was This Post Helpful? 0
  • +
  • -

#7 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 01:06 PM

Could you be more specific please? I don't understand what I'm doing wrong. What do I have to do to remove the square brackets and quotation marks?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 01:08 PM

parse the JSON-stringified array (from localStorage.getItem()) back into an array and join it again for HTML output.
Was This Post Helpful? 0
  • +
  • -

#9 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 01:35 PM

Sorry, I don't quite understand what I have to do. Thanks anyway Dormilich.
Was This Post Helpful? 0
  • +
  • -

#10 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 11 April 2013 - 02:17 PM

I am about to give up on you. :surrender:

I gave you an example of how to do this in post #23 of your other thread concerning the same subject.

See: http://www.dreaminco...ue-array-error/

It shows how to get rid of your bracket symbols and break the string into individual records for single displays.

I'm beginning to believe you do not read or understand the answers that are being generated for you.
Was This Post Helpful? 1
  • +
  • -

#11 Liam O'Byrne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 02-April 13

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 12 April 2013 - 06:56 AM

JMRKER, thank you so much for your time and effort! I successfully resolved my problem thanks to your support!
Was This Post Helpful? 0
  • +
  • -

#12 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: JSON.parse/JSON.stringify Array - Remove Square Brackets & Quotati

Posted 12 April 2013 - 07:47 AM

View PostLiam O, on 12 April 2013 - 07:56 AM, said:

JMRKER, thank you so much for your time and effort! I successfully resolved my problem thanks to your support!


You're welcome.

You might want to share your final solution with other forum members
who may have been following your threads...
either a code posting or a link to a live demo.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1