JavaScript How to extract selected Object properties to the newly created property with Array type?

I have an JS collection (array of objects with few properties).

let xyz = [{
    categoryId: 1,
    categoryName: 'Test',
    programId: 112,
    programName: 'Program Test',
    value: 5050,
    value2: 1000
},
{
    categoryId: 2,
    categoryName: 'Test',
    programId: 112,
    programName: 'Program Test',
    value: 5050,
    value2: 1000
},
{
    categoryId: 3,
    categoryName: 'Test',
    programId: 112,
    programName: 'Program Test',
    value: 5050,
    value2: 1000
}]

How to transform such collection using LODASH to the following structure (added values property as an array of values coming from 'value' and 'value2':

let abc = [{
        categoryId: 1,
        categoryName: 'Test',
        programId: 112,
        programName: 'Program Test',
        values: [5050,1000]
    },
    ...
    ]
Answer:1

You need to transform your array using basic map operation. Using lodash:

let abc = _.map(xyz, function(el) {
    return {
        categoryId: el.categoryId,
        categoryName: el.categoryName,
        programId: el.programId,
        programName: el.programName,
        values: [el.value, el.value2]
    };
});

But I would rather follow "You Dont Need Lodash" trend here by using Array.prototype.map function

Answer:2

you can do it with _.merge

var res = _.map(xyz, function(item) {
    return _.merge(
        {}, // to avoid xyz mutations
        _.omit(item, ['value', 'value2']), // remove useless keys
        {values: _.at(item, ['value', 'value2'])} // get needed values
    );
});
Answer:3

Functions that were polyfilled with core-js (for example, babel-polyfill) appear as native. Promise.race.toString() produces: function race() { [native code] } So does Object.values.toString(). ...

Functions that were polyfilled with core-js (for example, babel-polyfill) appear as native. Promise.race.toString() produces: function race() { [native code] } So does Object.values.toString(). ...

  1. core-js babel
  2. core-js babel-polyfill
  3. core-js babel 7
  4. core-js babel webpack
  5. core js babel config
  6. core-js babel-loader
  7. core-js vs babel-polyfill
  8. core-js/stable babel
  9. core-js vs babel
  10. core js 3 babel
  11. babel corejs option
  12. babel core-js version
  13. babel core-js missing
  14. babel core js 2
  15. babel core-js example
  16. babel core-js promise
  17. babel core-js/shim
  18. js babel-core/register

Example HTML page: <div> <ul> <li> 1 </li> <li> 2 </li> </ul> </div> Then in a ajax request I get <div> <ul> <li> ...

Example HTML page: <div> <ul> <li> 1 </li> <li> 2 </li> </ul> </div> Then in a ajax request I get <div> <ul> <li> ...

I am trying to format a number upon input, where you can only insert valid numbers. I have everything working except for the decimal. The input box allows me it insert as many decimals as I would like,...

I am trying to format a number upon input, where you can only insert valid numbers. I have everything working except for the decimal. The input box allows me it insert as many decimals as I would like,...

  1. input allow only numbers
  2. input allow only alphanumeric
  3. input allow only numbers and dot
  4. input allow only numbers angular
  5. input allow only letters
  6. input allow only 2 decimal places
  7. input allow only numbers jquery
  8. input allow only numbers and decimal
  9. input allow only numbers react
  10. input allow only images
  11. input allow only integers
  12. input allow only numbers angular 2
  13. input allow only letters and numbers
  14. input allow only digits
  15. input allow only whole numbers
  16. input allow only numbers and one dot
  17. input allow only positive numbers
  18. input allow only one number
  19. input allow only numbers regex
  20. input allow only two numbers

updated. I have stored my data in an array of tuples: called $scope.unique. basically it looks like this: [["Chris Ivory", "Alfred Morris"], ["CJ", "DJAX"], ["DJ", "CX"]["Willie", "Fred]]. The ...

updated. I have stored my data in an array of tuples: called $scope.unique. basically it looks like this: [["Chris Ivory", "Alfred Morris"], ["CJ", "DJAX"], ["DJ", "CX"]["Willie", "Fred]]. The ...

  1. ng repeat with arrays
  2. repeat arrays matlab
  3. repeat arrays python
  4. repeat arrays
  5. ng-repeat with two arrays
  6. ng-repeat with multiple arrays
  7. ng-repeat with 2 arrays
  8. ng-repeat with nested arrays