0

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**"
            },

          }
        },
    }
1
  • You're missing .id at the end of item.product.sup[0]. Commented Nov 9, 2021 at 1:34

2 Answers 2

1

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

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 );

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

2 Comments

my Jsonresult obj is not ordered, so the sup[0] is not working.
Could you please explain what you mean, or post the correct JSON.
0

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' }
]

1 Comment

tks @Michael . But I need to push the return into array "items" variable. how can I do that?

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.