Skip to content

Instantly share code, notes, and snippets.

@naosim
Created October 3, 2024 21:33
Show Gist options
  • Save naosim/f564878251f7e7b39e07e7da80e8625c to your computer and use it in GitHub Desktop.
Save naosim/f564878251f7e7b39e07e7da80e8625c to your computer and use it in GitHub Desktop.
fromEntries
const text = "name=naosim,point=10,state=4"
const result = text.split(",").reduce((memo, v) => {
const [key, value] = v.split("=");
return {...memo, [key]:value};
}, {})
console.log(result);
const result2 = Object.fromEntries(text.split(",").map(v => v.split("=")))
console.log(result2);
/**
* 出力
* { name: 'naosim', point: '10', state: '4' }
* { name: 'naosim', point: '10', state: '4' }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment