JavaScript相当于PHP函数:array_flip [英] JavaScript equivalent of PHP function: array_flip

查看:96
本文介绍了JavaScript相当于PHP函数:array_flip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何捷径可以完成相当于 PHP的array_flip功能在JavaScript中还是必须通过暴力循环来完成?

Is there any shortcut to accomplishing the equivalent of PHP's array_flip function in JavaScript or does it have to be done via brute force looping?

它必须用于几十个阵列,所以即使很小的加速也可能加起来。

It has to be used for dozens of arrays so even small speedups will probably add up.

推荐答案

不要认为内置了一个。示例实现此处,虽然:)

Don't think there's one built in. Example implementation here, though :).

function array_flip( trans )
{
    var key, tmp_ar = {};

    for ( key in trans )
    {
        if ( trans.hasOwnProperty( key ) )
        {
            tmp_ar[trans[key]] = key;
        }
    }

    return tmp_ar;
}

这篇关于JavaScript相当于PHP函数:array_flip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