Javascript split 不是一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10583055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:58:16  来源:igfitidea点击:

Javascript split is not a function

javascriptjqueryfacebookstringsplit

提问by Code Prank

Hey friends i am using javascript sdk to post on users friends wall with jQuery facebook multi friend selector however i am getting this error friendId.split is not a function. Here is my code

嘿朋友们,我正在使用 javascript sdk 通过 jQuery facebook 多朋友选择器在用户朋友墙上张贴,但是我收到此错误friendId.split is not a function。这是我的代码

function recommendToFriend(pic, url, friendId, fromName)
{
    alert(friendId);
    var friendList ;
    pFriend = new Array();
    pFriend = friendId.split(',');
    for( x in pFriend )
    {
        alert(pFriend[x]);
        var publish = {
            method:'feed',
            picture:pic,
            link:url,
            name:'SHARP Product Recommend',
            caption: fromName + 'has recommend a product to you via Sharp Expert lounge',
        };

        FB.api('/'+pFriend[x]+'/feed', 'post', publish, function(resp) {
            if( !response || response.error )
                alert('Unable to share');
            else
                alert('Successfully posted to firends wall');
        });
    }
}

In alert box i got comma seperated friend ids so i use split function post on each users wall seperately i dont know whats wrong here please help me

在警告框中,我有逗号分隔的朋友 ID,所以我在每个用户墙上单独使用拆分功能帖子我不知道这里出了什么问题,请帮助我

采纳答案by Junaid

You can traverse JS Objects like this

你可以像这样遍历 JS 对象

for (var key in friendid) {
   var obj = friendid[key];
   for (var prop in obj) {
      alert(prop + " = " + obj[prop]);
   }
}

Hope this helps

希望这可以帮助

alternate

备用

for( var x in friendId )
 {
  alert(friendId[x]); // this would be your desired value
 }

回答by 6502

Most probably friendIDis already an array. If you call alertthe array is converted to a string and becomes a comma separated list of values.

很可能friendID已经是一个数组。如果调用alert该数组将转换为字符串并成为逗号分隔的值列表。

Note that converting an array to a string is not the same as calling JSON.stringify(where you get also brackets and double quotes around elements when they're strings)

请注意,将数组转换为字符串与调用不同JSON.stringify(当它们是字符串时,您还会在元素周围获得括号和双引号)

回答by Anitha

The JavaScript split()function is for the type string ie for eg.

JavaScriptsplit()函数用于字符串类型,即例如。

var friendid='1,34,67';

As VisioN says, when you alert an array you get comma separated values.

正如 VisioN 所说,当你提醒一个数组时,你会得到逗号分隔的值。