Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript split is not a function

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

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

like image 713
Code Prank Avatar asked May 14 '12 12:05

Code Prank


People also ask

Is there a split function in Javascript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is the opposite of Split in Javascript?

The split() method splits a String object into an array of string by separating the string into sub strings. The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first. The join() method joins all elements of an array into a string.

How do you separate text in Python?

Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


2 Answers

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

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)

like image 164
6502 Avatar answered Oct 02 '22 06:10

6502


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

var friendid='1,34,67';

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

like image 39
Anitha Avatar answered Oct 02 '22 06:10

Anitha