将多个对象属性值合并为一个 [英] Concatenate multiple object property values into one

查看:65
本文介绍了将多个对象属性值合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的对象具有以下结构:

If I have object with following structure:

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

假设属性名称是固定的,并且并非总是按此顺序排列,那么将这个对象转换为以下对象的最优雅的方法是什么:

Assuming that property names are fixed and not always in this order, 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(', ')
    }
}

这篇关于将多个对象属性值合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