2

I'm trying to simplify a convoluted structure.
I've simplified it as much as I can and have reduced it to this:

[
  {'Cells':{'Results':[
                        {'Key':'ID','Value':123},
                        {'Key':'Color','Value':'Red'},
                        {'Key':'Direction','Value':'West'}
                      ]}},
  {'Cells':{'Results':[
                        {'Key':'ID','Value':456},
                        {'Key':'Color','Value':'Green'},
                        {'Key':'Direction','Value':'East'}
                      ]}}
]

My lodash is skills are lacking and I need help turning the above into this:

[
  {'ID':123, 'Color':'Red', 'Direction':'West'},
  {'ID':456, 'Color':'Green', 'Direction':'East'}
]

I should note that the number of keys vary by object by the way. At a minimum, it could have just the ID, but some might have more than those three in the example.

5 Answers 5

3

In plain Javascript, you could use two nested loop for it.

var array = [{ 'Cells': { 'Results': [{ 'Key': 'ID', 'Value': 123 }, { 'Key': 'Color', 'Value': 'Red' }, { 'Key': 'Direction', 'Value': 'West' }] } }, { 'Cells': { 'Results': [{ 'Key': 'ID', 'Value': 456 }, { 'Key': 'Color', 'Value': 'Green' }, { 'Key': 'Direction', 'Value': 'East' }] } }],
    condensed = array.map(function (a) {
        var o = {};
        a.Cells.Results.forEach(function (b) {
            o[b.Key] = b.Value;
        });
        return o;
    });

console.log(condensed);

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

Comments

1

A solution using Lodash:

var result = _.chain(data)
    .map('Cells.Results')
    .map(res => _.zipObject(_.map(res,"Key"), _.map(res,"Value")))
    .value();

Comments

1

Something like this should do (although I'm not sure that it's better than plain javascript)

_.chain(array)
 .pluck('Cells')
 .pluck('Results')
 .map(function (value) { return _.object(_.map(value, _.values)); })
 .value()

2 Comments

This doesn't output the correct format unfortunately. Each array object only has the last key in it with no value (???)
For the sample input you provided the above code outputs exactly what you gave.
1

Here's another alternative using lodash fp.

var result = map(compose(
  mapValues('Value'),
  keyBy('Key'),
  iteratee('Cells.Results')
))(data);

var data = [{
  'Cells': {
    'Results': [{
      'Key': 'ID',
      'Value': 123
    }, {
      'Key': 'Color',
      'Value': 'Red'
    }, {
      'Key': 'Direction',
      'Value': 'West'
    }]
  }
}, {
  'Cells': {
    'Results': [{
      'Key': 'ID',
      'Value': 456
    }, {
      'Key': 'Color',
      'Value': 'Green'
    }, {
      'Key': 'Direction',
      'Value': 'East'
    }]
  }
}];

var { compose, map, iteratee, keyBy, mapValues } = _;

var result = map(compose(
  mapValues('Value'),
  keyBy('Key'),
  iteratee('Cells.Results')
))(data);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.fp.js"></script>

Comments

1

Yet another answer, using map and reduce

    var initial_array=[
      {'Cells':{'Results':[
                        {'Key':'ID','Value':123},
                        {'Key':'Color','Value':'Red'},
                        {'Key':'Direction','Value':'West'}
                      ]}},
      {'Cells':{'Results':[
                        {'Key':'ID','Value':456},
                        {'Key':'Color','Value':'Green'},
                        {'Key':'Direction','Value':'East'}
                      ]}}
    ];

     function mergeObj(accumulator,current){
       accumulator[current['Key']]=current['Value'];
       return accumulator;  
     }

     function getResults(obj){
       return obj.Cells.Results.reduce(mergeObj,{});
     }

     var transformed_array=_(initial_array).map(getResults).value();
     console.log(transformed_array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>

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.