0

I need to transform this structure:

[
{"Field1": "value11", "Field2":"value21", "CField1":[{"Id": 1, "Value":"myValue11", "Flag":"Y"}, {"Id": 2, "Value":"myValue12", "Flag":"n"}]},
{"Field1": "value21", "Field2":"value22", "CField1":[{"Id": 1, "Value":"myValue21", "Flag":"Y"}, {"Id": 2, "Value":"myValue22", "Flag":"n"}]}
]

to

[
{"Field1": "value11", "Field2":"value12", "Value1": "myValue11", "Flag1":"Y", "Value2": "myValue12", "Flag2":"n"},
{"Field1": "value21", "Field2":"value21", "Value1": "myValue21", "Flag1":"Y", "Value2": "myValue22", "Flag2":"n"},
]

i found that similar solution:

tab.forEach(function (item) {
  transformedObject[item.key] = transformedObject[item.key] ? `${transformedObject[item.key]}, ${item.value}` : item.value;
});

console.log(transformedObject);

// Access the '2' property
console.log(transformedObject['2']);

but need some help to adapt it to my case

thanks

2 Answers 2

2

You could map the inner array as new objects.

const
    data = [{ Field1: "value11", Field2: "value21", CField1: [{ Id: 1, Value: "myValue11", Flag: "Y" }, { Id: 2, Value: "myValue12", Flag: "n" }] }, { Field1: "value21", Field2: "value22", CField1: [{ Id: 1, Value: "myValue21", Flag: "Y" }, { Id: 2, Value: "myValue22", Flag: "n" }] }],
    result = data.map(({ CField1, ...o }) => Object.assign(
        o,
        ...CField1.map(({ Id, ...q }, i) => Object.fromEntries(Object
            .entries(q)
            .map(([k, v]) => [k + (i + 1), v])
        ))
    ));

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

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

5 Comments

in perfomance can be better than: ratings.forEach((element, index, array) => { element.CField1.forEach((nuovo) => { element['Value' + nuovo.Id] = nuovo.Value; element['Flag' + nuovo.Id] = nuovo.Flag; }); });
you mutate elementand this contains still CField1.
correct, i can remove it. not sure about which can be faster
how many items do you have?
up to some thousands
0

You can map your data like this:

data = [
{"Field1": "value11", "Field2":"value21", "CField1":[{"Id": 1, "Value":"myValue11", "Flag":"Y"}, {"Id": 2, "Value":"myValue12", "Flag":"n"}]},
{"Field1": "value21", "Field2":"value22", "CField1":[{"Id": 1, "Value":"myValue21", "Flag":"Y"}, {"Id": 2, "Value":"myValue22", "Flag":"n"}]}
]

console.log(data.map(function(value, key) {
  return Object.entries(value).reduce(function(carry, [ key, value ]) {
    if (key !== 'CField1') {
      carry[key] = value
    } else {
      carry = Object.assign(carry, value.reduce(function(innerCarry, value, key) {
         innerCarry[`Flag${value.Id}`] = value.Flag
         innerCarry[`Value${value.Id}`] = value.Value
         return innerCarry;
      }, {}))
    }
    return carry
  }, {})
}))

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.