如何平展对象文本属性

How flatten object literal properties?

本文关键字:文本 属性 对象 何平      更新时间:2023-09-26

我有一个由遗留服务器返回的对象,我想通过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"
       }
   }
]

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

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

你可以使用一个基本的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;
});

(小提琴)

您可以使用

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];
    }
}

我相信输出实际上是写一个简单的函数

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