Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

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.

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

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. Image

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Limit items in object

Hello Ellen,

Please parse this value before using the method slice :

prevar limit =JSON.parse(post).slice(0, 4);/pre

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Limit items in object

Thanks. That limits the object well but it returns as [object Object],[object Object],[object Object],[object Object]. I used typeof to make sure that post was a string and it was so I'm not sure why it won't work.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Limit items in object

After calling the function slice, you have to call the method "stringify", so the final code is
prevar limitObject = post && post.length ? JSON.parse(post).slice(0, 4) : [];
var limit = JSON.stringify(limitObject);/pre

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Limit items in object

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:
prevar current = localStorage.getItem('shottolist')

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

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.

Image Image

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.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Limit items in object

  1. You didn't parse the array "shottolist" from localStorage. in your example - it is a string
    1. This is the feature of ES6. Appery.io supports only ES5.
    2. What exactly value do you need to get? What is the value of the variable shottolist?
Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Limit items in object

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.
Image

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Limit items in object

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

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

here arr - the variable with your array
unique - the variable with the array of unique values

Ellen Schlechter
Posts: 0
Joined: Sun Aug 31, 2014 3:28 am

Limit items in object

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.

pre
&#47;&#47;existing object
var unique = localStorage&#46;getItem('shottolist');
&#47;&#47;potential new item
var arr = {shot: "ss", dose: "33"};

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

Code: Select all

 } 
 if (isUnique) { 
    unique&#46;push(a[i]); 
 } 

}

&#47;&#47;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&#46;getItem('shotprelist')
var pre2 = pre&#46;substring(
pre&#46;lastIndexOf("[") + 1,
pre&#46;lastIndexOf("]")
);

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

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

&#47;&#47;limits items
var limitObject = post && post&#46;length ?

Code: Select all

 JSON&#46;parse(post)&#46;slice(0, 5) : []; 

var limit = JSON&#46;stringify(limitObject);

localStorage&#46;setItem("shottolist", limit);

/pre

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Limit items in object

  1. Please pass the array to the variable "arr" (now you are using the object there)

    1. The variable "unique" should be an empty array before calling that code

    2. You can see the full list of unique values in the variable "unique".

    3. If you need to save it to the localStorage or print to the console - please stringify it first.

      Here is an example:
      prevar arr = [{shot: "ss", dose: "33"},{shot: "ss", dose: "33"},{shot: "11", dose: "11"}];
      var unique = [];
      for (var i = 0; i < arr&#46;length; i++) {
      var isUnique = true;
      for (var j = 0; j < unique&#46;length; j++) {
      if (arr&#46;shot === unique[j]&#46;shot || arr&#46;dose === unique[j]&#46;dose){
      isUnique = false;
      }
      }
      if (isUnique) {
      unique&#46;push(arr);
      }
      }
      console&#46;log(JSON&#46;stringify(unique));
      /pre

Return to “Issues”