Looping through json data using angular and filling in missing values

411 Views Asked by At

So I have this problem where I am populating a range input using data coming from an external json file...

So basically the data has a bunch of objects with an array of values and text in each object. But some of the objects don't have consecutive values to loop though and output on the range slider like this:

var someObj = [
  {
    value: 1,
    text: 'A'
  },
  {
    value: 2,
    text: 'B'
  },
  {
    value: 3,
    text: 'C'
  },
  {
    vaule: 5,
    text: 'D'
  },
  {
    value: 6,
    text: 'E'
  },
  {
    vaule: 8,
    text: 'F'
  }
];

Notice that Value: 4 and value: 7 are missing, this can change too, in some objects value: 11 and value: 13 are missing too for example.

So basically what i need to achieve is to loop through each object and if there are values missing to add the value in and duplicate the text value from the value before so for example...

var someObj = [
  {
    value: 1,
    text: 'A'
  },
  {
    value: 2,
    text: 'B'
  },
  {
    value: 3,
    text: 'C'
  },
  {
    vaule: 5,
    text: 'D'
  },
  {
    value: 6,
    text: 'E'
  },
  {
    vaule: 8,
    text: 'F'
  }
];

Would become

var someObj = [
  {
    value: 1,
    text: 'A'
  },
  {
    value: 2,
    text: 'B'
  },
  {
    value: 3,
    text: 'C'
  },
  {
    value: 4,
    text: 'C'
  },
  {
    vaule: 5,
    text: 'D'
  },
  {
    value: 6,
    text: 'E'
  },
  {
    vaule: 7,
    text: 'E'
  },
  {
    vaule: 8,
    text: 'F'
  }
];

Is this possible and if so how on earth would i go about it please??

thanks in advance

4

There are 4 best solutions below

4
bukke hari prasad On BEST ANSWER
var i=0;
do{
if(someObj[i].value!=i+1){
    var obj = {}
    debugger;
    obj['value']= i+1;
    obj['text']= someObj[i-1].text;
    someObj.splice(i,0,obj);
}
i++;

}while(i<someObj.length);

refer this one :https://jsfiddle.net/cw5kkpgu/

2
cullimorer On

Ok I actually finally have solved it now, sorry didn't fully understand initially what you were after but here's the Plunker for it:

http://plnkr.co/edit/n1tSfj2YJMeBZNZCSAF3?p=preview

Please confirm if it is :)

The crux of it is:

$scope.loop = function(){
  var previousText = '';
  var previousValue = '';
  var newObject = [];

  angular.forEach($scope.someObj, function(object){

    if(object.value == 1){
      newObject.push(object);
      previousText = object.text;
      previousValue = object.value;
      console.log("value:" + previousValue + " | Text:" + previousText);
    }
    else {
      if(object.value != (previousValue + 1)){
        while (object.value != (previousValue + 1)){
            previousValue += 1;
            newObject.push({ value: previousValue, text: previousText})

            console.log("value:" + previousValue + " | Text:" + previousText);
        }
      }
      else{
            newObject.push(object);
            previousValue = object.value;

            console.log("value:" + previousValue + " | Text:" + previousText);
      }

      previousText = object.text;
    }

  });

  // Add the last value as it's always missed
  newObject.push($scope.someObj[$scope.someObj.length - 1])

  $scope.someObj = newObject;
}
0
brute_force On

I have created a js function to complete the missing objects. Let me know if it works.

function completeObject(someObj)
{
    var otherObj = [];
    for(var obj in someObj)
    {
      otherObj[someObj[obj]['value']] = someObj[obj];
    }

    var j = 0;
    for(var i =1 ; i <= someObj[someObj.length-1]['value'];i++)
    {
      if(otherObj[i] === undefined)
      {
        var obj = {}
        obj['value']= i;
        obj['text']= otherObj[j]['text'];
        otherObj.push(obj);
      }
      else
      {
        j = i ;
      }
    }

    return otherObj;
}
0
krishna On

Get the last object value like

var someObjLength = someObj[someObj.length-1].value;

And

for(i = 0; i < someObjLength-1; i++) {

    if(someObj[i].value != (i+1)){

        someObj.splice(i, 0, {value: i+1, text: someObj[i-1].text})

    }

}