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);

                     }
                 }
             });
         });
5
  • 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. Commented Jun 11, 2018 at 7: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. Commented Jun 11, 2018 at 7: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.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented Jun 11, 2018 at 7:15
  • @T.J.Crowder Sorry for the confusion. It's not a JSON data. I have updated the title. Commented Jun 11, 2018 at 7: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. Commented Jun 11, 2018 at 7:19

3 Answers 3

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);

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

2 Comments

Could you please explain? It would greatly help me.
@StrugglingCoder I added an explanation. Please let me know if you have a Q
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 ] ]

Comments

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)

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.