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);
Answer from Nenad Vracar on Stack OverflowYou 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.
javascript - How to convert an array into an object? - Stack Overflow
How to go from object to array and back again
How to transform an array of numbers into objects?
Convert array to object
Videos
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" }
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.
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?