0

I have a nested Object: (Example of the form)

const result = { '1':
   { 'a':
      [ { id: '1',
          sndid: 'a',
          type: 'ios',
          value: '55',
          weight: 2
        },
        { id: '1',
          sndid: 'a',
          sumvalue: 2 },
        { id: '1',
          sndid: 'a'},
        { id: '1',
          sndid: 'a',
          calcsum: '3' } ],
     '2':
      [ { id: '1',
          sndid: 'b',
          type: 'ios',
          value: '55',
          weight: 3
         },
        { id: '1',
          sndid: 'b',
          sumvalue: 3 },
        { id: '1',
          sndid: 'b'},
        { id: '1',
          sndid: 'b',
          calcsum: '7' } ] },
  '3':
   { 'a':
      [ { id: '3',
          sndid: 'a',
          type: 'ios',
          value: '55',
          weight: 23},
        { id: '3',
          sndid: 'a',
          sumvalue: 4 },
        { id: '3',
          sndid: 'a',
          sumvalue_old: '4' },
        { id: '3',
          sndid: 'a',
          calcsum: '5' } ] },    
}

const assignedobject = {};

 _.forEach(result, (values) => {
    _.forEach(values, (vals) => {
      _.forEach(vals, (val) => {
      _.assign(assignedobject, val);
      });
    });
  });

console.log(assignedobject)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

With the Values in this Object I want to calc multiple values. Before that I want to transforme this Object into a (more readable) Object.

Every "id" have multiple "sndid". So I want to group the Object by "id" and "sndid".

(For example the transformed Object should have this structure)

  { id: '1',
  sndid: 'a',
  type: 'ios',
  values: '22',
  sumvalue: 8,
  weight: 20,
  calcsum: 2
  },
  { id: '1',
  sndid: 'b',
  type: 'ios',
  values: '22',
  sumvalue: 8,
  weight: 20,
  calcsum: 2
  },
  { id: '2',
  sndid: 'a',
  type: 'ios',
  values: '22',
  sumvalue: 8,
  weight: 20,
  calcsum: 2}

I tried to loop into all nested objects and assign it to a new object. But he just push the last loop into my Object. Also the code looks badly.

The result is like above the example but he only take the last object.

1 Answer 1

1

Recursive flatMap variant:

const result = { '1':
   { 'a':
      [ { id: '1',
          sndid: 'a',
          type: 'ios',
          value: '55',
          weight: 2
        },
        { id: '1',
          sndid: 'a',
          sumvalue: 2 },
        { id: '1',
          sndid: 'a'},
        { id: '1',
          sndid: 'a',
          calcsum: '3' } ],
     '2':
      [ { id: '1',
          sndid: 'b',
          type: 'ios',
          value: '55',
          weight: 3
         },
        { id: '1',
          sndid: 'b',
          sumvalue: 3 },
        { id: '1',
          sndid: 'b'},
        { id: '1',
          sndid: 'b',
          calcsum: '7' } ] },
  '3':
   { 'a':
      [ { id: '3',
          sndid: 'a',
          type: 'ios',
          value: '55',
          weight: 23},
        { id: '3',
          sndid: 'a',
          sumvalue: 4 },
        { id: '3',
          sndid: 'a',
          sumvalue_old: '4' },
        { id: '3',
          sndid: 'a',
          calcsum: '5' } ] },    
}

const extractSndid = item => {
    if (_.isArray(item)) {
        return _.map(item, i => _.assign(...i));
    }
    return extractSndid(_.values(item))
}

const assignedobject = _.flatMap(result, extractSndid);

console.log(assignedobject)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

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

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.