advanced filter array angularjs - arrays

I'm trying to perform an advanced search with an angular filter. The idea of the filter is to receive two values (maximum and minimum) in an array and copy all intervals that are within the maximum and minimum of another array.
example:
array = {20,32,10, 60, 75, 43, 95}
minimum: 50
maximum: 100
resultant vector = {60, 95} 75.
code:
for (var i = 0; i < products.length; i++) {
if (product[i].precio >= $scope.minimo && product[i].precio <= $scope.maximo)
return this.products[i];
}
return null;
};

Something like:
angular.filter('someFilter', function() {
var newArray = [];
return function(products, min, max) {
for(var i = 0; i < products.length, i++) {
if (products[i].precio >= min && products[i].precio <= max) {
newArray.push(products[i]);
}
}
return newArray;
};
});
And use it like: <... ng-repeat="products | someFilter:min:max" ...>

Related

how do I find an object's index number in an array

I've got this code that is looking for the closest number to 0 in my array :
var tideArray = new Array();
tideArray.push({tide:"haute", difference: "-14"});
tideArray.push({tide:"haute", difference: "3"});
tideArray.push({tide:"basse", difference: "-4"});
tideArray.push({tide:"basse", difference: "8"});
if (tideArray.length > 0)
{
var minItem: Object = tideArray[0];
for (var index:int = 1; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
}
}
}
trace(minItem.difference) // OUTPUT is 3 in this case
s there a way to find the index of minItem.difference in my tideArray ? (so, the result here should be index = 1 )
I've tried tideArray.indexOf(minItem.difference) but the output is -1, so the index wasn't found...
I'm looking for the index number and not the value of "difference" or "tide".
Try using map, e.g.:
tideArray.map(function (cv) { return cv.difference }).indexOf("-14")
A really simple approach would be to record the index using your existing loop:
if (tideArray.length > 0)
{
var mindex: int = NaN;
var minItem: Object = tideArray[0];
for (var index:int = 0; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
mindex = index;
}
}
trace("Min item index: " + mindex);
}

Loop won't break in javascript

So I am trying to create a function where it will display to me the FIRST even number divisible by 2. The values to be divided are inside an array and another function helps me determine whether the values in the array are divisible by 2. The problem is that the loop won't break and the loop continues until the last value of the array. I want it to break once it finds the first number divisible by 2. So in this case the loop should stop once it reaches value 8 in the array but it doesn't and continues until 10. I hope you can help me
This is my code:
function findElement(arr, func) {
var num = 0;
arr.sort();
for(var i = arr[0]; i <= arr[arr.length-1]; i++) {
if(func(arr[i])) {
num = arr[i];
break;
}
if(!func(arr[i])) {
num = undefined;
}
}
return num;
}
findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; });
I believe your for over array is off.
Instead of
for(var i = arr[0]; i <= arr[arr.length-1]; i++) {
It should be
for(var i = 0; i <= arr.length-1; i++) {
Otherwise, you might as well be verifying undefined array indexes.
Please remove arr.sort() your function works find
please find the updated code .its working fine run and check.
function findElement(arr, func) {
var num = 0;
// arr.sort();
for(var i = arr[0]; i <= arr[arr.length-1]; i++) {
if(func(arr[i])) {
num = arr[i];
break;
}
if(!func(arr[i])) {
num = undefined;
}
}
return num;
}
console.log(findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; }));

Multidimensional Arrays and one of the fields

There is a multi-d array and I want to reach specific field in it. I have look around it but I was unable to find proper answer to my question.
My array is like that;
array-md
columns-- 0 | 1 | 2
index 0 - [1][John][Doe]
index 1 - [2][Sue][Allen]
index 2 - [3][Luiz][Guzman]
.
.
.
index n - [n+1][George][Smith]
My question is how can I reach only second column of the array? I tried name = array[loop][1]; but it says "Cannot access a property or method of a null object reference". What is the right way to do that?
Here is main part of the code.
get
var lpx:int;
var lpxi:int;
var arrLen:int = Info.endPageArray.length;
for(lpx = 0; lpx < arrLen; lpx++)
{
for(lpxi = Info.endPageArray[lpx][2]; lpxi < Info.endPageArray[lpx][1]; lpxi++)
{
if(Info._intervalSearch[lpxi] == "completed")
{
successCount++;
Info._unitIntervalSuccess.push([lpx, successCount / (Info._intervalSearch.length / 100)]);
}
}
}
set
for(lpix = 0; lpix < arrayLength; lpix++)
{
if(lpix + 1 <= arrayLength)
{
Info.endPageArray.push([lpix, Info._UnitsTriggers[lpix + 1], Info._UnitsTriggers[lpix]]);
}
else
{
Info.endPageArray.push([lpix, Info._UnitsTriggers[lpix], Info._UnitsTriggers[lpix - 1]]);
}
}
Try this:
var tempArr:Array = [];
function pushItem(itemName:String, itemSurname:String):void
{
var tempIndex:int = tempArr.length;
tempArr[tempIndex] = {};
tempArr[tempIndex][tempIndex + 1] = {};
tempArr[tempIndex][tempIndex + 1][name] = {};
tempArr[tempIndex][tempIndex + 1][name][itemSurname] = {};
}
function getNameObject(index:int):Object
{
var result:Object;
if(index < tempArr.length)
{
result = tempArr[index][index + 1];
}
return result;
}
pushItem("Max", "Payne");
pushItem("Lara", "Croft");
pushItem("Dart", "Vader");
//
trace(getNameObject(0));
trace(getNameObject(1));
trace(getNameObject(2));
Multidimensional array is an array of arrays, which you can create like this :
var persons:Array = [
['John', 'Doe'],
['Sue', 'Allen'],
['Luiz','Guzman']
];
var list:Array = [];
for(var i:int = 0; i < persons.length; i++)
{
list.push([i + 1, persons[i][0], persons[i][1]]);
}
trace(list);
// gives :
//
// 1, John, Doe
// 2, Sue, Allen
// 3, Luiz, Guzman
Then to get some data :
for(var j:int = 0; j < list.length; j++)
{
trace(list[j][1]); // gives for the 2nd line : Sue
}
For more about multidimensional arrays take a look here.
Hope that can help.

