How flatten object literal properties?

喜夏-厌秋 提交于 2020-01-06 16:18:12

问题


I have an object being returned by a legacy server and I want to change the structure on the client-side via JavaScript, jQuery, or even Underscore.js.

Below is what my original object looks like:

[
   {
      "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
   }
]

On the client-side though, I would like to flatten it to get the "Values" and stuff the "LValues" into a separate property so I don't loose them if I need it later:

[
   {
      "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"
       }
   }
]

this would make working with the object so much easier and any help would be deeply appreciated!


回答1:


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:


You could use a basic Javascript map for this. Assuming hard-coded properties:

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)

If the properties are variable, then you could iterate through them inside the map iterator:

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)




回答3:


You could use

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



回答4:


I believe that the output is actually write a simple function

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


来源:https://stackoverflow.com/questions/19436457/how-flatten-object-literal-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!