Limit items in object

I have an object saved in local storage and I want to limit the items in that object to 5.

I have tried using slice like this, but it returns 5 characters instead of 5 items.

var limit = (post.slice(0, 4));

For example, this is what is saved:

[{shot: "ll", dose: "ll"}, {shot: "ss", dose: "33"}, {shot: "qq", dose: "44"},...]

But this is what I get: [{sho

The screenshot shows what is in local storage and that is exactly what I want except only 5.
1 person has
this question
+1
Reply
  • Hello Ellen,


    Please parse this value before using the method slice :


    var limit =JSON.parse(post).slice(0, 4);
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Thanks that worked perfectly.

    My next question:
    I am trying to search this object saved in storage for certain values and I want it to return true or false.

    This is what I have tried:
    var current = localStorage.getItem('shottolist')

    console.log("result " + current.some( findshot => findshot['shot'] === 'ss' ))


    I tried this as seen in the screenshot and it works how it want it to but when I add it to my Appery.io project, I get this error.



    If you have advice on how to fix the error, that would be great. Or is there a better way to do this?

    Also, my object has two parameters, "shot" and "dose". The logic I have in mind (shown above) only searches for a match to "shot". Do you know how I can see if a certain value matches "shot" and if a different value matches "dose" in the same item? Basically an exact match vs partial match.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • This screenshot shows the value of shottolist. I want to prevent there from being a duplicate item in the object. Since I am limiting it to 5, I want to make sure that every item is unique instead of having duplicates. So for example, I would allow "ss" and "23" to go through and be added, but I would not allow "ss" and "33" to be added to the variable since there is one already there. I just need to run a simple search to that returns true or false depending on if we have a complete match or a partial/no match.

    I want to prevent the situation like 1 and 3 are the exact same.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Please use the JS code below for that:
    var unique = [];

    for(var i=0; i < arr.length; i++){
    var isUnique = true;
    for(var j=0; j < unique.length; j++){
    if (arr[i].shot === unique[j].shot || arr[i].dose === unique[j].dose){
    isUnique = false;
    }
    }
    if (isUnique){
    unique.push(a[i]);
    }
    }


    here arr - the variable with your array
    unique - the variable with the array of unique values
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • I'm sorry, I don't specifically understand that code so I am having trouble implementing it. Using the code you last provided, does that automatically add a new item to my object? It didn't seem to add it to shottolist, but it essentially needs to make it to shotprelist but I believe I can do that manually unless this code is just supposed to update it.

    My plan was to run the code that follows the code you last provided only if the new item is unique. I'm sorry, I don't really know how to explain this and make it make sense. But does your code add to the object, or does it return a true/false depending on if it unique? And how do I access that true/false because I wan't able to access it with console.log.


    //existing object
    var unique = localStorage.getItem('shottolist');
    //potential new item
    var arr = {shot: "ss", dose: "33"};

    //var unique = [];
    for (var i = 0; i < arr.length; i++) {
    var isUnique = true;
    for (var j = 0; j < unique.length; j++) {
    if (arr[i].shot === unique[j].shot || arr[i].dose === unique[j].dose){
    isUnique = false;
    }

    }
    if (isUnique) {
    unique.push(a[i]);
    }
    }

    //lines 21-35 add the new item to the front of the list so that when I limit, the oldest item is removed
    var pre = localStorage.getItem('shotprelist')
    var pre2 = pre.substring(
    pre.lastIndexOf("[") + 1,
    pre.lastIndexOf("]")
    );

    var current = localStorage.getItem('shottolist')
    var current2 = current.substring(
    current.lastIndexOf("[") + 1,
    current.lastIndexOf("]")
    );

    var post = "[" + pre2 + "," + current2 + "]";

    //limits items
    var limitObject = post && post.length ?

    JSON.parse(post).slice(0, 5) : [];

    var limit = JSON.stringify(limitObject);

    localStorage.setItem("shottolist", limit);

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 1. Please pass the array to the variable "arr" (now you are using the object there)
    2. The variable "unique" should be an empty array before calling that code
    3. You can see the full list of unique values in the variable "unique".
    4. If you need to save it to the localStorage or print to the console - please stringify it first.

    Here is an example:
    var arr = [{shot: "ss", dose: "33"},{shot: "ss", dose: "33"},{shot: "11", dose: "11"}];
    var unique = [];
    for (var i = 0; i < arr.length; i++) {
    var isUnique = true;
    for (var j = 0; j < unique.length; j++) {
    if (arr[i].shot === unique[j].shot || arr[i].dose === unique[j].dose){
    isUnique = false;
    }
    }
    if (isUnique) {
    unique.push(arr[i]);
    }
    }
    console.log(JSON.stringify(unique));
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Thank you. I understand now. Using arr that you defined it works great. arr needs to be dynamic, however, so I tried to define by getting an object out of local storage and instead of returning an updated object, it just returns what is in the screenshot.



    My object in storage looks the same as yours that you hard-coded in so I'm not sure what the issue is.

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Please parse the value from the local storage before using it in this code. Please use the function JSON.parse() for that
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated