Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 对象的AS3筛选器数组_Arrays_Flash_Actionscript 3 - Fatal编程技术网

Arrays 对象的AS3筛选器数组

Arrays 对象的AS3筛选器数组,arrays,flash,actionscript-3,Arrays,Flash,Actionscript 3,我有一个要根据某些搜索字符串筛选的对象数组。我想从原始数组创建一个新数组,该数组只包含属性等于搜索中字符串的对象: var _array = new Array(); _array.push({name:"Ben",Title:"Mr",location:"UK"}); _array.push({name:"Brian",Title:"Mr",location:"USA"}); _array.push({name:"Ben",Title:"Mr",locatio

我有一个要根据某些搜索字符串筛选的对象数组。我想从原始数组创建一个新数组,该数组只包含属性等于搜索中字符串的对象:

    var _array =  new Array();
    _array.push({name:"Ben",Title:"Mr",location:"UK"});
    _array.push({name:"Brian",Title:"Mr",location:"USA"});
    _array.push({name:"Ben",Title:"Mr",location:"USA"});

    var searchQuery:Array = new Array();
    searchQuery.push("Ben");
    searchQuery.push("Mr");
我希望新数组包含第一个和最后一个对象,因为它们都包含字符串“Ben”和“Mr”

我可以使用Array.filter来实现这一点吗

感谢您的帮助。

这是我的方法:

var _array:Array =  new Array();
_array.push({name:"Ben",Title:"Mr",location:"UK"});
_array.push({name:"Brian",Title:"Mr",location:"USA"});
_array.push({name:"Ben",Title:"Mr",location:"USA"});

var searchQuery:Array = new Array();
searchQuery.push("Ben");
searchQuery.push("Mr");

var resultArray:Array = _array.filter(ff); //The result.

function ff(el:*,ind:int,arr:Array){//Filter Function
    for(var i:int=0;i<searchQuery.length;i++){//Everything in searchQuery array should in el object.
        var b:Boolean = false;
        for(var s:String in el){
            if(el[s]==searchQuery[i]){
                b=true; break;
            }
        }
        if(!b) return false; //no searchQuery[i] in el... :(
    }
    return true;
}
var\u数组:数组=新数组();
_array.push({name:“Ben”,Title:“Mr”,location:“UK”});
_array.push({name:“Brian”,Title:“Mr”,location:“USA”});
_array.push({name:“Ben”,Title:“Mr”,location:“USA”});
var searchQuery:Array=new Array();
searchQuery.push(“本”);
searchQuery.push(“Mr”);
var resultArray:Array=\u Array.filter(ff)//结果呢。
函数ff(el:*,ind:int,arr:Array){//Filter函数
对于(var i:int=0;i这是我的方法:

var _array:Array =  new Array();
_array.push({name:"Ben",Title:"Mr",location:"UK"});
_array.push({name:"Brian",Title:"Mr",location:"USA"});
_array.push({name:"Ben",Title:"Mr",location:"USA"});

var searchQuery:Array = new Array();
searchQuery.push("Ben");
searchQuery.push("Mr");

var resultArray:Array = _array.filter(ff); //The result.

function ff(el:*,ind:int,arr:Array){//Filter Function
    for(var i:int=0;i<searchQuery.length;i++){//Everything in searchQuery array should in el object.
        var b:Boolean = false;
        for(var s:String in el){
            if(el[s]==searchQuery[i]){
                b=true; break;
            }
        }
        if(!b) return false; //no searchQuery[i] in el... :(
    }
    return true;
}
var\u数组:数组=新数组();
_array.push({name:“Ben”,Title:“Mr”,location:“UK”});
_array.push({name:“Brian”,Title:“Mr”,location:“USA”});
_array.push({name:“Ben”,Title:“Mr”,location:“USA”});
var searchQuery:Array=new Array();
searchQuery.push(“本”);
searchQuery.push(“Mr”);
var resultArray:Array=_Array.filter(ff);//结果。
函数ff(el:*,ind:int,arr:Array){//Filter函数

对于(var i:int=0;iAh没有什么比一个好的旧集合问题更合适的了:)

尽管JiminP的答案确实正确;但它存在一些性能问题;其中最大的问题就是性能问题,因此如果在大型阵列上搜索,操作可能会运行缓慢

下面的函数不是很干净,但是在大型阵列上会有更好的性能

var _array : Array = [];
_array.push({name:"Ben", Title:"Mr", location:"UK"});
_array.push({name:"Brian", Title:"Mr", location:"USA"});
_array.push({name:"Ben", Title:"Mr", location:"USA"});

// I presumed you would want a little bit more control over the search matching; by
// using a Map you can ensure that no-one with the (somewhat unlikley) name of "Mr"
// gets matched by mistake.
var searchQueryMap : Dictionary = new Dictionary();
searchQueryMap["name"] = "Ben";
searchQueryMap["Title"] = "Mr";

const results : Array = [];

// Loop over earch objectin the 'haystack' that we wish to search.
for each (var object : Object in _array) 
{
    // This variable is used to break out of the loop if the current object doesn't match the
    // searchQueryMap; this gets reset to true for each loop of the supplied array.
    var match : Boolean = true;

    // Loop over each key (property) in the searchQueryMap.
    for (var key : * in searchQueryMap) 
    {
        if (searchQueryMap[key] !== object[key]) 
        {
            // No match, we can break out of looping over the searchQueryMap here.
            match = false;
            break;          
        }
    }

    // Check to see if we still have a positive match; if we do, push it onto the results Array.
    if (match) {
        results.push(object);   
    }
}

// Debug the results.
trace("Matches:");
for each (var result : Object in results)
{
    for (var prop : * in result) {
        trace("\t" + prop + " => " + result[prop]);
    }
    trace("---");
}

啊,没有什么比得上一个好的老问题:)

尽管JiminP的答案确实正确;但它存在一些性能问题;其中最大的问题就是性能问题,因此如果在大型阵列上搜索,操作可能会运行缓慢

下面的函数不是很干净,但是在大型阵列上会有更好的性能

var _array : Array = [];
_array.push({name:"Ben", Title:"Mr", location:"UK"});
_array.push({name:"Brian", Title:"Mr", location:"USA"});
_array.push({name:"Ben", Title:"Mr", location:"USA"});

// I presumed you would want a little bit more control over the search matching; by
// using a Map you can ensure that no-one with the (somewhat unlikley) name of "Mr"
// gets matched by mistake.
var searchQueryMap : Dictionary = new Dictionary();
searchQueryMap["name"] = "Ben";
searchQueryMap["Title"] = "Mr";

const results : Array = [];

// Loop over earch objectin the 'haystack' that we wish to search.
for each (var object : Object in _array) 
{
    // This variable is used to break out of the loop if the current object doesn't match the
    // searchQueryMap; this gets reset to true for each loop of the supplied array.
    var match : Boolean = true;

    // Loop over each key (property) in the searchQueryMap.
    for (var key : * in searchQueryMap) 
    {
        if (searchQueryMap[key] !== object[key]) 
        {
            // No match, we can break out of looping over the searchQueryMap here.
            match = false;
            break;          
        }
    }

    // Check to see if we still have a positive match; if we do, push it onto the results Array.
    if (match) {
        results.push(object);   
    }
}

// Debug the results.
trace("Matches:");
for each (var result : Object in results)
{
    for (var prop : * in result) {
        trace("\t" + prop + " => " + result[prop]);
    }
    trace("---");
}