How to convert an Object {} to an Array [] of key-value pairs in JavaScript
You can use Object.keys()
and map()
to do this
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map((key) => [Number(key), obj[key]]);
console.log(result);
Javascript - Convert object to array object
forEach
is method for array, meanwhile your oldObj
is object
First you have to transform it to array, here we could do is transforming object to array of key-values pairs
And using with map
could make code shorter
const oldObj = {
Georgia: {
notes: "lorem ipsum",
lat: "32.1656",
long: "82.9001",
},
Alabama: {
notes: "lorem ipsum",
lat: "32.3182",
long: "86.9023",
},
}
const res = Object.entries(oldObj).map(([name, obj]) => ({ name, ...obj }))
console.log(res)
Convert object to an array of objects?
You can use .map()
with Object.keys()
:
let data = { "1": "Technology", "2": "Startup", "3": "IT",};
let result = Object.keys(data) .map(key => ({id: Number(key), name: data[key]}));
console.log(result);
How to convert object A to an array of objects with object A's properties and assign value and label to them
Using Object.entries()
and map()
const obj = {"id":12432,"application":"pashmodin","unit":null,"status":"gholam","issueDate":"1999-06-24T00:00:00","description":"hasan"}
const res = Object.entries(obj).map(([label, value]) => ({label, value}))
console.log(res)
best way to convert object with arrays to array with objects and viceversa
You could use two function for generating an array or an object. They works with
Object.keys
for getting all own property names,Array#reduce
for iterating an array and collecting items for return,Array#forEach
just fo itarating an array.
function getArray(object) { return Object.keys(object).reduce(function (r, k) { object[k].forEach(function (a, i) { r[i] = r[i] || {}; r[i][k] = a; }); return r; }, []);}
function getObject(array) { return array.reduce(function (r, o, i) { Object.keys(o).forEach(function (k) { r[k] = r[k] || []; r[k][i] = o[k]; }); return r; }, {});}
var data = { category: ['a', 'b', 'c'], title: ['e', 'f', 'g'], code: ['z', 'x', 'v'] };
console.log(getArray(data));console.log(getObject(getArray(data)));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Typescript Convert Object to Array - because *ngFor does not supports iteration of object
You can use Object.keys(obj) to get named indexes. This will return an array structure which you can use/customize further. A sample use to iterate over object values may look like this
var persons = {
john: { age: 23, year:2010},
jack: { age: 22, year:2011},
jenny: { age: 21, year:2012}
}
Getting an iterator
var resultArray = Object.keys(persons).map(function(personNamedIndex){
let person = persons[personNamedIndex];
// do something with person
return person;
});
// you have resultArray having iterated objects
How to convert an object to an array in axios
try to use res.data.data
. usually, there are 2 levels of data
Related Topics
Is There a Use-Case For Singletons With Database Access in PHP
Is There a Function to Extract a 'Column' from an Array in PHP
Fatal Error: Maximum Execution Time of 30 Seconds Exceeded
How to Create a Pdo Parameterized Query With a Like Statement
Error Message "Strict Standards: Only Variables Should Be Passed by Reference"
PHP + MySQL Transactions Examples
Pdoexception "Could Not Find Driver"
PHP Method Chaining or Fluent Interface
How to Use Multiple Databases in Laravel
Target Class Controller Does Not Exist - Laravel 8
Difference Between Require, Include, Require_Once and Include_Once
How to Validate an Email Address in PHP
Access a JavaScript Variable from PHP
Send Email from Localhost Running Xammp in PHP Using Gmail Mail Server
How to Repair a Serialized String Which Has Been Corrupted by an Incorrect Byte Count Length