locked
Add Commas to Appended Values RRS feed

Answers

All replies

  • I have also logged the array out to a console, but it doesn't separate the values. Could it be because I am appending HTML?

    Personal Blog: http://thebitsthatbyte.com

    Saturday, February 8, 2014 4:45 AM
  • you can use for loop

    var str="";
    for(i=0;i<results.length-1;i++){
    //add comma to your
    str=str+results[i]+",";
    }
    
    str=str+results[i]; //no comma
    
    



    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

    Sunday, February 9, 2014 5:53 PM
  • Where would that live in the following code? If I have in the last $.each, it doesn't work, if I place it within the first $.each it creates doubles because of the looping.

    1.function yourcompanyNews()
    {
        var basePath = "/sites/resourcecenter/news/_api/";
        $.ajax({
            async: false,
            url: basePath + "web/lists/GetByTitle('Posts')/items?$filter=Placement eq 'News'&$top=8&$orderby=NewsOrder asc",
            type: "GET",
            headers: { "Accept": "application/json;odata=verbose" },
            success: function (data)
            {
                //script to build UI HERE
                $.each(data.d.results, function (index, value)
                {
                    // Need to create DIV stored value to avoid global variable
                    var blogPostID = value.ID;
                    var categoryValue = value.CategoryValues;
                    var categoryValueArray = categoryValue.split(',');
    2.                $("#YourCompanNews").append('<img src="' + value.Thumbnail.Url + '" /><a href="/sites/resourcecenter/news/Lists/Posts/Post.aspx?ID=' + blogPostID + '">' + value.Title + '</a></br><p id="BlogPost' + blogPostID + '"></p>');
    3.                $.each(categoryValueArray, function (index, item) {
                        $.ajax({
                            async: false,
                            url: "https://yourcompany.com/sites/resourcecenter/news/_api/web/lists/GetByTitle('Categories')/items?$filter=ID eq " + item,
                            type: "GET",
                            headers: { "Accept": "application/json;odata=verbose" },
                            success: function (categories) {
                                //script to build UI HERE
                                var categoryValuesCommas = []
    4.                            $.each(categories.d.results, function (index, value) {
                                    categoryValuesCommas.push('<a href="/sites/resourcecenter/news/Lists/Categories/Category.aspx?CategoryId=' + value.ID + '">' + value.Title + '</a>');
                                    
                                });
                                console.log(categoryValuesCommas.join(" , ")) // is this code running ??
                                $('#BlogPost' + blogPostID).append(categoryValuesCommas.join(","))
                            },
                        });
                    });
                });
            },
            error: function (data)
            {
                //output error HERE
                alert(data.statusText);
            },
            error: function (data) {
                //output error HERE
                alert(data.statusText);
            },
            });
    }
    


    Personal Blog: http://thebitsthatbyte.com

    Monday, February 10, 2014 3:08 PM
  • how about removing the last character of your joint string 

    var str=categoryValuesCommas.join(",");
    str=substring(0,str.length-1)
    $('#BlogPost' + blogPostID).append(str);

    I tried the above two lines for 

    str="1,2,3,4,5,"

    the updated value "1,2,3,4,5"


    Hope that helps|Amr Fouad|MCTS,MCPD sharePoint 2010

    Wednesday, February 12, 2014 6:40 AM
  • Thanks, but I got it working. I had placed the create array within the second loop. You can see the answer here:

    http://forum.jquery.com/topic/add-commas-between-each-item-outputted-in-each-loop-but-not-at-the-end-of-the-last#14737000005055951


    Personal Blog: http://thebitsthatbyte.com

    Wednesday, February 12, 2014 2:05 PM