Home > database >  concat strings from nested objects arrays
concat strings from nested objects arrays

Time:11-09

i need to concat all the 'externalId'(inside prod obj) "id" (inside sup array) "name" (inside product obj). What would be the best way to do that? I've tried with map and reduce but I wasn't successful. Any help will be appreciated.

const jsonResultKeys = ['AAA', 'BBB', 'CCC']


const items = [];


jsonResultKeys.forEach(k => {
            const item = Jsonresult.items[k];
            items.push({
                description: item.product.name   ':'   item.product.sup[0]   ':'  item.product.sup[0].prod.externalId ,
            })
        });

the output expected for this example:

[ 
{ description: '4444:2:product1'},
{ description: '3333:2:product2'},
{ description: '2222:1:product3'}
]

the json object:

const Jsonresult = {
        items: {
          'AAA': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: **4444**
                    },
                    id: **2**
                }],
                name: "**product 1**"
            },
            total: 9.84,
            quantity: 1,
            price: 15,
            updatedAt: '2021-02-11T17:25:22.960-03:00'
          },
          'BBB': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: **3333**
                    },
                    id: **2**
                }],
                name: "**product 2**"
            },
            total: 9.84,
            quantity: 1,
            price: 15,
            updatedAt: '2021-02-11T17:25:22.960-03:00'
          },
          'CCC': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: **2222**
                    },
                    id: **1**
                    
                }],
                name: "**product 3**"
            },

          }
        },
    }

CodePudding user response:

You could do the following, Map over the results and return an object, this will create an array of objects.

You wont even need the keys.

const map = Object.values(Jsonresult.items).map((item) => {
    return {
        description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`,
    };
});
[
  { description: '4444:2:product 1' },
  { description: '3333:2:product 2' },
  { description: '2222:1:product 3' }
]

CodePudding user response:

The Array#map() method is the most logical method to use - see @MichaelMano's solution - but if you have to use .push() to populate items (const) then stick with .forEach() as follows:

 Object.values( Jsonresult.items ).forEach(item => {
    items.push( {description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`} );
 });

DEMO

Show code snippet

const items = [];

const Jsonresult = {
        items: {
          'AAA': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: 4444
                    },
                    id: 2
                }],
                name: "product 1"
            },
            total: 9.84,
            quantity: 1,
            price: 15,
            updatedAt: '2021-02-11T17:25:22.960-03:00'
          },
          'BBB': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: 3333
                    },
                    id: 2
                }],
                name: "product 2"
            },
            total: 9.84,
            quantity: 1,
            price: 15,
            updatedAt: '2021-02-11T17:25:22.960-03:00'
          },
          'CCC': {
            createdAt: '2021-02-11T17:25:22.960-03:00',
            product: {
                sup: [{
                    prod: {
                        externalId: 2222
                    },
                    id: 1
                    
                }],
                name: "product 3"
            },

          }
        },
    };
    
 Object.values( Jsonresult.items ).forEach(item => {
      items.push( {description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`} );
 });
 
    
    console.log( items );
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related