简体   繁体   English

无法将元素从一个数组推送到jQuery中的另一个数组

[英]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 您可以使用.map()执行此操作

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 问题是.toArray()返回一个没有.each()方法的数组对象,可以改用.forEach()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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