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

convert object to array of objects javascript
convert object to array javascript
convert object to array typescript
javascript object to array of values
convert array to key value pair javascript
convert object to array angular 6
convert object into array php
convert object to array c#

I'm learning to manipulate arrays and objects in 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. been trying to solve this problem but no solution yet? Anyone, please help?

You should get it as JSON out of postgres with row_to_json(), json_agg and/or array_to_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.

`${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.

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.

  1. Add commas between objects: }{ to },{
  2. Get the keys and add quotes around them: user_id: to "user_id":
  3. Replace single quotes by double quotes: 'Alice' becomes "Alice"
  4. Your timestamps must be converted to strings: 2018-11-23T20:47:39.618Z to "2018-11-23T20:47:39.618Z"

Once this is all fixed, we use JSON.parse() and there you have your JSON array of objects.

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.

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))

Javascript, Converting an Object to an Array of Objects., With this in mind it becomes important to understand how to convert our data into our desired format. The Example. Using newer Javascript  A while back I wrote an article that got some traction about converting an Array of Objects to an Object. Today we are going to do the opposite, convert an object to an array of objects.

Also,

Object.values(obj);

where obj = your data

Rewriting JavaScript: Converting an Array of Objects to an Object., The Solution. For me the ideal solution would be to convert the array into an object that looks like the following: const peopleObject = { "  How to convert an Object {} to an Array [] of key-value pairs in JavaScript? The task is to convert an Object {} to an Array [] of key-value pairs using JavaScript. Introduction: Objects, in JavaScript, is it’s most important data-type and forms the building blocks for modern JavaScript.

Try this..

objectsArray = Array.from( ObjectsList );

Converting Object to an Array, We have 3 variations to convert an Object to an Array in JavaScript. So by converting the object into an array, you have access to all of that. Woohoo 🥳 Long long ago, in a far far galaxy, looping over Objects was not so easy. Okay, I'​m  I've written a method that takes an object whose keys are arrays and converts it to an array of objects. I feel like the code is fairly complex, after waiting a week or so and revisiting it, I don't think anyone would be able to really understand what is going on, especially if they aren't used to this style of coding.

How to convert an Object {} to an Array [] of key-value pairs in , Introduction: Objects, in JavaScript, is it's most important data-type and forms the building It is often used when we want to store list of elements and access them by a single variable. We can convert an Object {} to an Array [] of key-value pairs using methods How to convert a plain object into ES6 Map using JavaScript ? var object = arr.reduce( (obj, item) => (obj[item.key] = item.value, obj) , {}); Here we benefit from comma operator, it evaluates all expression before comma and returns a last one (after last comma). So we don't copy obj each time, rather assigning new property to it. share.

How to push an array into the object in JavaScript ?, In order to push an array into the object in JavaScript, we need to utilize the push​() function. With the But here we will use this function to push the whole array into an object. Syntax: How to remove object from array of objects using JavaScript ? How to convert an Object {} to an Array [] of key-value pairs in JavaScript? .map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array. This modification is done based on what is returned in the callback function.

Array.from(), arrayLike: An array-like or iterable object to convert to an array. array-like objects (objects with a length property and indexed elements); or the intermediate array would necessarily have values truncated to fit into the appropriate type. method // of C with an argument list containing the single item len. An array-like or iterable object to convert to an array. mapFn Optional Map function to call on every element of the array. thisArg Optional Value to use as this when executing mapFn. Return value. A new Array instance. Description. Array.from() lets you create Arrays from: array-like objects (objects with a length property and indexed elements); or

Comments
  • where is the difference? what have you tried?
  • Your list of objects is just "floating" in space, its not valid...
  • There is not such thing as a (built-in) list data type in JS. What do you mean with list?
  • please add the original format of the input data.
  • The way you have presented that query result is not valid JS. Please show whether it is an object (with properties) or an array. Also show what you mean with destructuring it (into what?). To be sure, output JSON.stringify(queryresult) and put that in your question.