简体   繁体   中英

Not able to push elements from one array to another array in jquery

I have a line of code which is trying to push elements from one array to another array.

var criArray=[];
jQuery('form').find(':input[name=criteria]').toArray().each(
    function(val) {
        criArray.push(jQuery(val).val())
    }
);

When I load the page, in browser console, I am getting below error at this line.

Uncaught TypeError: undefined is not a function.

I am clueless of ways to push elements to array as I tried many answers from my google search. Can someone help me on this?

You can use .map() to do this

var criArray = jQuery('form').find(':input[name=criteria]').map(function () {
    return jQuery(this).val();
}).get();

The problem is .toArray() returns an array object which does not have the .each() method, you could use .forEach() instead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM