Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:19:59+00:00 2026-05-26T19:19:59+00:00

In localstorage I have key ‘ results ‘ with this values : [{id:item-1,href:google.com,icon:google.com}, {id:item-2,href:youtube.com,icon:youtube.com},

  • 0

In localstorage I have key ‘results‘ with this values:

[{"id":"item-1","href":"google.com","icon":"google.com"},
{"id":"item-2","href":"youtube.com","icon":"youtube.com"},
{"id":"item-3","href":"google.com","icon":"google.com"},
{"id":"item-4","href":"google.com","icon":"google.com"},
{"id":"item-5","href":"youtube.com","icon":"youtube.com"},
{"id":"item-6","href":"asos.com","icon":"asos.com"},
{"id":"item-7","href":"google.com","icon":"google.com"},
{"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}]

To get the last item I use this:

// this is how I parse the arrays
var result = JSON.parse(localStorage.getItem("result")); 


for(var i=0;i<result.length;i++) {
    var item = result[i];
    $('element').val(item.href);
}

How can I get the href for item-3 or for a specific ID?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:19:59+00:00Added an answer on May 26, 2026 at 7:19 pm

    jQuery has filter helper for that:

    $(result).filter(function(){return this.id == "item-3";})[0]
    

    Function for href of item with specific id would be:

    function getItemHrefById(json, itemId){
        return json.filter(function(testItem){return testItem.id == itemId;})[0].href;
    }
    

    And sample usage is:

    var href = getItemHrefById(result, "item-3");
    

    You can see working example on http://jsfiddle.net/LXvLB/

    UPDATE

    If you cannot read item from local storage, maybe you forgot to call JSON.stringify when setting value:

    localStorage["results"] = JSON.stringify([{"id":"item-1","href":"google.com","icon":"google.com"}, 
    {"id":"item-2","href":"youtube.com","icon":"youtube.com"}, 
    {"id":"item-3","href":"google.com","icon":"google.com"}, 
    {"id":"item-4","href":"google.com","icon":"google.com"}, 
    {"id":"item-5","href":"youtube.com","icon":"youtube.com"}, 
    {"id":"item-6","href":"asos.com","icon":"asos.com"}, 
    {"id":"item-7","href":"google.com","icon":"google.com"}, 
    {"id":"item-8","href":"mcdonalds.com","icon":"mcdonalds.com"}])
    

    You need to convert json to string to be properly serialized (and to use JSON.parse to get JSON back)

    This is final example.

    EDIT

    As Useless Code pointed out, this metod is substantially slower than native filter function (and custom loop but I think that introducing few new lines of code to save 20-30ms is overkill unless performance is a issue), so I’m updating my example to not use jquery filter. +1 please for his answer for that.

    Also, what is important to point out here, if this array would have hundreds instead of 8 bookmarks, for loop would probably be statistically about twice faster (as it does not have to iterate through rest of the array). But, in that case it would probably be a good idea to put for loop into function which returns first found item which satisfies condition, and with prototypejs it probably can even be hooked up to array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this in my localStorage: [{id:item-1,href:google.com,icon:google.com}, {id:item-2,href:youtube.com,icon:youtube.com}, {id:item-3,href:google.com,icon:google.com}, {id:item-4,href:google.com,icon:google.com}, {id:item-5,href:youtube.com,icon:youtube.com}, {id:item-6,href:asos.com,icon:asos.com}, {id:item-7,href:google.com,icon:google.com}, {id:item-8,href:mcdonalds.com,icon:mcdonalds.com}]
How can I count how many different values I have in 1 localstorage key.
I have this code: for(var i=0; i < localStorage.length; i++) { var subjects =
I have a login page that stores a few values to localStorage (html5) then
I have been experimenting with the localstorage module for Backbone.js (https://github.com/jeromegn/Backbone.localStorage). As I understand
I have few tabs which their ID is the Item in localstorage which I'm
I have the following localStorage key: localStorage.setItem(1, <div id='MyId'>value 1</div><div id='NewId'>other value</div>); Is it
I have this code which I have found from google. It's a adapter for
Im using this code: $(this).css('backgroundcolor', localStorage.getItem('bgColorr') + !important;); When i write: alert( localStorage.getItem('bgColorr') +
I've looked up and down and have found a few references to recovering localstorage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.