0

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',
}

实际上数据是通过 django 后端中的 SQL 查询和 item( models.TextField()) 和 date( models.DateTimeField()) 获取的 似乎 JS 对象已损坏,我想知道这种情况的任何解决方案

4

1 回答 1

0

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>

于 2020-06-06T15:07:01.413 回答