Nikola B. Nikola B. - 2 months ago 5x
Javascript Question

Concatenate multiple object property values into one

If I have object with following structure:

var test = {
property1: "value1",
property2: "value2",
property3: "value3",
property4: "value4",
property5: "value5"

what is the most elegant way to convert this object into following one:

var test_copy = {
prop1Copy: "value1",
propConcat: "value2, value3, value4, value5"


I don't think there's any particularly elegant way to do this.

Since your input data has a small number fixed keys there's barely any point using a loop, so this works:

function munge(o) {
    return {
        prop1Copy: o.property1,
        propConcat: [o.property2, o.property3, o.property4, o.property5].join(', ')