How to create individual arrays from keys of an object

javascript object get value by key
javascript get value by key in array
convert object to array of objects javascript
object.keys foreach
object to array javascript
convert object to array typescript
javascript object keys values
javascript object to array of values

Here is my Object:

I want to create three arrays from the three keys of the object such as:

let person = [Object.person];
let products = [Object.products];
let items = [Object.items];

So, if I print that three variables,

console.log(person);
console.log('==========================');
console.log(products);
console.log('==========================');
console.log(items);

Expected output will be:

[
    {
        id: '1',
        profession: 'student',
        name: 'Alex'
    },
    {
        id: '2',
        profession: 'businessman',
        name: 'Jay'
    }
]
==========================
[
   {
      id: '5',
      name: 'furniture',
      price: 200.0
   }
]
==========================
[
   {
      id: '6',
      name: 'Medical Devices',
      price: 70.0
    }
]

How to do this?

let obj = {
        person: [
            {
                id: '1',
                profession: 'student',
                name: 'Alex'
            },
            {
                id: '2',
                profession: 'businessman',
                name: 'Jay'
            }
        ],
        products: [
            {
                id: '5',
                name: 'furniture',
                price: 200.0
            }
        ],
        items: [
            {
                id: '6',
                name: 'Medical Devices',
                price: 70.0
            }
        ]
    };
    
let person = [obj.person];
let products = [obj.products];
let items = [obj.items];

console.log(person);
console.log('==========================');
console.log(products);
console.log('==========================');
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Simply use object destructuring. Please note that you cannot use Object as a variable name because it is a reserved word (name of the Object prototype in Javascript).

let obj = {
    person: [
        {
            id: '1',
            profession: 'student',
            name: 'Alex'
        },
        {
            id: '2',
            profession: 'businessman',
            name: 'Jay'
        }
    ],
    products: [
        {
            id: '5',
            name: 'furniture',
            price: 200.0
        }
    ],
    items: [
        {
            id: '6',
            name: 'Medical Devices',
            price: 70.0
        }
    ]
};

let { person, products, items } = obj;

console.log(person);
console.log(products);
console.log(items);

Creating an array from object properties with Object.keys() vs Object , Use .keys() if you need to do something with the keys other than to retrieve the values. Otherwise, you're only getting the keys in order to access  Learn how to create a key value array in javascript. Arrays in javascript are not like arrays in other programming language. They are just objects with some extra features that make them feel like an array. It is advised that if we have to store the data in numeric sequence then use array else use objects where ever possible.

You just need to remove the wrapper [] in your initialization of those three variables, since they are already arrays -

So, instead of

let person = [obj.person]; // This will create an array with a single item as whatever is in `obj.person`, in your case an array

you should use

let person = obj.person;

Run the code snippet below to check

const obj = {
    person: [
        {
            id: '1',
            profession: 'student',
            name: 'Alex'
        },
        {
            id: '2',
            profession: 'businessman',
            name: 'Jay'
        }
    ],
    products: [
        {
            id: '5',
            name: 'furniture',
            price: 200.0
        }
    ],
    items: [
        {
            id: '6',
            name: 'Medical Devices',
            price: 70.0
        }
    ]
};

let person = obj.person;
let products = obj.products;
let items = obj.items;

console.log(person);
console.log('==========================');
console.log(products);
console.log('==========================');
console.log(items);

Object.keys, values, entries, Let's step away from the individual data structures and talk about the iterations over If we ever create a data structure of our own, we should implement them too. Object.entries(obj) – returns an array of [key, value] pairs. Object.keys(o): The object.keys(o) method is used to traverse an object o and returns an array containing all enumerable property names. Object.getOwnPropertyNames(o): A lesser known method, it iterates over an object o and returns an array of all natural property names of the object, enumerable or not. Arrays

First I don't belive you can use Object since it's reserved, second remove [] around [obj.person]

let person = obj.person;
let products = obj.products;
let items = obj.items;

Demo

var obj = {
  person: [{
      id: '1',
      profession: 'student',
      name: 'Alex'
    },
    {
      id: '2',
      profession: 'businessman',
      name: 'Jay'
    }
  ],
  products: [{
    id: '5',
    name: 'furniture',
    price: 200.0
  }],
  items: [{
    id: '6',
    name: 'Medical Devices',
    price: 70.0
  }]
};

let person = obj.person;
let products = obj.products;
let items = obj.items;

console.log(person);
console.log('==========================');
console.log(products);
console.log('==========================');
console.log(items);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

How to convert an Object {} to an Array [] of key-value pairs in , Objects are more complex and each object may contain any combination of these primitive data-types as well as reference data-types, while the array is a single  Group Array of JavaScript Objects by Keys or Property Values This fork of JamieMason's implementation changes the key parameter to be an array of keys instead of just a single key. values() returns an array whose elements are the enumerable property values found on the object. For example, an array may have keys 0, 1, 2, 5, 3, 6, 7.

save the first Object as variable and access the properties and save to a new variables like :

const myObject = {
person: [
    {
        id: '1',
        profession: 'student',
        ...
        price: 70.0
     }
  ]
};
let person = myObject.person;
let products = myObject.products;
let items = myObject.items;

console.log(person, products, items);

Object.keys(), The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop  Use Object.entries(obj) to get an array of key/value pairs from obj. Use array methods on that array, e.g. map . Use Object.fromEntries(array) on the resulting array to turn it back into an object.

Object.fromEntries(), fromEntries() method transforms a list of key-value pairs into an object. as Array or Map or other objects implementing the iterable protocol. Object.keys(obj) Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object. Description. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object

Associative Array in JavaScript, When you assign values ​​to keys in a variable of type Array, the array is So we can create an associative array with the Object reserved word, then and of ECMAScript, we can get the list of attributes of an object in a single statement: Syntax Object.keys(obj)Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object.

Array Functions - Manual, array_change_key_case — Changes the case of all keys in an array the values from a single column in the input array; array_combine — Creates an array by their values; count — Count all elements in an array, or something in an object  If you choose not to assign an explicit key to list items then React will default to using indexes as keys. Here is an in-depth explanation about why keys are necessary if you’re interested in learning more. Extracting Components with Keys . Keys only make sense in the context of the surrounding array.

Comments
  • I don't think you can use the word Object what if you rename it to [obj.person] ?