How to insert data from one array into another for object type

Asked

Viewed 40 times

-1

I have the array as follows:

let arrTeste = [ [ '1', '10' ], [ '2', '30' ], [ '3', '40' ] ]

How would it be to take the data from the sub array and turns to something like:

let Obj={ 
          { item:'1', quantidade:'10'}, 
          { item:'2', quantidade:'30'}, 
          { item:'3', quantidade:'40'} 
}

or

let obj=[ 
          { item:'1', quantidade:'10'}, 
          { item:'2', quantidade:'30'}, 
          { item:'3', quantidade:'40'} 
]
  • 2

    What you tried so far and what errors your algorithm gave. Put a [MCVE] presenting your attempt to address the problem.

  • Dear good afternoon, thank you for trying to help, but that part was the only doubt I was having... what the colleague posted then clarified.

  • thank you Augusto.

1 answer

1


Can map to a new variable the data contained in a array and taking each position and turning into objects with key and value and for this use the method map of array to recreate this array object, example:

const arrTeste = [
  ['1', '10'],
  ['2', '30'],
  ['3', '40']
];

const obj = arrTeste.map((item) => {
  return {'item': item[0], 'quantidade': item[1]}
});

console.log(obj);

the other form which is an object and within that object several other objects is not correct as it does not have the key and also will not return a array information.

  • 1

    Thank you for the time in answering, it was very helpful

  • If it is useful, tick as the answer to your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.