javascript - 是否可以在Android中编写一个生成两个数组的函数

标签 javascript arrays jquery-ui

使用JQuery自动完成我有一个函数可以生成 Array用户在框中输入的好友数量。

该数组名为 selected_Id 。然而,我们的项目已经扩展,并随着“ friend ”选择 (<--the one that produces the selected_Id array) 一起扩展。

我们想添加一个“敌人”功能。

是否可以重写该代码片段以生成 2 个单独的数组?如果我将它们分开,它将调用 getfriends 函数两次 (the getfriends function produces an array of available friends that loads into the jQuery ui that the user gets to choose from)

这是我所拥有的:

  $(function() {
    var available = new Array();
    available = getFriends();
    available = my_friends
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }

    $( "#tags" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            minLength: 0,
            source: function( request, response ) {
                // delegate back to autocomplete, but extract the last term
                response( $.ui.autocomplete.filter(
                    available, extractLast( request.term ) ) );
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                //replaced value with label to continue showing the names selected  
                terms.push( ui.item.label);
                //ui.item.valuethis is where my 
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;


            }
        });
            $( "button" ).button();
        $( "button" ).click(function() {
        var selected_Text = $( "#tags" ).val();
        var selected_Friends = selected_Text.split(","); 
        //friend name put in the autocomplete box
        var friend_Name = "";
        //selected id of the person in the array 
                var testCount = 0; 
        //i < j --> run through interation once 
        for(var i = 0, j=selected_Friends.length; i<j; i++ )
        {
            friend_Name = selected_Friends[i];
            while (friend_Name[0] == " "){
                friend_Name = friend_Name.substring(1,friend_Name.length);
            }


            for (var k = 0, l = my_friends.length; k<l; k++)
            {

                if (friend_Name == my_friends[k]["label"])
                {
                    selected_Id.push(my_friends[k]["value"])

                    testCount++;

                } else {

                } 
            }
        }

        return false; 


    });
 });

最佳答案

您可以返回数组对象:

function someFunction() {
    return {
        enemies: [1, 2, 3],
        friends: [4, 5, 6]
    };
}
console.log(someFunction().enemies) //The array of [1,2,3]

关于javascript - 是否可以在Android中编写一个生成两个数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602511/

相关文章:

javascript - 是否可以重置通话历史记录,但保留行为?

javascript - 悬停时特定于 jquery 的显示按钮

javascript - 为什么 jQuery 的 .data 方法会这样? (可能的错误?)

arrays - 可比较的扩展有效但结果不佳

python - Numpy 产生错误的值

javascript - 限制 jqueryui 可排序项目可以放置的区域

javascript - jQuery/js 使用多选小部件防止重复复选框出现

javascript - Protractor E2E测试错误: Object [object Object] has no method 'getWindowHandle'

Java:将字符串转换为日期

从 C 中现有的一维数组创建二维数组?