Trying to convert an array of arrays (where the inner arrays only have two values stored) into an object.
This is what I've got so far:
function fromListToObject(array) {
var obj = {};
for (i in array) {
obj[array[i[0]]] = array[i[1]];
};
return obj
};
A1=[['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
console.log(fromListToObject(A1));
Trying to convert an array of arrays (where the inner arrays only have two values stored) into an object.
This is what I've got so far:
function fromListToObject(array) {
var obj = {};
for (i in array) {
obj[array[i[0]]] = array[i[1]];
};
return obj
};
A1=[['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
console.log(fromListToObject(A1));
But it's giving me an object where the keys are the array pairs, and the values are "undefined."
Halp?
Share Improve this question asked Oct 27, 2017 at 4:39 Advokaten01Advokaten01 651 silver badge4 bronze badges 1-
1
Not an answer, but just a side note: you shouldn't use
for...in
loops to iterate over an array, they're meant for objects. Use a normal for loop.for (i = 0; i < array.length; i++)
- MDN web docs on this subject. – dwsndev Commented Oct 27, 2017 at 4:44
6 Answers
Reset to default 3With ES6, you could use
Object.assign
for a new object,- spread syntax
...
for taking an array as parameters, Array#map
for returning single objects in an array,- destructuring assignment for getting parts of an array/object and
- puted property names for a named value pair for an object.
var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]],
object = Object.assign(...array.map(([k, v]) => ({ [k]: v })));
console.log(object);
You can use Array.prototype.reduce()
as follows:
const array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
const object = array.reduce((result, [key, value]) => {
result[key] = value;
return result;
}, {});
console.log(object);
Change your code to:
function fromListToObject(array) {
var obj = {};
for (i in array) {
obj[array[i][0]] = array[i][1];
};
return obj
};
A1=[['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
console.log(fromListToObject(A1));
You wrote wrong syntax when get array value.
The Object.fromEntries
method does this simply
var array = [['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
var obj = Object.fromEntries(array);
console.log(obj);
More about Object.fromEntries()
- https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
Try this:
function fromListToObject(array) {
return Object.assign.apply({}, array.map(function(subarray) {
var temp = {}
temp[subarray[0]] = subarray[1]
return temp
})
)
};
A1=[['make', 'Ford'], ['model', 'Mustang'], ['year', 1964]];
console.log(fromListToObject(A1));
It flattens all the arrays into one big object with these key-value pairs:
{ make: 'Ford', model: 'Mustang', year: 1964 }
If you're using TS and having this kind of errror message: "A spread argument must either have a tuple type or be passed to a rest parameter.ts(2556)"
Since you're using the spread operator, and in this case you're passing more than one argument to the target object (1st argument of the assing's method) you must "encapsulate" your array first.
like this:
const object = Object.assign([...yourArray].map( ([k, v]) => ( {[k]: v} ) ));
It should work. You can check more on this other topic: A spread argument must either have a tuple type or be passed to a rest parameter React