Stupid JS question

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.

Stupid JS question

Postby KyleMassacre » Sun May 28, 2017 2:50 pm

Anyone who knows me knows that JS is not my strong suit but lately I have been forcing myself to work with it a lot more. I have a question about filter().

Let's say I have a simple array and each index of the array has several properties. What I would like to do is filter the array but be able to reverse the filter back to its normal state so I can apply other filters based on the original array without calling the database again to either get the original state or apply the filter(s) I want. Also note that I don't want to install a 3rd party package so let's try to act like that is not an option.

If have tried stuff like:
Code: Select all
var negate = function(cb) {
  return function() {
    return !cb.apply(this,arguments);
  }
};

var myFilter = function(m) {
  return m.someProp === "someString";
};

function applyFilter() {
  myArray.filter(myFilter);
}

function revertFilter() {
  myArray.filter(negate(myFilter));
}

The 'revertFilter' function basically erases my entire array when I call it.
Skype: KyleMassacre
My NWE Modules
User avatar
KyleMassacre
 
Posts: 547
Joined: Wed Nov 27, 2013 7:42 am
Has thanked: 18 times
Been thanked: 34 times

Re: Stupid JS question

Postby a_bertrand » Sun May 28, 2017 10:24 pm

The issue is that the .filter function creates a new array with only the entries which match the filter. So your reset should just return the original array not try to further filter the array. To do so, simply keep the original array somewhere. You may use .slice() to duplicate the array (warning it's not a deep copy).
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
a_bertrand
New Worlds Engine creator
 
Posts: 1445
Joined: Mon Feb 25, 2013 8:46 am
Has thanked: 15 times
Been thanked: 20 times

Re: Stupid JS question

Postby KyleMassacre » Sun May 28, 2017 10:54 pm

So basically you are saying to do something like:
Code: Select all
var myClass = new (class
{
  public mainArray: SomeType[];
  public cacheArray: SomeType[];
}
....
static GetData() {
  $.ajax({'...'},
  success: function(data) {
    myClass.mainArray = data;
    myClass.cachedArray = data;
  }
  ....
}
static ApplyFilter()
{
  return myClass.mainArray.filter(myFilter); // used from my first post
}

static RevertFilter()
{
  myClass.mainArray = myClass.cachedArray;
  return myClass.mainArray;
}
Skype: KyleMassacre
My NWE Modules
User avatar
KyleMassacre
 
Posts: 547
Joined: Wed Nov 27, 2013 7:42 am
Has thanked: 18 times
Been thanked: 34 times

Re: Stupid JS question

Postby a_bertrand » Mon May 29, 2017 9:24 am

I think you are somewhat having issues with pointers here, even if in any modern language you don't see them anymore any code still rely on it ;)

Code: Select all
var myClass = new (class
{
  public mainArray: SomeType[];
  public cacheArray: SomeType[];
}
....
static GetData() {
  $.ajax({'...'},
  success: function(data) {
    myClass.mainArray = data.slice();
    myClass.cachedArray = data.slice();
  }
  ....
}
static ApplyFilter()
{
  return myClass.mainArray.filter(myFilter); // used from my first post
}

static RevertFilter()
{
  myClass.mainArray = myClass.cachedArray.slice();
  return myClass.mainArray;
}
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
a_bertrand
New Worlds Engine creator
 
Posts: 1445
Joined: Mon Feb 25, 2013 8:46 am
Has thanked: 15 times
Been thanked: 20 times


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x