Convert Array to Object
ECMAScript 6 introduces the easily polyfillable Object.assign
:
The
Object.assign()
method is used to copy the values of all
enumerable own properties from one or more source objects to a target
object. It will return the target object.
Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}
The own length
property of the array is not copied because it isn't enumerable.
Also, you can use ES8 spread syntax on objects to achieve the same result:
{ ...['a', 'b', 'c'] }
For custom keys you can use reduce:
['a', 'b', 'c'].reduce((a, v) => ({ ...a, [v]: v}), {})
// { a: "a", b: "b", c: "c" }
How to convert an array to object in PHP?
This one worked for me
function array_to_obj($array, &$obj)
{
foreach ($array as $key => $value)
{
if (is_array($value))
{
$obj->$key = new stdClass();
array_to_obj($value, $obj->$key);
}
else
{
$obj->$key = $value;
}
}
return $obj;
}
function arrayToObject($array)
{
$object= new stdClass();
return array_to_obj($array,$object);
}
usage :
$myobject = arrayToObject($array);
print_r($myobject);
returns :
[127] => stdClass Object
(
[status] => Have you ever created a really great looking website design
)
[128] => stdClass Object
(
[status] => Figure A.
Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution.
)
[129] => stdClass Object
(
[status] => The other day at work, I had some spare time
)
like usual you can loop it like:
foreach($myobject as $obj)
{
echo $obj->status;
}
Convert array of objects into a single object
You were on the right track with your answer, but you need to check if acc
already has a key of item.name
:
const data = [{
name: "abc",
value: "1"
},
{
name: "xyz",
value: "2"
},
{
name: "abc",
value: "3"
},
{
name: "abc",
value: "4"
},
{
name: "xyz",
value: "5"
},
]
const result = data.reduce((acc, { name, value }) => ({
...acc,
[name] : [...(acc[name] || []), value]
}), {})
console.log(result)
How to convert an Array to Array of objects with same keys in Javascript?
You can do this using Array.map
, which allows you to specify a function that returns a new item to replace in the array.
arr.map(o => ({ name: o }))
Here's one without fancy arrow function shorthand, just in case you are confused.
arr.map(function(o) {
return {
name: o,
}
})
Convert array to object keys
try with Array#Reduce
const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)
Convert Array to Object for table Schema
I think you could do as follows
const array = ["Foo", "Bar", "John"];
Object.fromEntries(
array
.map((el, index) => {
return [el, {name: el, index, type: el.constructor.name}]
})
)
Here you can find an example snippet:
const array = ["Foo", "Bar", "John"];
const result = Object.fromEntries(
array
.map((el, index) => {
return [el, {name: el, index, type: el.constructor.name}]
})
);
console.log(result);
Related Topics
What Is Event Bubbling and Capturing
Use Dynamic Variable Names in JavaScript
How to Calculate Number of Days Between Two Dates
Understanding Unique Keys For Array Children in React.Js
How to Sort an Array of Objects by Multiple Fields
What Is the Reason to Use the 'New' Keyword At Derived.Prototype = New Base
How to Pass a Parameter to a Settimeout() Callback
All Falsey Values in JavaScript
What Does the Construct X = X || Y Mean
Getting the Id of the Element That Fired an Event
How to Detect If Multiple Keys Are Pressed At Once Using JavaScript
How to Get a Timestamp in JavaScript
Get a Random Item from a JavaScript Array
How to Convert Decimal to Hexadecimal in JavaScript
Reactjs Component Names Must Begin With Capital Letters