Javascript 将函数结果添加到数组中

Javascript 将函数结果添加到数组中,javascript,jquery,Javascript,Jquery,我有许多输入框,我正试图将的名称存储到一个数组中。我目前正在使用它来获取名称: var getImplementedNames = function (selector){ $(selector).each(function() { console.log($( this ).attr('name').replace('imp-', '')); }); } console.log(getImplementedNames('[id^=imp]')); 这是

我有许多输入框,我正试图将的名称存储到一个数组中。我目前正在使用它来获取名称:

var getImplementedNames = function (selector){
    $(selector).each(function() {
        console.log($( this ).attr('name').replace('imp-', ''));
    });
}   

console.log(getImplementedNames('[id^=imp]'));
这是可行的,但现在我想将所有reslt添加到一个数组中。我试过了

var array = [getImplementedNames('[id^=imp]')];

console.log(array);
返回一个未定义的数组

我不知道该如何妥善处理这件事

使用

用法


阅读

您的函数当前没有返回任何内容。尝试:

var getImplementedNames = function (selector){
    return $(selector).map(function() {
        return $( this ).attr('name').replace('imp-', '');
    });
}   

console.log(getImplementedNames('[id^=imp]'));

@MichaelN欢迎您乐于帮助:)
console.log(getImplementedNames('[id^=imp]'));
var getImplementedNames = function (selector){
    return $(selector).map(function() {
        return $( this ).attr('name').replace('imp-', '');
    });
}   

console.log(getImplementedNames('[id^=imp]'));