Array receiving two objects

Asked

Viewed 151 times

1

I have two objects and wanted to create an array this way =>

[{"loj_codigo":2,"loj_fantasia":"teste 1"},{"loj_codigo":1,"loj_fantasia":"teste 2";}]}

I am receiving the following objects =>

Object {1: "1", 2: "2"}
Object {1: "teste 1", 2: "teste 2"}
  • What is the relationship between objects? Does it look crossed or is it sequential? Key 1 must be what in the new object?

  • @Sergio this numeric index there in the objects is correct?

  • @durtto yes, you can use numbers as key/key on objects. It is limited as you cannot do obj.3 = 'foo';, in that case it has to be `obj[3] = 'foo';``

  • Code 2 refers to the "test 1" store and code 1 to "test 2"? For the objects you are receiving it is difficult to make this link.

1 answer

1


From what I understand, this should solve your question.

var obj1 = {1:"1", 2:"2"};
var obj2 = {1:"teste 1", 2:"teste 2"};
var arr  = [];

for( i in obj1 ){
    arr.push( JSON.parse('{"loj_codigo":'+i+',"loj_fantasia":"'+obj2[i]+'"}') );
}
  • Thanks @Givanildo, I managed to resolve the issue.

Browser other questions tagged

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