user2818060
user2818060

Reputation: 845

Comma separated string not working

problem

input data:

9037566,

9037579,

9037577,

9030724,

9019686,

but output data should be like for example

1,2,3,4,5

json data as input

{ "keyword_search":
  {"data":
    [
      {"JOB_NO":"9037566"},
      {"JOB_NO":"9037579"},
      {"JOB_NO":"9037577"},
       {"JOB_NO":"9030724"},
           {"JOB_NO":"9019686"}
         ]}}

js code i tried with

           $.ajax({
           url: root_url + 'mylogs/keyword',
           cache: false,
           type: "POST",
           data: {'data': keyword},
           dataType: 'json',
           success: function (html) {
                var keyword=html.keyword_search.data;
                $.each(keyword, function (key, value){
                    console.log(value.JOB_NO.split(',')+',');

Upvotes: 1

Views: 188

Answers (1)

Curtis
Curtis

Reputation: 103348

You are calling console.log() for each iteration of the loop, and therefore you will get a seperate output for each piece of data.

Change the following:

       success: function (html) {
            var keyword=html.keyword_search.data;
            $.each(keyword, function (key, value){
                console.log(value.JOB_NO.split(',')+',');

To:

       success: function (html) {
            var keyword=html.keyword_search.data;
            var jobNos = keyword.map(function(i){
                 return i.JOB_NO;
            });
            console.log(jobNos.join(","));

Simplified example here: https://jsfiddle.net/Lscewr1v/

Upvotes: 2

Related Questions