Skip to content

Instantly share code, notes, and snippets.

@undfine
Created August 10, 2024 16:41
Show Gist options
  • Select an option

  • Save undfine/8b93430083673a0670f07fc7f64133f5 to your computer and use it in GitHub Desktop.

Select an option

Save undfine/8b93430083673a0670f07fc7f64133f5 to your computer and use it in GitHub Desktop.
Convert array of objects to a single object
function arrayToObject(arr) {
return arr.reduce((acc, curr) => {
acc[curr.name] = curr.value;
return acc;
}, {});
}
// Example usage:
const inputArray = [
{ name: "firstName", value: "John" },
{ name: "lastName", value: "Doe" },
{ name: "age", value: 30 },
];
const result = arrayToObject(inputArray);
console.log(result);
// Output: { firstName: 'John', lastName: 'Doe', age: 30 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment