简体   繁体   English

使用angular遍历json数据并填写缺失值

[英]Looping through json data using angular and filling in missing values

So I have this problem where I am populating a range input using data coming from an external json file... 所以我有一个问题,我正在使用来自外部json文件的数据填充范围输入...

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. 请注意,缺少值:4和值:7,这也可以更改,例如,在某些对象中,值:11和值:13也丢失。

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 提前致谢

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/ 指一本: https : //jsfiddle.net/cw5kkpgu/

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

    }

}

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: 好的,我现在终于解决了,很抱歉,最初并没有完全了解您的需求,但是这里是Plunker:

http://plnkr.co/edit/n1tSfj2YJMeBZNZCSAF3?p=preview 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;
}

I have created a js function to complete the missing objects. 我创建了一个js函数来完成缺少的对象。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM