[英]How to turn an list of object into an array of objects in javascript?
我正在学习在Javascript中操纵数组和对象。
这是对象列表:
输入:
{ user_id: 1,
name: 'Alice',
created_at: 2018-11-23T20:47:39.618Z,
count: '3' }
{ user_id: 4,
name: 'Daphne',
created_at: 2018-11-19T21:47:39.618Z,
count: '3' }
{ user_id: 2,
name: 'Bob',
created_at: 2018-11-23T18:47:39.618Z,
count: '2' }
{ user_id: 5,
name: 'Evan',
created_at: 2018-11-18T19:47:39.618Z,
count: '2' }
{ user_id: 6,
name: 'Fabia',
created_at: 2018-11-22T23:47:39.618Z,
count: '2' }
所需输出:
[ { user_id: 1,
name: 'Alice',
created_at: 2018-11-23T20:47:39.618Z,
count: '3' },
{ user_id: 4,
name: 'Daphne',
created_at: 2018-11-19T21:47:39.618Z,
count: '3' },
{ user_id: 2,
name: 'Bob',
created_at: 2018-11-23T18:47:39.618Z,
count: '2' },
{ user_id: 5,
name: 'Evan',
created_at: 2018-11-18T19:47:39.618Z,
count: '2' },
{ user_id: 6,
name: 'Fabia',
created_at: 2018-11-22T23:47:39.618Z,
count: '2' } ]
我是javascript编程的新手。 一直试图解决这个问题,但还没有解决方案? 有人帮忙吗
也,
Object.values(obj);
其中obj =您的数据
您应该使用row_to_json()
, json_agg
和/或array_to_json()
从postgres row_to_json()
其作为JSON获取。 请参阅有关聚合函数的此文档
select array_to_json(array_agg(row_to_json(t)))
from (
select id, text from words
) t
或者,您的列表必须是字符串,因为它不是有效的JS。
`${yourQueryData}`
如果输入有在问题被提到,这样的字符串,(让我们从一个txt输入或什么说的)这里是我们能做些什么。
我们将在该字符串周围添加方括号以开始使用。
然后,我们确定将其解析为JSON数据时无效的各个方面。
}{
至},{
user_id:
到"user_id":
'Alice'
变为"Alice"
2018-11-23T20:47:39.618Z
到"2018-11-23T20:47:39.618Z"
修复所有问题后,我们将使用JSON.parse()
,您将拥有对象的JSON数组。
有很多方法可以到达那里。 您可以在多个位置分割字符串,然后进行迭代并执行必要的任务。
在这里,我将使用正则表达式。 为了清楚起见,每个步骤都非常明确,即使正则表达式的组也比必要的多,但我希望它可以使它更容易理解。
该答案完全基于您提供的代码。 但是,如果name
包含一个单引号,则需要进行一些修改。
const list = ` { user_id: 1, name: 'Alice', created_at: 2018-11-23T20:47:39.618Z, count: '3' } { user_id: 4, name: 'Daphne', created_at: 2018-11-19T21:47:39.618Z, count: '3' } { user_id: 2, name: 'Bob', created_at: 2018-11-23T18:47:39.618Z, count: '2' } { user_id: 5, name: 'Evan', created_at: 2018-11-18T19:47:39.618Z, count: '2' } { user_id: 6, name: 'Fabia', created_at: 2018-11-22T23:47:39.618Z, count: '2' } ` // Add brackets around the list to have the string representation of an array const array = `[${list}]` // wrap into array // 1. Add commas between objects: `}{` to `},{` const regex = /\\}[\\r\\n|\\r|\\n]?\\{/gm // match closing followed by opening bracket const fixObjects = array.replace(regex, "},\\n{") // add comma at the end of each object // 2. Get the keys and add quotes around them: `user_id:` to `"user_id":` const keysReg = /^(?:[\\s|\\{]+)([\\w]+)(?:\\:)/gm // get keys const fixKeys = fixObjects.replace(keysReg, function(match, g1) { return match.replace(g1, `"${g1}"`) }); // wrap keys in quotes // 3. Replace single quotes by double quotes: `'Alice'` becomes `"Alice"` const quotesReg = /'(.*)'/gm // match between single quotes const fixQuotes = fixKeys.replace(quotesReg, function(match, g1) { return `"${g1}"` }) // 4. Your timestamps must be converted to strings: `2018-11-23T20:47:39.618Z` to `"2018-11-23T20:47:39.618Z"` const datesReg = /(\\d{4})\\-(\\d{2})\\-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}).(\\d{3})Z/gm // timestamps as strings const fixDates = fixQuotes.replace(datesReg, function(match) { return `"${match}"` } ) console.log(JSON.parse(fixDates))
尝试这个..
objectsArray = Array.from( ObjectsList );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.