简体   繁体   English

如何为.push函数制作数组。 收到“ typeError:.push不是函数”?

[英]How to make array for a .push function. Getting “typeError: .push is not a function”?

I'm having trouble understanding how my code is resulting in an error. 我无法理解我的代码是如何导致错误的。 The line that gives off an error is 发出错误的行是

queryParts.push("&" + param + "=");

I can see that it inserts an array, but I don't understand how to make it an arrow and push. 我可以看到它插入了一个数组,但是我不明白如何使它成为箭头并推动。

function urlBuilder(param, paramValue){
                base_url = $('.service-container').data('base_url');
                var fullurl = window.location.href;
                var urlStart = fullurl.split("?").length > 1 ? fullurl.split("?")[0] : null;
                var urlQuery = fullurl.split("?").length > 1 ? fullurl.split("?")[1] : null;

                if (urlQuery) {
                    var queryParts = urlQuery.split('&');
                    for (var i = 0; i < queryParts.length; i++)
                    {
                        console.debug('query part: ', queryParts[i]);
                        if (queryParts[i].includes(param)) {
                            if (paramValue === null) {
                                queryParts.splice(i, 1);
                                } else {
                                    queryParts = "?" + param + "=" + paramValue;
                                }
                            } else {
                            queryParts.push("&" + param + "=");
                            } 
                        }
                        var rtnUrl = urlStart + queryParts.join("&");
                    return rtnUrl;
                    }else {
                        return base_url + '?' + param + '=' + paramValue;
                    }
            }

You do 你做

queryParts = "?" + param + "=" + paramValue;

After this, queryParts isn't an array anymore. 之后, queryParts不再是数组。

You probably wanted 你可能想要

queryParts.push( "?" + param + "=" + paramValue);

This line makes your queryParts string. queryParts您的queryParts字符串。

queryParts = '?' + param + '=' + paramValue

To keep it array: 保持数组:

queryParts.push('?' + param + '=' + paramValue)

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

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