按索引返回数组值,如果给定else,则返回数组

return array value by index if given else return the array

本文关键字:数组 返回 else 如果 索引      更新时间:2023-09-26

如果给定索引号,是否可以通过其索引返回数组值,否则是否可以返回完整的数组,而不重复?

例如,我们有以下功能:

$.fn.getMatrix = function(i){
    return this.css('transform').split('(')[1].split(')')[0].split(',')[i];
};

如果i-参数未设置,我想返回完整的数组。

有什么想法可以在遵守DRY的前提下实现这一点吗?

类似的东西?

$.fn.getMatrix = function(i){
   var array = this.css('transform').split('(')[1].split(')')[0].split(',');
   return (typeof i === "number") ? array[i] : array;
};