0

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

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

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

I tried something like this:

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.

Any suggestions?

0

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.