JavaScript: Convert object structure

object in array javascript
javascript object
javascript get object properties
javascript map
javascript object reduce
javascript object has keys
find value in object javascript
how to get value from an object in javascript

I have an object like this:

  "A": [ "-4927","8779","-9971","-23767" ],
  "B": [ "-10617","-1456","3131","259" ],
  "C": [ "-5185","1168","21501","18989" ],
  "D": [ "2010","5664","2148","-674" ]

I want to convert to this:

    name: 'A',
    data: ["-4927","8779","-9971","-23767"]
  }, {
    name: 'B',
    data: ["-10617","-1456","3131","259"]
  }, {
    name: 'C',
    data: ["-5185","1168","21501","18989"]
  }, {
    name: 'D',
    data: ["2010","5664","2148","-674"]

I have used this following method:

var newData = [];
$.each($.parseJSON(data), function(k, v) {
  newData['name'] = k;
  newData['data'] = v;

But it only store the last key pair value as newData. That is

name: 'D',
data: ["2010","5664","2148","-674"]

I understand that it overwrite the previous data and store only the last data it get. But i can't solve this problem.

Any Help ?

You are assigning the properties name and data directly to newData instead of pushing an object into it as an array:

Change your loop to this:

var newData = [];
$.each($.parseJSON(data), function(k, v) {
        const myNewData = {
            name: k,
            data: v

Object.fromEntries(), entries() . Examples. Converting a Map to an Object. With Object.fromEntries , you can convert from Map  JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but it’s available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as language-independent.

You can do this using map

const oldData =
  { "A": [ "-4927","8779","-9971","-23767" ]
  , "B": [ "-10617","-1456","3131","259" ]
  , "C": [ "-5185","1168","21501","18989" ]
  , "D": [ "2010","5664","2148","-674" ]

const newShape = ([ name, data ]) =>
  ({ name, data })

const newData =

// [ { name: "A", data: [ "-4927","8779","-9971","-23767" ] }, ... ]

Object.getOwnPropertyNames(), The Object.getOwnPropertyNames() method returns an array of all properties (​including non-enumerable properties except for those which use  In JavaScript, objects can be seen as a collection of properties. With the object literal syntax, a limited set of properties are initialized; then properties can be added and removed. Property values can be values of any type, including other objects, which enables building complex data structures. Properties are identified using key values.

You can use of Object.keys and to generate your array.

It can be translated to this:

for each key in my object data, create a new item inside of a new array. This item is going to be an object, with a key name and a key data.

const data = {
  A: ['-4927', '8779', '-9971', '-23767'],
  B: ['-10617', '-1456', '3131', '259'],
  C: ['-5185', '1168', '21501', '18989'],
  D: ['2010', '5664', '2148', '-674'],

const ret = Object.keys(data).map(x => ({
  name: x,
  data: data[x],


Data Structures: Objects and Arrays :: Eloquent JavaScript, You'd have to somehow extract the digits and convert them back to numbers to access them. Fortunately, JavaScript provides a data type specifically for storing  A list. Objects, as generic blobs of values, can be used to build all sorts of data structures. A common data structure is the list (not to be confused with array). A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on.

You could use a for in loop and push each element.

var obj = {

var newArr = [];

  for(p in obj){


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 blocks for modern JavaScript. These objects are quite different from  JavaScript Objects. ❮ Previous Next ❯. In real life, a car is an object. A car has properties like weight and color, and methods like start and stop: All cars have the same properties, but the property values differ from car to car. All cars have the same methods, but the methods are performed at different times.

Should be:

var newData = [];
$.each($.parseJSON(data), function(k, v) {
    newData.push({'name': k, 'data': v});

You also don't need to use jQuery. You can do the same in vanilla JS:

var newData = [];
Object.keys(data).forEach(function(k) {
    newData.push({'name': k, 'data': data[k]});

How to convert an object to string using JavaScript?, Below are the methods to convert different objects to string. Method 1: Using the function String() The String() function converts the value of an object to a string. In the example above, the object "employees" is an array. It contains three objects. Each object is a record of a person (with a first name and a last name). Converting a JSON Text to a JavaScript Object. A common use of JSON is to read data from a web server, and display the data in a web page.

JSON methods, toJSON, Let's say we have a complex object, and we'd like to convert it into a string, stringify ( meetup ) ; // Error: Converting circular structure to JSON. The method JSON.stringify(student) takes the object and converts it into a string. The resulting json string is called a JSON-encoded or serialized or stringified or marshalled object. We are ready to send it over the wire or put into a plain data store. Please note that a JSON-encoded object has several important differences from the object

How To Convert Objects to Arrays and Vice Versa in JavaScript, Any time you have a complex data structure within another, we refer to this as nesting. Okay, now that we've done a one-minute primer on JSON,  Rewriting JavaScript: Converting an Array of Objects to an Object. Chris Burgin. Follow. Converting an Array of Objects to an Object (in a good functional way!). The Problem.

JSON PHP, Example. Use JSON.parse() to convert the result into a JavaScript object: var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (​this. Convert Object to Array Example. This section will see how we can convert Object to Array in Angular and Typescript with examples. Typescript Code. Let us assume that you have an object declared multiple properties. First Get the named keys using object.keys() method. This method retrieves keys from the given object and returns an array of keys.

  • Use another array to store the json array.
  • Your overlaying the name and data field on the newData, rather than creating a new sub object and putting it in the array, with those fields
  • Side note, any time you see a block of code that effectively does #1) create array, #2) loop over another element, #3) push elements to created array, this is the pattern that you should consider using either $ or $.map
  • Thanks. It Works :)
  • Like it with destructuration, well thought. Be carefull tho about the support of the Object.entries method in browser check here
  • Thanks for your suggestion :)
  • Thank you for your suggestion :)
  • Thanks for your idea :)