如何将JavaScript对象展平为类似菊花链的形式?

时间:2017-03-08 21:25:07

标签: javascript recursion typescript ecmascript-6 flatten

我想要压扁像这样的物体......

var obj1 = {
  firstName: 'John',
  lastName: 'Green',
  car: {
    make: 'Honda',
    model: 'Civic',
    revisions: [
      { miles: 10150, code: 'REV01', changes: },
      { miles: 20021, code: 'REV02', changes: [
        { type: 'asthetic', desc: 'Left tire cap' },
        { type: 'mechanic', desc: 'Engine pressure regulator' }
      ] }
    ]
  },
  visits: [
    { date: '2015-01-01', dealer: 'DEAL-001' },
    { date: '2015-03-01', dealer: 'DEAL-002' }
  ]
};

...进入如下所示的菊花链形式:

{
    "firstName": "John",
    "lastName": "Green",
    "car.make": "Honda",
    "car.model": "Civic",
    "car.revisions.0.miles": 10150,
    "car.revisions.0.code": "REV01",
    "car.revisions.0.changes": ,
    "car.revisions.1.miles": 20021,
    "car.revisions.1.code": "REV02",
    "car.revisions.1.changes.0.type": "asthetic",
    "car.revisions.1.changes.0.desc": "Left tire cap",
    "car.revisions.1.changes.1.type": "mechanic",
    "car.revisions.1.changes.1.desc": "Engine pressure regulator",
    "visits.0.date": "2015-01-01",
    "visits.0.dealer": "DEAL-001",
    "visits.1.date": "2015-03-01",
    "visits.1.dealer": "DEAL-002"
}

这是我的(失败)尝试:

function flatten(obj) {
    var flattenObject = {};

    // iterate given object
    for (let x in obj) {
        if (typeof obj[x] == 'string') {
            flattenObject[x] = obj[x];
        }

        if (typeof obj[x] == 'object') {
            for (let y in obj[x]) {
                flattenObject[x + '.' + y] = obj[x][y];
            }
        }
    }

    return flattenObject;
}

我很快就开始不必要地重复代码,以便菊花链内部对象和数组。这绝对是需要递归的东西。有什么想法吗?

编辑:此问题与other questions类似,但不重复。这个问题需要一个特定的符号和嵌套的对象和数组。

编辑:我也在another question中问了相反的,不平坦的。

2 个答案:

答案 0 :(得分:1)

您可以像这样创建递归函数,将以前的键存储在一个字符串中非常重要。

var obj1 = {
  firstName: 'John',
  lastName: 'Green',
  car: {
    make: 'Honda',
    model: 'Civic',
    revisions: [
      { miles: 10150, code: 'REV01', changes: 0},
      { miles: 20021, code: 'REV02', changes: [
        { type: 'asthetic', desc: 'Left tire cap' },
        { type: 'mechanic', desc: 'Engine pressure regulator' }
      ] }
    ]
  },
  visits: [
    { date: '2015-01-01', dealer: 'DEAL-001' },
    { date: '2015-03-01', dealer: 'DEAL-002' }
  ]
};

function flatten(data, c) {
  var result = {}
  for(var i in data) {
    if(typeof data[i] == 'object') Object.assign(result, flatten(data[i], c + '.' + i))
    else result[(c + '.' + i).replace(/^\./, "")] = data[i]
  }
  return result
}

console.log(JSON.stringify(flatten(obj1, ''), 0, 4))

答案 1 :(得分:0)

试试这个:



function flatten(obj)
{
  var result = {};
  (function f(e, p) {
    switch (typeof e) {
      case "object":
        p = p ? p + "." : "";
        for (var i in e)
          f(e[i], p + i);
        break;
      default:
        result[p] = e;
        break;
    }
  })(obj);
  return result;
}

var obj1 = {
  firstName: 'John',
  lastName: 'Green',
  car: {
    make: 'Honda',
    model: 'Civic',
    revisions: [{
      miles: 10150,
      code: 'REV01',
    }, {
      miles: 20021,
      code: 'REV02',
      changes: [{
        type: 'asthetic',
        desc: 'Left tire cap'
      }, {
        type: 'mechanic',
        desc: 'Engine pressure regulator'
      }]
    }]
  },
  visits: [{
    date: '2015-01-01',
    dealer: 'DEAL-001'
  }, {
    date: '2015-03-01',
    dealer: 'DEAL-002'
  }]
};

console.log(flatten(obj1));