Skip to content Skip to sidebar Skip to footer

Concatenate Object's Properties Of Variable Length Of A Javascript Object Into A New Property Of The Object

I have this complicated object structure: myObject = { 'myObject' : [ { 'id' : 1, 'parameters' : [ { 'name'

Solution 1:

You can loop through the object using Array#forEach and then construct the string based on parameter values using Array#map and Array#join, like this:

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);

Solution 2:

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)

Solution 3:

You can use .map() and .reduce() like this:

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; }

Solution 4:

One more way is to use nested map functions:

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);

Post a Comment for "Concatenate Object's Properties Of Variable Length Of A Javascript Object Into A New Property Of The Object"