Transforming null into array.length = 0 - arrays

Probably a noob Question: On my server, there is a filter which returns all available options if an array of the filter object has length.0 for certain options.
Now if say i have an input "All Options" from the user side, this option when clicked must somehow tell the array to have length.0, so the server returns all options .
I tried to set the Input to
<mat-option [value]= null >Alle</mat-option>
But that results in an array that has the value null instead of no length at all.
I also tried
If (data.array[1] == undefined){
data.array.length == 0
}
But that did not work

Leave the value to null and then do it like this:
if (!data.array) {
data.array = [];
}

Related

how to filter data from array and apply conditions angular8

I am getting below sample data from api dynamically.
Can anyone suggest how to apply condition based on data.
You need to check values in array
if (this.tagSchemaDetails.schema_context.providers.indexOf('All') < 0) {
request['schema_context'] = this.createConditionsData();
}
As your providers is array of string and you are just comparing that with string so in all cases it will validate the condition.
So you have to check the strings in the array and compare them with 'All' to properly check the condition.
So your code should look like this:
if(!this.tabSchemaDetails.schema_context.providers.includes('All')){
request['schema_context']=this.createConditionsData();
}
as others have said here, you have to check values in the "providers" array. since you just need to check provider != 'All' you can simply check for the first element in the array as below.
if (this.tagSchemaDetails.schema_context.providers[0] !== 'All') {
request['schema_context'] = this.createConditionsData();
}

AngularJS foreach - producing numerous null for unassigned values

I have a array variable viewedprofiles= []; initialized.
I'll be assigning the profiles that have been viewed to this viewedprofiles array. Now if I try to display it (By assigning it to a $scope), I get NULL for other values that have not got assigned or touched.
var viewedprofiles= [];
angular.forEach(profiles, function(value, key){
if(value.viewed== "yes") {
viewedprofiles[value.id] = TRUE;
}
});
The output of viewedprofiles is as follows
[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,TRUE]
Output explanation :
Since the 9th id's profile viewed value was yes, the output returned TRUE at the 9th element of the viewedprofiles array.
Nothing wrong actually.
But I was wondering as far as the above code, the id was TRUE for 9th element. What if the id was some large number say 15640, Will there be 15639 NULLs before TRUE? Am I doing anything wrong or is there another way to work this out?
I found the answer.
What I was trying to do was basically wrong at the assigning part. I should push the element instead of assigning.
The following worked.
angular.forEach(profiles, function(value, key){
if(value.viewed== "yes") {
viewedprofiles.push(value.id);
}
}, viewedprofiles);

Using .Filter When the Filter Criteria are in Pre-existing String (TypeScript/Node.js)

