简体   繁体   English

如何在JavaScript中将对象列表转换为对象数组?

[英]How to turn an list of object into an array of objects in javascript?

I'm learning to manipulate arrays and objects in Javascript. 我正在学习在Javascript中操纵数组和对象。

This is a list of Objects: 这是对象列表:

Input: 输入:

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

Desired Output: 所需输出:

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

I'm new to javascript programming. 我是javascript编程的新手。 been trying to solve this problem but no solution yet? 一直试图解决这个问题,但还没有解决方案? Anyone, please help? 有人帮忙吗

Also, 也,

Object.values(obj);

where obj = your data 其中obj =您的数据

You should get it as JSON out of postgres with row_to_json() , json_agg and/or array_to_json() . 您应该使用row_to_json()json_agg和/或array_to_json()从postgres row_to_json()其作为JSON获取。 See this documentation about Aggregate functions 请参阅有关聚合函数的此文档

select array_to_json(array_agg(row_to_json(t)))
from (
  select id, text from words
) t

Or, your list has to be a string as it is not valid JS. 或者,您的列表必须是字符串,因为它不是有效的JS。

`${yourQueryData}`

If the input has to be as mentioned in the question, so a string, (let's say from a txt input or whatever) here's what we can do. 如果输入有在问题被提到,这样的字符串,(让我们从一个txt输入或什么说的)这里是我们能做些什么。

We'll add brackets around that string to get started. 我们将在该字符串周围添加方括号以开始使用。

Then we identify each aspect that will not be valid when we parse it to JSON data. 然后,我们确定将其解析为JSON数据时无效的各个方面。

  1. Add commas between objects: }{ to },{ 在对象之间添加逗号: }{},{
  2. Get the keys and add quotes around them: user_id: to "user_id": 获取密钥并在其周围添加引号: user_id:"user_id":
  3. Replace single quotes by double quotes: 'Alice' becomes "Alice" 用双引号替换单引号: 'Alice'变为"Alice"
  4. Your timestamps must be converted to strings: 2018-11-23T20:47:39.618Z to "2018-11-23T20:47:39.618Z" 您的时间戳记必须转换为字符串: 2018-11-23T20:47:39.618Z"2018-11-23T20:47:39.618Z"

Once this is all fixed, we use JSON.parse() and there you have your JSON array of objects. 修复所有问题后,我们将使用JSON.parse() ,您将拥有对象的JSON数组。

There are many ways to get there. 有很多方法可以到达那里。 You could split your string at several places then iterate and do the necessary tasks. 您可以在多个位置分割字符串,然后进行迭代并执行必要的任务。

Here I'll be using regex. 在这里,我将使用正则表达式。 For the sake of clarity, each step is quite explicit, even the regexes have way more groups than necessary but I hope it makes it easier to understand as it is. 为了清楚起见,每个步骤都非常明确,即使正则表达式的组也比必要的多,但我希望它可以使它更容易理解。

This answer is based exactly on the code you provided. 该答案完全基于您提供的代码。 But it will require some modifications if a name containes a single quote for instance. 但是,如果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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM