Join two objects in another object

Asked

Viewed 97 times

-1

Eae guys wanted to know how to pick up two objects and put everything together in a third object I speak two objects because the angular is saying that they are objects and not arrays.

      var obj1 = ['1', '02', '5'];
      var obj2 = ['ana', 'fia', 'tina'];
      vm.obj3 = [{codigo: '', nome:''}];

        //deixando assim
        vm.obj3 = [{
            {codigo: '1', nome: 'ana'}, 
            {codigo: '02', nome: 'fia'}, 
            {codigo: '5', nome: 'tina'}
        }];

1 answer

3

To do this you will need to make sure that the variables obj1 and obj2 are with the same amount of items and with the order you need correct.

var obj1 = ['1', '02', '5'];
var obj2 = ['ana', 'fia', 'tina'];

var final = [];
obj1.map((o,i) => {
    final.push({codigo: obj1[i], nome:obj2[i]})
})

console.log(final)

  • vlw guy helped a lot

Browser other questions tagged

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