I'm trying to process an array of JSON objects that have various common attributes, filtering each array entry on one or more of those attributes.
Normally, I'd just do it something like this:
let filteredResultsArray = originalArray.filter((obj) => {
return obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10;
});
My problem is that the filter parameters (the part after "return" in the code above) are highly variable (and unpredictable) from run to run, so I can't hard-code them in the filter. I compute them on the fly and store the whole thing in a string in my code. For example, on one run it might be:
myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
I've tried doing this:
let filteredResultsArray = originalArray.filter((obj) => {
return myAttributeString;
});
That's failing to filter anything. Apparently .filter() is not properly interpreting what I've stored in myAttributeString as filter criteria.
I have a sneaking suspicion that eval(myAttributeString) might be one way to pull this off, but unfortunately I'm working on a team where we've got tslint set to disallow the use of eval(), so that's not an option.
Anybody have an idea how I can get this to work?
When you "compute them on the fly", instead of creating a string, create a callback function that you can then pass to filter. For example, instead of
const myAttributeString = "obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10";
do
const filterCallback = obj => obj.attribute1 <= 3 && obj.attribute2 > 0 && obj.attribute3 === 10
Then, later, when the appropriate time comes to .filter, simply pass that as the callback:
const filteredResultsArray = originalArray.filter(filterCallback);
If you can't pass functions around, another option would be to build an array of conditions, for example
[
{
prop: "attribute1",
constraint: "<=",
value: 3
},
{
prop: "attribute2",
constraint: ">",
value: 0
},
// ...
]
and then turn the object into the filter function needed.
****************************************UPDATE******************************
As I suspected, eval() did work, but since I can't use it in my delivered code, and thanks to CertainPerformance's suggestion (which put my thinking on the right track) as well as the Node.js documentation site (via a lucky Google search), I was able to find a workaround using the vm module:
import * as vm from "vm";
let filteredResultsArray = originalArray.filter(
vm.runInThisContext("(obj) => {
return " + myAttributeString + ";}"));
Case closed.

Apply Filter to Column with Numeric Values

I have a solution for filtering on this question.
This works perfectly with a column that has string values. When I try to filter a column with numeric values it will not work. I'm assuming it is because .setHiddenValues() will not accept numeric values. I could be wrong.
Let me explain my scenario:
The user inputs a value on an HTML interface, let's say 6634.
The HTML calls my function on .gs and passes the numeric value the user inputted.
google.script.run //Executes an Apps Script JS Function
.withSuccessHandler(updateStatus) //function to be called upon successfull completion of Apps Script function
.withFailureHandler(failStatus)
.withUserObject(button) //To pass the event element object
.projectSearch2(projectID); //Apps Script JS Function
return;
The function (on the linked question above) will take that value and bump it up against the values in a column deleting the value if it is found. What I am left with is an array of values that I do not want filtered.
function projectSearch2(projectID){
var ss = SpreadsheetApp.getActive();
var monthlyDetailSht = ss.getSheetByName('Data Sheet');
var monLastCN = monthlyDetailSht.getLastColumn();
var monLastRN = monthlyDetailSht.getLastRow();
var data = monthlyDetailSht.getRange(1,1,1,monLastCN).getValues();//Get 2D array of all values in row one
var data = data[0];//Get the first and only inner array
var projectIDCN = data.indexOf('Project Id') + 1;
//Pull data from columns before filtering
var projectIDData = monthlyDetailSht.getRange(2,projectIDCN,monLastRN,1).getValues();
//Reset filters if filters exist
if(monthlyDetailSht.getFilter() != null){monthlyDetailSht.getFilter().remove();}
//Start Filtering
var projectIDExclCriteria = getHiddenValueArray(projectTypeData,projectID); //get values except for
var rang = monthlyDetailSht.getDataRange();
var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues(projectIDExclCriteria).build();//Create criteria with values you do not want included.
var filter = rang.getFilter() || rang.createFilter();// getFilter already available or create a new one
if(projectID != '' && projectID != null){
filter.setColumnFilterCriteria(projectIDCN, projectIDFilter);
}
};
function getHiddenValueArray(colValueArr,visibleValueArr){
var flatUniqArr = colValueArr.map(function(e){return e[0];})
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
return flatUniqArr;
}
That array is used in .setHiddenValues() to filter on the column.
Nothing is filtered however. This works for all columns that contain string values, just not columns with numeric values. At this point I'm lost.
Attempted Solutions:
Convert user variable to string using input = input.toString(). Did not work.
manually inserted .setHiddenValues for projectIDExclCriteria. Like this: var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues([1041,1070,1071,1072]).build(); That succeeded so I know the issue is before that.
Step before was calling getHiddenValueArray. I manually inserted like so: var projectIDExclCriteria = getHiddenValueArray(projectIDData,[6634]); It is not working. Is the issue with that getHiddenValueArray function not handling the numbers properly?
Here is a solution. Changing the following:
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
To:
.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); })
That works! Thank you Tanaike. The next question is will this impact columns that are not numeric. I have tested that and it works as well.
How about this modification?
From :
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
To :
.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); })
Note :
In this modification, the number and string can compared using each value.
If you want to use return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1), you can achieve it by modifying from colValueArr.map(function(e){return e[0];}) to colValueArr.map(function(e){return e[0].toString();}).
In this modification, colValueArr.map(function(e){return e[0].toString();}) converts the number to string, so the number is used as a string.
Reference :
Array.prototype.indexOf()

Extjs4, filter store dynamically

I fill out an array dynamically with a list of Ids:
var allChildNodeIDs = [];
Ext.getCmp('categoriesTreePanel').getRootNode().eachChild(function(Mynode){
allChildNodeIDs.push(Mynode.data.idCategorie);
});
Then I want to filter a store according to all Ids contained in the array. For example if the array contains two values, I want that my store will be filtered like so:
myStore.filterBy(function(record) {
return (record.get('idCategorie') == allChildNodeIDs [1] && record.get('idCategorie') == allChildNodeIDs [2]);
});
But the array is filled out dynamically, and I don't know his length!
It seems like the sorting function in your example would always return false. I assume what you want to do is accept all the record which category is present into the array (i.e. that ou meant to use || instead of &&).
You can use Array's indexOf method.
Example:
myStore.filterBy(function(record) {
return allChildNodeIDs.indexOf(record.get('idCategorie')) !== -1;
});

Resources