2

I have an array data format coming from back-end which looks like:

Array

[{"ckey::"C1","date":"0506","rows":17},
 {"ckey::"C1","date":"0706","rows":7},
 {"ckey::"C2","date":"0706","rows":13},
 {"ckey::"C2","date":"0806","rows":11}]

So for few days C1 data is there and few days C2 data. Only one day has C1 and C2 data both.

I want to build an array like for C1 and C2

[[17,7,0],[0,13,11]]

First nested array for C1 where third value is 0 because for 0806 date the value was not present.

Second nested array for C2 where first value is 0 because for 0506 date the value was not present.

Please help. I cannot form the array effectively.

I think it would be O(n^3) solution. But please help with the same.

UPDATE

Here was my approach, I could not post the code here but it looks something like this.

I was getting date values in separate array like and I filter for unique days.

        angular.forEach(data, function(obj){
            if(timeData.indexOf(obj.date) === -1)
                timeData.push(obj.date);
        });

Then ckey array _distinctckeyArray also were there containing values ["C1","C2"].

angular.forEach(_distinctckeyArray,function(_ckey){
     var _formattedDataArrItem = [];
     angular.forEach(timeData,function(_dateTimeString) {
         var _tempDataVolume = [];

            angular.forEach(_data,function(_dataObj) {
                 if(_dataObj.date === _dateTimeString) {
                     if(_dataObj.ckey === _ckey) {
                         _tempDataVolume.push(_dataObj.rows);

                     }else {
                         _tempDataVolume.push(0);

                     }
                 }
             });
         });
StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103
  • 1
    If the data from the backend really looks like you've shown, it's not JSON -- or at least, not valid JSON. The quotes are unbalanced. – T.J. Crowder Jun 11 '18 at 07:09
  • @T.J.Crowder It's a real array and we can loop through the array. I just depicted the data format but it is real JS array of objects. – StrugglingCoder Jun 11 '18 at 07:09
  • 1
    Then you might want to fix the question. It's also unclear what you're asking. If it's an array of objects, it's not JSON. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jun 11 '18 at 07:15
  • @T.J.Crowder Sorry for the confusion. It's not a JSON data. I have updated the title. – StrugglingCoder Jun 11 '18 at 07:17
  • i dint understabd this line "First nested array for C1 where third value is 0 because for 0806 date the value was not present.". Could you please make it clear. – simbathesailor Jun 11 '18 at 07:19

3 Answers3

1

You can make an object dates that will have date properties. Initialize the values to 0

You reduce to group the array. Use Object.values to convert the object into an array.

let arr = [{ckey:"C1","date":"0506","rows":17},{ckey:"C1","date":"0706","rows":7},{ckey:"C2","date":"0706","rows":13},{ckey:"C2","date":"0806","rows":11}];

//Make an object with property dates. assign all property to 0 as initial value.
//Expected output:
//{"0506":0,"0706":0,"0806":0}
let dates = arr.reduce((c, v) => Object.assign(c, {[v.date]: 0}), {});

//Loop thru the array using `reduce`. 
//This is to group the array to object using the ckey as the key
//After grouping, use `Object.values` to convert the object into array
let result = Object.values(arr.reduce((c, {ckey,date,rows}) => {
  c[ckey] = c[ckey] || { ...dates };   //Check if c[ckey] exist. If it does not, "clone" the dates object.
  c[ckey][date] = rows;                //Overide the initial value 0 to the rows value
  return c;
}, {})).map(o => Object.values(o));

console.log(result);
Eddie
  • 26,593
  • 6
  • 36
  • 58
1

I think this is what you are looking for. Let me know.

let data = [{
  'ckey': 'C1',
  'date': '0506',
  'rows': 17
}, {
  'ckey': 'C1',
  'date': '0706',
  'rows': 7
}, {
  'ckey': 'C2',
  'date': '0706',
  'rows': 13
}, {
  'ckey': 'C2',
  'date': '0806',
  'rows': 11
}]

function nested_arrays(array) {
  const result = []

  const obj = {
    c1: [],
    c2: []
  }

  for (let i = 0; i < array.length; i++) {

    if (array[i].ckey === 'C1') {
      obj.c1.push(array[i].rows)
    }
    if (array[i].ckey === 'C2') {
      obj.c2.push(array[i].rows)
    }

  }

  obj.c1.push(0) // set last value to 0
  obj.c2.unshift(0) // set first value to 0

  result.push(obj.c1, obj.c2)

  return result

}

let _tempDataVolume = nested_arrays(data)
console.log(_tempDataVolume) //=> [ [ 17, 7, 0 ], [ 0, 13, 11 ] ]
Flavio
  • 506
  • 4
  • 9
1
let arr = [{"ckey::"C1","date":"0506","rows":17},
 {"ckey::"C1","date":"0706","rows":7},
 {"ckey::"C2","date":"0706","rows":13},
 {"ckey::"C2","date":"0806","rows":11}]

    arr.map(res =>{ 
let c1arr = [],
 let c2arr = [], 
    if(res.ckey== 'C1'){  
      c1arr.push(res.rows) 
    }else{ c2arr.push(res.rows) }
     })

let newArrr = []
newArr.push(c1arr);
newArr.push(c2arr);

console.log('arr is',newArr)
jadlmir
  • 465
  • 1
  • 6
  • 16