I'm working with an object similar to the following (although this object has 4 key/value pairs, my actual object can have any number of them):
var myObj = {
action: 'open',
card: 'Comment card name',
id: '54AEF364',
url: 'www.myurl.com'
};
I'm trying to add the keys and values into a single string, separated by ":" and "|", like this:
var myString = 'action:open|card:Comment card name|id:54AEF364|url:www.myurl.com'
I'm attempting to do this using a for...in loop:
for (var i in myObj) {
var myString += i + ':' + myObj[i] + '|';
}
I'm able to get most of what I need, with 2 exceptions:
- 'undefined' occurs before the first property name
- for the last key/value pair, there shouldn't be a "|" after the final value, since this is the end of the string
How can I accomplish this?