I want to achieve this:
var keys = ['name', 'description']
var obj = {
id: 1,
name: 'Test',
description: 'Lorem ipsum dolores',
moreKeysHere: 'moreValues'
}
console.log(obsKeysToString(obj, keys, '-'))
Result: Test - Lorem ipsum dolores
I have some solution with for
loop, and some stirring operations and so on but I am sure there's a better way..
Another answer to show 1-liners for joining keys
, or values
, or both
:
Join Keys: Object.keys(obj)
const
obj = {a: '1', b: '2'},
joined = Object.keys(obj).join(',');
Join Values: Object.values(obj)
const
obj = {a: '1', b: '2'},
joined = Object.values(obj).join(',');
Join Both: Object.entries(obj)
const
obj = {a: '1', b: '2'},
joined = Object.entries(obj).join(',');
Note: Object.entries(obj)
was announced a standard by ecma in June 2017 (See link). Browser support: all except IE Desktop and Mobile Safari.
Update: Object.entries(obj)
now supports in Mobile Safari Also
Ref: developer.mozilla.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With