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" }
Answer from Oriol on Stack OverflowECMAScript 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" }
With a function like this:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
rv[i] = arr[i];
return rv;
}
Your array already is more-or-less just an object, but arrays do have some "interesting" and special behavior with respect to integer-named properties. The above will give you a plain object.
edit oh also you might want to account for "holes" in the array:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
In modern JavaScript runtimes, you can use the .reduce() method:
var obj = arr.reduce(function(acc, cur, i) {
acc[i] = cur;
return acc;
}, {});
That one also avoids "holes" in the array, because that's how .reduce() works.
How to transform an array of numbers into objects?
Convert array to object
Array of objects, best way to do it.
Learning arrays and object arrays
you only have 1 object in your Array, and that object has 2 additional objects within it (ssm & notSSM).
Here is your current structure:
globalDiskSpaceParameters[0].ssm.Test.... value2
globalDiskSpaceParameters[0].ssm.Test1...... Value2
globalDiskSpaceParameters[0].notSSM.Testing..... value3
globalDiskSpaceParameters[0].notSSM.Testing1..... value4
If you changed it to:
var globalDiskSpaceParameters = [{Test: "value2", Test1: "Value2"}, {Testing: "value3", Testing1: "value4"}];
you would have 2 objects in your array, but they would both have different properties...
globalDiskSpaceParameters[0].Test.... value2
globalDiskSpaceParameters[0].Test1...... Value2
globalDiskSpaceParameters[1].Testing..... value3
globalDiskSpaceParameters[1].Testing1..... value4
More on reddit.comVideos
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) => [key, obj[key]]);
console.log(result);
The best way is to do:
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.entries(obj);
console.log(result);
Calling entries, as shown here, will return [key, value] pairs, as the caller requested.
Alternatively, you could call Object.values(obj), which would return only values.
Solved: so the problem was I did not understand what everyone was saying by returning my array/result. I knew I was close, kind of, but was missing the final pieces of the puzzle until I saw the working code, then it made sense.
This is the solution I went with and it passed the check:
function transformToObjects(numberArray) {return numberArray.map((num) => { let obj = { val: num }; console.log(obj); return obj; }); }
I'm still unsure why the console.log(obj) does not log this, but I presume it was used for testing and has something to do with the return keyword that makes it not display the obj variable.
____________________________________________________
Hi, I am stuck on an exercise from a course on Udemy and I'm not sure how to solve it.
I need to put through [1,2,3] in my argument and return [ Object({ val: 1 }), Object({ val: 2 }) ]
Below is my attempt at it:
function transformToObjects(numberArray) {
// Todo: Add your logic
// should return an array of objects
//console.log(numberArray);
numberArray.map((num) => {
let numObj = { val: num };
return numObj;
});
}
transformToObjects([1, 2, 3]);Note: I must use map() for this exercise. Any thoughts/help on this?