Filtering by value in array in object

Let's say I have an array of objects, something like:

$scope.data = [{name: 'foo name', value: 'foo value', spots: ['s1', 's2', 's3']}, {name: 'foo name 2', value: 'foo value 2', spots: ['s1', 's2', 's3', 's4'}];

I want to filter this data with respect to the value of the current "spot", that is selected by the user. I know I could just write something like:

<div ng-repeat="subItem in data | filter: 's' + currentSpot"> // Some content </div>

But in the real case the data in each item is much more complex and thus causes a delay when the DOM is updated. I would thus like to filter only by the 'spots' field. I've searched around but didn't find any good solution that optimizes this problem.

--------------Solutions-------------

As of Angular 1.1.3 or newer, you can do something like this

<input type="text" ng-model="search">
<div ng-repeat="item in data | filter: {'spots': search}">
//something
</div>

Where 'spots' is the key from item in data, on which you want to search.

See this fiddle: http://jsfiddle.net/0Lftnfqc/

You could do something like this:

filter: function (value) {
var deferred = $q.defer();
var matches = $scope.data.filter(function (datarow) {
if (value== "" || value== null) return false;
else if (datarow.spots.toLowerCase().indexOf(value.toLowerCase()) != -1) return true;
});
$timeout(function () {
deferred.resolve(matches);
}, 100);
return deferred.promise;
}

Really fast copy paste so you have to adapt it to your own situation. I suggest using 2 arrays, the full one and the filtered one you then reference in your ng-repeat.

Category:javascript Time:2018-12-27 Views:0

Related post

  • Display values of array of objects 2011-08-08

    How can I dialplay values of array of objects which is stored into session. I make like this //fetch array of countries from db $countries = array(); while($row = mysql_fetch_array($result)){ $country = new Country(); $country->setCountryName($row

  • How to convert values into array of object? 2012-02-01

    I have following values, firstsession secondsession thirdsession B+ i have to convert the above to values to array of object, stdClass Object ( [Itemname] => firstsession [Ability] => B+ ) stdClass Object ( [Itemname] => secondsession [Abili

  • How to get the largest value from array of objects in javascript, in the most elegant way? 2011-11-14

    [Object { curr="SEK", antal=7}, Object { curr="JPY", antal=1}, Object { curr="DKK", antal=1}] I could create a new array and sort it. But I want to sort the array of objects by the antal property. How to do that in pure javascript. No jquery solution

  • Test for value within array of objects 2012-03-13

    I am dynamically building an array of objects using a process that boils down to something like this: //Objects Array var objects = []; //Object Structure var object1 = {"id":"foobar_1", "metrics":90}; var object2 = {"id":"some other foobar", "metric

  • Get minimum property value from array of objects 2012-04-07

    Apologies if this is a stupid question, I'm relatively new to Matlab. I've got an array of rectangles of class Rectangle with properties minx, miny, maxx, maxy, which represent the corner coordinates. I'm trying to get the index of the top-left recta

  • Sorting an Alphanumeric value in array of objects using javascript? 2014-03-19

    I am getting a string an array of objects field from the data base which contains a value like:dataarray=[{name:'xyz',id:' '},{name:'abc',id:'DOC-TEMP-1'},{name:'efg',id:''},{name:'abc',id:'DOC-TEMP-21'},{name:'abc',id:''},{name:'jklm',id:'DOC-TEMP-2

  • Find Missing Values in Array of Objects 2011-05-23

    I have a json array that has multiple missing numbers and is out of sequence. What is the best way to find which numbers are missing? My First thought was to iterate through and construct a new temporary array in order (so if the first key is 50, it

  • Create array of values from array of objects 2012-02-14

    Is there any tool to to create: [ 'John', 'Sam', 'Marry' ] from: [ { name: 'John' }, { name: 'Sam' }, { name: 'Marry' } ] ? --------------Solutions------------- Yeah, the map() method: var array = [{name: 'John'}, {name: 'Sam'}, {name: 'Mary'}].map(f

  • Accessing value from array of objects 2012-04-18

    I am having two arrays, Namely NMutableArray* first; NMutableArray* second; Now I am copying first object to the second array like for (int i=0;i<first.count; i++) { [second addObject:[first objectAtIndex:i]; } This is ok. I don't know how to acce

  • Pass values from array to object? - PHP 2010-07-23

    My problem, simplified is: class A { public $a; public $b; function f1 () { // Code } } $obj = new A(); $arr = array ("a" => 1, "b" => 2); How can I put the contents of $arr into $obj? (Obviously without $obj->a = $arr["a"], suppose there ar

  • Find value in Array in objective C 2011-07-12

    I have a NSArray structure likes this {w123,123,234,345,w124,234,235,w125,234,453,435,....} What I am trying to do is to specify which one begins with "w" then do something But I don't know how to do that in objective C, anyone can help? Thanks in ad

  • Sorting an array of an array of objects in PHP by key value 2009-01-05

    Basically I have a setup like the following: Array ( [0] => Array ( [0] => stdClass Object ( [nid] => 1 [title] => title1 [uid] => 1 [parent] => 0 [weight] => -15 [name] => name1 [value] => 0 ) [1] => stdClass Object ( [

  • Problem sorting an array of objects by date & time in Javascript - null values allowed 2010-07-31

    I'm working with an array of objects in Javascript and need to sort them by date and time. Here's the setup: place title date (optional) time (optional) Conceptually, the application allows users to create a list of places they're planning to go. The

  • How to get unique values based on dictionary key, which is an element of an array in objective C? 2010-09-27

    How to get unique values based on dictionary key, which is an element of an array in objective C ? for example: i am having an array of an Items aryItem aryItem[0] = Dictionary{ ItemCategory ItemName ItemPrice } aryItem[1] = Dictionary{ ItemCategory

  • PHP: Function to group array of objects by value 2010-12-01

    I am working with a multi-dimensional array. How can I remove duplicates by value? In the following array, [0], [2] and [5] have the same [ID]. Is there a function that will remove any duplicate arrays based on a particular value? In this case, I wou

  • PHP: Counting values in an array of objects 2011-03-01

    I have a databse class which returns an array of objects. I am trying to get multiple bits of data from this single query, in order to prevent having to run three queries. I have stumbled upon array_count_values() [ http://uk3.php.net/manual/en/funct

  • How to find a value in a multidimensional object/array in Javascript? 2011-03-03

    I have a multidimensional object (it's basically an array): Object = { 1 : { name : bob , dinner : pizza }, 2 : { name : john , dinner : sushi }, 3 : { name : larry, dinner : hummus } } I want to be able to search the object/array for where the key i

  • Linq to create a csv string for some values stored in an array of object using c# 2011-04-21

    I have an array of object storing some accounts. 12345631 L1234512 P12345 234556 19090912 J123456 Using Linq and .Net 3.5, I want to create a csv string for accounts starting with L or P and is of 6 characters. Following code creates a csv string but

  • To search a value and then delete object containing it from an array of objects? 2011-05-01

    #include <iostream> #include <vector> #include <algorithm> using namespace std; class A { public : void getData(vector< A > &); void putData(vector< A > &); private : int x; char name[90]; }; vector< A > v;

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.799 (s). 13 q(s)