0

我有一个由旧服务器返回的对象,我想通过 JavaScript、jQuery 甚至 Underscore.js 更改客户端的结构。

下面是我的原始对象的样子:

[
   {
      "Id":{
         "LValue":1,
         "Value":1
      },
      "Date":{
         "LValue":"2013-10-17T00:00:00",
         "Value":"2013-10-24T00:00:00"
      },
      "User":{
         "LValue":508,
         "Value":507
      },
      "Comments":{
         "LValue":"This a test load",
         "Value":"This a test"
      },
      "Name":"John Doe",
      "IsDeleted":false
   }
]

不过,在客户端,我想将其展平以获取“值”并将“LValues”填充到单独的属性中,以便以后需要时不会丢失它们:

[
   {
      "Id":1,
      "Date":"2013-10-24T00:00:00",
      "User":507,
      "Comments":"This a test",
      "Name":"John Doe",
      "IsDeleted":false,
      "LValues": {
          "Id":1,
          "Date":"2013-10-17T00:00:00",
          "User":508,
          "Comments":"This a test load"
       }
   }
]

这将使使用该对象变得更加容易,任何帮助将不胜感激!

4

4 回答 4

0

你可以使用

var items = [
   {
      "Id":{
         "LValue":1,
         "Value":1
      },
      // ...
   }
];

items = items[0];
var obj = {LValues: {}};
for(var i in items) {
    if(typeof items[i] === 'object') {
        obj[i] = items[i].Value;
        obj.LValues[i] = items[i].LValue;
    } else {
        obj[i] = items[i];
    }
}
于 2013-10-17T20:34:08.717 回答
0
var oList = [
   {
      "Id":{
         "LValue":1,
         "Value":1
      },
      "Date":{
         "LValue":"2013-10-17T00:00:00",
         "Value":"2013-10-24T00:00:00"
      },
      "User":{
         "LValue":508,
         "Value":507
      },
      "Comments":{
         "LValue":"This a test load",
         "Value":"This a test"
      },
      "Name":"John Doe",
      "IsDeleted":false
   }
];

var newFormat = _(oList).map(function(o) {
  var flattened = { LValues: {} };
  _(o).each(function(val, propName) {
      flattened[propName] = val.Value ? val.Value : val;
      if(val.LValue) {
          flattened.LValues[propName] = val.LValue;
      }
  });
  return flattened;
}
于 2013-10-17T20:26:53.793 回答
0

我相信输出其实就是写一个简单的函数

function flatten(obj){
  var r = {};
  if(obj){
    console.log(obj);
     r.Id = obj.Id.Value;
     r.Date = obj.Date.Value;
     r.User = obj.User.Value;
     r.Comments = obj.Comments.Value;
     r.Name = obj.Name.Value;
     r.IsDeleted = obj.IsDeleted; 
     r.LValues = {}; 
     r.LValues.Id = obj.Id.LValue;
     r.LValues.Date = obj.Date.LValue;
     r.LValues.User = obj.User.LValue;
     r.LValues.Comments = obj.Comments.LValue;
  }  
  return r;
}
于 2013-10-17T20:46:15.140 回答
0

您可以为此使用基本的 Javascript 地图。假设硬编码属性:

var flattenedItems = items.map(function(x) {
return {
    Id: x.Id.Value,
    Date: x.Date.Value,
    User: x.User.Value,
    Comments: x.Comments.Value,
    Name: x.Name,
    IsDeleted: x.IsDeleted,
    LValues: {
        Id: x.Id.LValue,
        Date: x.Date.LValue,
        User: x.User.LValue,
        Comments: x.Comments.LValue,
    }
};
});

(小提琴)

如果属性是可变的,那么您可以在迭代器中遍历它们map

var flattenedItems = items.map(function(x) {
    var flattened = { LValues: {} };
    for (var prop in x) {
        flattened[prop] = x[prop].Value;
        flattened.LValues[prop] = x[prop].LValue;
    };
    return flattened;
});

(小提琴)

于 2013-10-17T20:24:56.740 回答