将JavaScript对象的可变长度的对象属性连接到该对象的新属性中 [英] Concatenate object's properties of variable length of a JavaScript object into a new property of the object

查看:33
本文介绍了将JavaScript对象的可变长度的对象属性连接到该对象的新属性中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个复杂的对象结构:

I have this complicated object structure:

myObject = {
    "myObject" : [
        {
            "id" : 1,
            "parameters" : [
                {
                    "name" : "name1",
                    "special" : "xxx"
                },
                {
                    "name" : "name2",
                    "special" : "yyy"           
                }
            ]
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name3",
                    "special" : "zzz"
                }
            ]
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name4",
                    "special" : "ttt"
                },
                {
                    "name" : "name5",
                    "special" : "aaa"
                },
                {
                    "name" : "name6",
                    "special" : "zzz"
                }
            ]
        },
        ...
    ]
};

它由一系列其他对象组成,每个对象具有可变数量的 parameters .

It consists of a array of other objects, each of them having a variable number of parameters.

我的目标是将每个对象的参数 special 连接成一个新的字符串,该字符串必须作为它的新属性存储.

My goal is to concatenate the special of parameters of each object into a new string which must be stored as new property of it.

在这种情况下,结果应如下所示:

In this case, the result should look like:

myObject = {
    "myObject" : [
        {
            "id" : 1,
            "parameters" : [
                {
                    "name" : "name1",
                    "special" : "xxx"
                },
                {
                    "name" : "name2",
                    "special" : "yyy"           
                }
            ],
            "newProp" : "xxxyyy"
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name3",
                    "special" : "zzz"
                }
            ],
            "newProp" : "zzz"
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name4",
                    "special" : "ttt"
                },
                {
                    "name" : "name5",
                    "special" : "aaa"
                },
                {
                    "name" : "name6",
                    "special" : "zzz"
                }
            ],
            "newProp" : "tttaaazzz"
        },
        ...
    ]
};

我尝试过这样的事情:

forEach(arr in myObject.myObject){
    arr.parameters(forEach (i in arr.parameters.special) {
    myObject.myObject = i.concat(myObject.myObject);
  })
}

显然,它不起作用.但是我想这可能是正确的方法.

obviously, it does not work. But I guess that this could be the right approach.

有什么建议吗?

推荐答案

为每个对象使用reduce和

Use reduce and for Each

var myObject = {
    "myObject" : [
        {
            "id" : 1,
            "parameters" : [
                {
                    "name" : "name1",
                    "special" : "xxx"
                },
                {
                    "name" : "name2",
                    "special" : "yyy"           
                }
            ]
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name3",
                    "special" : "zzz"
                }
            ]
        },
        {
            "id" : 2,
            "parameters" : [
                {
                    "name" : "name4",
                    "special" : "ttt"
                },
                {
                    "name" : "name5",
                    "special" : "aaa"
                },
                {
                    "name" : "name6",
                    "special" : "zzz"
                }
            ]
        }
    ]
};

myObject.myObject.forEach(arr => {
 arr.prop = arr.parameters.reduce((res,obj)=> res+obj.special, '')
})

console.log(myObject)

这篇关于将JavaScript对象的可变长度的对象属性连接到该对象的新属性中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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