提问者:小点点

如何用Lodash格式化JS对象


null

const _ = require('lodash');

org_data = [{ item: 'TESTING123', date: '2020-0610T00:00:00.000Z' },{ item: 'TESTING456', date: '2020-0610T00:00:00.000Z' }]

function formating (rows, key='date') {

  return _.chain(rows).keyBy('item').mapValues(key).value()
};

// output
{
 TESTING123: 2020-0610T00:00:00.000Z,
 TESTING456: 2020-0610T00:00:00.000Z,
}

//expected output
{
 'TESTING123' : '2020-0610T00:00:00.000Z',
 'TESTING456': '2020-0610T00:00:00.000Z',
}

null

实际上,数据是通过SQL查询和django后端中的项目(models.textField())和日期(models.dateTimeField())获取的。JS对象似乎被破坏了,我想知道这种情况下有什么解决方案


共1个答案

匿名用户

null

var org_data = [{ item: 'TESTING123', date: '2020-0610T00:00:00.000Z' },{ item: 'TESTING456', date: '2020-0610T00:00:00.000Z' }]

function formating (rows, key='date') {

  return _.chain(rows).keyBy('item').mapValues(key).value()
};
var t = formating(org_data)

var str = Object.keys(t).map(function (key) { 
  return "" + key + "=" + t[key];
})

console.log(t)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>