2

I want to create an object in jquery using a json response that is return from my controller

var cellContents = {'29-08-2018': '20','18-08-2018': '60'};

This is the desired format that i want and below given is my json response

{"status":1,"result":[{"followup_datee":"17-08-2018","date":[{"fcount":"1"}]},{"followup_datee":"18-08-2018","date":[{"fcount":"1"}]}]}

i tried some code to make the format that i want but it failed this is the code that i tried

var arr2 = [];
      //var cellContents = JSON.parse(data.result);

      for(var i=0; i<data.result.length; i++){
        var arr = [];
        for(var j=0; j<data.result[i].date.length; j++)
        { 
        arr.push(parseInt(data.result[i].date[j].fcount));
        } 
        var test = [data.result[i].followup_datee];
        arr2.push(test.concat(arr));
        } 
        var jsonString = JSON.stringify(arr2);
      console.log(jsonString);

after i tried this code i got some response in the console like this

[["17-08-2018",1],["18-08-2018",1]]

This is the controller that i am using in php

 public function getLeadcount()
{
    $status = 1;
    $arr = [];
    $sess = $this->session->userdata('user_id');
    $result = $this->mdl_lead_registration->getLeaddate($sess);
    if(!empty($result))
    {
        $status = 1;
        foreach($result as $res)
        {
            $res->{'date'} = '';
            $res->date = $this->mdl_lead_registration->getLeadcount($sess,$res->followup_datee);
        }

    }

   echo json_encode(array("status" => $status,"result" => $result));
}

I think you all understand my problem . Any sort of help is appreciable.

10
  • Please give an example - what are you get from the server, what's the js result, and what's the result you want. Commented Aug 16, 2018 at 7:25
  • I want result like this {'17-08-2018': '1','18-08-2018': '1'} but i am getting like this [["17-08-2018",1],["18-08-2018",1]] Commented Aug 16, 2018 at 7:29
  • Why your date property is an array? Do you want result like {'17-08-2018': [1],'18-08-2018': [1]}? Commented Aug 16, 2018 at 7:31
  • i dont want any array format in my result i want date as my key and count as my value Commented Aug 16, 2018 at 7:33
  • But you've array of dates! Dio you want to get the sum of them? Commented Aug 16, 2018 at 7:34

1 Answer 1

1

Use the following code:

var data = {"status":1,"result":[{"followup_datee":"17-08-2018","date":[{"fcount":"1"}]},{"followup_datee":"18-08-2018","date":[{"fcount":"1"}]}]};

var obj = {};

for (var i = 0; i < data.result.length; i++){
  obj[data.result[i].followup_datee] = parseInt(data.result[i].date[0].fcount);
  // Here you change the property.
  // In js you can access to an object's property like an array,
  // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors.

  // If you want to use the count as a string, use instead:
  // obj[data.result[i].followup_datee] = data.result[i].date[0].fcount;
}

console.log(obj);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.