如何展平对象的文字属性?

时间:2013-10-17 20:19:42

标签: javascript jquery json underscore.js object-literal

我有遗留服务器返回的对象,我想通过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 个答案:

答案 0 :(得分: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,
    }
};
});

(Fiddle)

如果属性是可变的,那么你可以在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;
});

(Fiddle)

答案 1 :(得分: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;
}

答案 2 :(得分: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];
    }
}

答案 3 :(得分: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;
}