1

I am trying to create different dataset based on month value. For eg. for June month one dataset and for July another dataset. But in my code, all the month values are getting combined and created as one dataset.

It will be really helpful who can help me in creating different dataset dynamically. I have attached the fiddle which I tried with my data object

JSFIDDLE

var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }]
  var maindataset = [];
  var dataset = [];
 ["reqC", "resC"].forEach((series) => {
    dataset.push({
      seriesname: series,
      data: obj.map((el) => {
        return el[series]
      })
    })

  });
  maindataset.push({
    dataset: dataset
  });
  alert(JSON.stringify(maindataset));

// Expected Output  

{
  "dataset": [
    {
      "dataset": [                 //June
        {
          "seriesname": "Req",   
          "data": [
            {
              "value": "129963"
            },
            {
              "value": "261162"
            }
          ]
        },
        {
          "seriesname": "Res",
          "data": [
            {
              "value": "80522"
            },
            {
              "value": "83743"
            }
          ]
        }
      ]
    },
    {
      "dataset": [                   //July
        {
          "seriesname": "Req",
          "data": [
            {
              "value": "438860"
            }
          ]
        },
        {
          "seriesname": "Res",
          "data": [
            {
              "value": "166107"
            }
          ]
        }
      ]
    }
  ]
}

4 Answers 4

1

You could use a nested hash table and iterate later the keys for the wanted parts.

var data = [{ date: "2017-06-01", reqC: "129963", month: "JUNE", resC: "80522" }, { date: "2017-06-05", reqC: "261162", month: "JUNE", resC: "83743" }, { date: "2017-07-03", reqC: "438860", month: "JULY", resC: "166107" }],
    result = { dataset: [] },
    parts = { reqC: 'Req', resC: 'Res' },
    hash = { _: result.dataset };

data.forEach(function (a) {
    var temp = hash;
    if (!temp[a.month]) {
        temp[a.month] = { _: [] };
        temp._.push({ dataset: temp[a.month]._ });
    }
    temp = temp[a.month];
    Object.keys(parts).forEach(function (k) {
        if (!temp[k]) {
            temp[k] = { _: [] };
            temp._.push({ seriesname: parts[k], data: temp[k]._ });
        }
        temp[k]._.push({ value: a[k] });
    });
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

You can create groups based on month and then you can output the desired data structure. Check the snippet.

var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }];

var result = {};

var groups = obj.reduce(function(acc, obj) {
  acc[obj.month] = acc[obj.month] || [];
  acc[obj.month].push(obj);
  return acc;
}, {});

//console.log(groups);

result.dataset = Object.keys(groups).map(function(key) {
  return {
    dataset: [{
      "seriesname" : "Req",   
      "data": groups[key].map(function(o) {
        return { value : o.reqC };
      })
    }, {
      "seriesname" : "Res",   
      "data": groups[key].map(function(o) {
        return { value : o.resC };
      })
    }]
  };
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

0

You cannot use the same property name for an object twice. You have an object in your data that looks like this:

"data": [
      {
        "value": "80522"
      },
      {
        "value": "83743"
      }
    ]

Either change the keys to unique ones:

"data": [
      {
        "value1": "80522"
      },
      {
        "value2": "83743"
      }
    ]

Or make it an array:

"data": [ "80522", "83743" ]

1 Comment

thats properties in 2 different objects, there shouldn't be a problem here
0

You need to add check for month as well. Try this:

    var obj = [{
    date: "2017-06-01",
    reqC: "129963",
    month: "JUNE",
    resC: "80522"
  }, {
    date: "2017-06-05",
    reqC: "261162",
    month: "JUNE",
    resC: "83743"
  },{
    date: "2017-07-03",
    reqC: "438860",
    month: "JULY",
    resC: "166107"
  }]
  var maindataset = [];

  ["JUNE","JULY"].forEach((month)=>{
  var dataset = [];
     ["reqC", "resC"].forEach((series) => {
      dataset.push({
        seriesname: series,
        data: obj.reduce((filtered, el) => {
          if(el["month"] === month){
           filtered.push({value: el[series]});
           }
           return filtered;
        },[])
      })

    });
    maindataset.push({
      dataset: dataset
    });
  })

  alert(JSON.stringify(maindataset));

output:

[{
    "dataset": [{
        "seriesname": "reqC",
        "data": [{
            "value": "129963"
        }, {
            "value": "261162"
        }]
    }, {
        "seriesname": "resC",
        "data": [{
            "value": "80522"
        }, {
            "value": "83743"
        }]
    }]
}, {
    "dataset": [{
        "seriesname": "reqC",
        "data": [{
            "value": "438860"
        }]
    }, {
        "seriesname": "resC",
        "data": [{
            "value": "166107"
        }]
    }]
}]

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.