Using pop function in multidimensional array AS3

What I'd like to be able to do is make my function find the index of an array in my array list and delete it. I'd like it to find the first and last name of a person stored inside of an array stored in in it's two first slots (0 and 1) (ex: example[0]=["Robert", "Brown", 1000, 2100, 600, 400];) and delete it (the whole element[0]) from the list of my array. This is the list that I have right now:
voyageur[0]=["Roger", "Dagenais", 1000, 2100, 600, 400];
voyageur[1]=["Phil", "Thomas", 200, 300, 1400, 800];
voyageur[2]=["Nikolas", "Brideau", 1000, 2000, 3000, 5000];
Now let's say I want to delete Phil Thomas (element #1) from my array. How would I do it?
function supprimer(event:MouseEvent):void {
var indiceVoyageur:int;
var indiceVoyageurPrenom:int;
var indiceVoyageurNom:int;
if ( (boitePrenom.text != "") && (boiteNom.text != "") )
{
for (var rang:int = 0; rang < voyageur.length; rang++)
{
indiceVoyageurPrenom = (voyageur[rang][0].indexOf(boitePrenom.text));
indiceVoyageurNom = (voyageur[rang][1].indexOf(boiteNom.text));
if (indiceVoyageurPrenom != -1)
{
for (var i=indiceVoyageur; i <voyageur.length; i++)
{
voyageur[i] = voyageur[i+1];
}
voyageur.pop();
}
}
}
}
This is some code that I had made previously but it doesn't really work properly. If you have another way of doing what it is that I want to do, that would also be fine.
Any help is appreciated.
Instead of shuffling and popping, you can just use splice:
function supprimer(): void {
var indiceVoyageur: int;
var indiceVoyageurPrenom: int;
var indiceVoyageurNom: int;
if ((boitePrenom.text != "") && (boiteNom.text != "")) {
for (var rang: int = 0; rang < voyageur.length; rang++) {
indiceVoyageurPrenom = (voyageur[rang][0].indexOf(boitePrenom.text));
indiceVoyageurNom = (voyageur[rang][1].indexOf(boiteNom.text));
if (indiceVoyageurPrenom != -1 && indiceVoyageurNom != -1) {
voyageur.splice(rang, 1);
break;
}
}
}
}
I cannot understand exactly your code as it seems to be with some strange variables.. But this should work:
function clear(firstName:String, lastName:String):Boolean {
var total:uint = _users.length;
// _users is a member variable of the users array
// you can pass it to the function if you don't like it to be member
var found:Boolean;
for (var i:uint = 0; i < total; i++) {
var data:Object = _users[i];
if (data.indexOf(firstName) != -1 && data.indexOf(lastName) != -1) {
data.splice(i, 1);
i--; // keep searching for other items in the array
found = true;
}
}
return found; // return true if it finds it
}
this will satisfy your need as to delete those elements and returns the output. no need of looping. directly use array methods max for optimization.
var voyageur1:Array=["Roger", "Dagenais", 1000, 2100, 600, 400];
var voyageur2:Array=["Phil", "Thomas", 200, 300, 1400, 800];
var voyageur3:Array=["Nikolas", "Brideau", 1000, 2000, 3000, 5000];
var voyageur4:Array=voyageur2.splice(0,2);
trace(voyageur4); //Phil,Thomas
trace(voyageur2); //200,300,1400,800[after deleting].
hope you expected this.

Avoid duplicates in a repeater are not allowed

Is there any built-in feature on AngularJS to avoid ng-repeater to receive duplicated entries?
Right now I'm using the following code to prevent it:
$scope.tags = ['black','white','red','yellow','blue'];
$scope.selectedTags = [];
// textarea value
var words = $scope.message.split(' ');
for(var j = 0; j < words.length; j++) {
for (var k = 0; k < $scope.selectedTags.length; k++) {
if ($scope.selectedTags[k].Name == words[j]) {
contains = true;
}
}
if (!contains)
{
$scope.selectedTags.push($scope.tags[i]);
contains = false;
}
}
Angular UI has a unique filter:
Filters out all duplicate items from an array by checking the specified key
Alternatively, if it's just a string array you can filter your array like:
arr.filter(function (e, i, arr) {
return arr.lastIndexOf(e) === i;
});

Resources