javascript如果不是空元素添加函数返回

时间:2017-03-17 11:00:27

标签: javascript angularjs return indexof angularjs-filter

我想要使用angularjs过滤mysql。

my fiddle

当您检查电视按钮并单击提交时。 (tv按钮值= 2)angularjs过滤器在设施栏中找到2。这是有效的。但是当电视和有线电视检查并点击提交过滤器不起作用时。因为我的过滤器代码很糟糕。



$scope.am_en = function()
{
$scope.ot_Filter = function (location) {
return location.amenities.indexOf(1)==0;
};
}




如何更改此行indexOf(1)

这得到一个值indexOf(1)我想要多个值indexOf(' 1,7,9,11')Indexof可能是另一种方法?

HTML



<input type="checkbox" name="more_filter[]" value="1" id="map-search-amenities-1" ng-checked="false">
<input type="checkbox" name="more_filter[]" value="2" id="map-search-amenities-2" ng-checked="false">
..vs
<button id="more_filter_submit" ng-click="am_en();">Submit</button>
&#13;
&#13;
&#13;

我尝试了这个但没有工作:

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

$scope.am_en = function(){
$scope.ot_Filter = function (location) {
var xC = $(".hosting_amenities input:checkbox:checked").map(function(){return $(this).val();}).get();
var xxc = "["+xC+"]";
var k1 = xxc.indexOf(1)!==-1;
var k2 = xxc.indexOf(2)!==-1;
var k3 = xxc.indexOf(3)!==-1;
...

var zz1 = location.amenities.indexOf(1)==0;
var zz2 = location.amenities.indexOf(2)==0;
var zz3 = location.amenities.indexOf(3)==0;
...

return zz1 || zz2 || zz3 || ...;
};
}
&#13;
&#13;
&#13;

说明

&#13;
&#13;
k1 == zz1

k2 == zz2
&#13;
&#13;
&#13;

我想: 如果k1等于true,则添加此短语return:|| ZZ1 如果元素存在,则添加return OR元素

例如:

&#13;
&#13;
if (k1 == true) ? "k1":""; 

if (k2 == true) ? "|| k2 ":"";  

if (k3 == true) ? "|| k3 ":"";

return k1 || k3;
&#13;
&#13;
&#13;

例2:

&#13;
&#13;
k1 == false; 

k2 == true; 

k3 == true;

k4 == true;

return k2 || k3 || k4;
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

根据您对我的重述的回复(是的,是的),请转到:

let rooms = [ 
 { name : 'Room1', amenities : '1,3' },
 { name : 'Room2', amenities : '2,4' },
 { name : 'Room3', amenities : '1,2,3' },
 { name : 'Room4', amenities : '2,3,4' }
 ];

 let amenities = [ 
    { id : '1', name : 'Amenity1' },
    { id : '2', name : 'Amenity2' },
    { id : '3', name : 'Amenity3' },
    { id : '4', name : 'Amenity4' },
    ];

 function getAmenities ( room ) {

    // Turn the room amenity ids into an array
    let ids_array = room.amenities.split ( ',' );

    // Filter the amenities table to get the array of amenities for this room
    return amenities.filter ( x => {
        return ( ids_array.indexOf ( x.id ) !== -1 );
    } );
 }

 // Show amenities for a given room
 rooms.forEach ( x => { console.log ( getAmenities ( x ) ) } )