DEV Community

Discussion on: Cool Object methods in JavaScript

Collapse
 
avalander profile image
Avalander

What would you do with the output of entries though?

It's quite useful to convert objects to other formats, like query strings or SQL queries:

const query {
  page: 3,
  type: 'unicorn',
}

const toQueryString = query =>
  '?' + Object.entries(query)
    .map(([ key, value ]) => [ key, value ].join('=')) // Or .map(x => x.join('='))
    .join('&')

toQueryString(query) // '?page=3&type=unicorn'
Collapse
 
squigglybob profile image
squigglybob

Yep, that's nice. Object.keys(obj) always bothered me as then you needed to grab the value from the obj with the index of the key, but this way you have it there when you map etc.