将JavaScript对象的可变长度的对象的属性连接到对象的新属性中

时间:2018-04-26 13:06:39

标签: javascript arrays object

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

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

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

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

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);
  })
}
显然,它不起作用。但我想这可能是正确的做法。

有什么建议吗?

4 个答案:

答案 0 :(得分:5)

您可以使用Array#forEach遍历对象,然后使用Array#mapArray#join根据参数值构建字符串,如下所示:



const 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(item => {
  item.newProp = item.parameters.map(p => p.special).join('');
});

console.log(myObject);




答案 1 :(得分:1)

使用reduce和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)




答案 2 :(得分:1)

您可以像这样使用.map().reduce()



let 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"}]}];

let result = myObject.map(
  o => (o.newProp = o['parameters'].reduce((a, o) => a + o['special'], ""), o)
);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 3 :(得分:1)

另一种方法是使用嵌套的地图函数:

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.map(x => {
  x.newProp = x.parameters.map(p => p.special).join('');
  return x;
})

console.log(myObject);