0

I am working in jQueryMobile and PhoneGap.

I Have 2 objects: eup and gld. The length of eup is 22, and the length of gld is 6.

I have tried:

//common.push(eup,gld);
//common.join(eup,gld);
//common.concat(eup,gld);

alert(common.length)  // 2

When I check common.length, it's 2.

But for my logic I need it as 1. That means merge eup and gld and shows its length as 1. I got the result of eup and gld from two different APIs and it's in JSON format. The main thing is the tags of both objects are identical. So I think it should be possible to merge these values as one and show its length as ONE.

Is there any solution for this????

10
  • Which is the structure of those arrays? Commented May 31, 2013 at 9:23
  • why 1? length of final array should be 28 , right ? Commented May 31, 2013 at 9:26
  • Like this { "company_data": { "doors": "2-4 Doors", "transmission": "Manual", "aircondition": "A/C", "CarSize": "xxxx", "type": "ss", "total_price": "1124.16" } } Commented May 31, 2013 at 9:29
  • are you trying to merge two objects or arrays ? Commented May 31, 2013 at 9:31
  • @tracevipin I need this : common.length = 1 but now I got common.length = 2 (eup,gld) Commented May 31, 2013 at 9:31

1 Answer 1

4

It sounds like you want to extend an object, not concatenate an array.

var common = $.extend({}, eup, gld);

However, this will overwrite values with common keys. To merge without overwriting, the solution is explained at Merge JS objects without overwriting.

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

6 Comments

@Derek : Actually my requirement is; In my application; I have two or more api’s . In its response as JSON format; there are lot of Tags are given in the Result but my required details are given in a Tag known as "company_data". so now I’m collecting company_data details to an array in each respond file.. var eup =new Array(); var gld=new Array(); var common =new Array() When I got the result from each API calling; its company_data are set to its array value.
@Amir, I apologize, but I cannot understand what you are saying. Can you perhaps post what you expect common to look like? Seeing the result will help eliminate confusion and give you a working solution. (BTW, don't use new Array(); use [] instead.)
@Derek Sorry.. I will explain my requirement once again. In my list there is 2 different companies (Actually its value is dynamic depends up on different countries); I got 2 companies details in JSON format. Here I have 2 API for 2 Companies. company1.php and company2.php And I got 2 Different JSON data. But here the JSON both are same format that means Tags are same in Both JSON and only the values are different.Then I need to merge the values of 2 different JSON data to a common array.For this; var eur =new Array(); var gld=new Array(); var common =new Array();
1st api result set to Array "eru" and 2nd api result set to Array "gld".. currently this purpose is OK. To get the values as one ; I tried to set the values of two different arrays "eru and gld" to a Common Array. For this method; I tried; common.push(eup,gld); common.join(eup,gld); common.concat(eup,gld); alert(common.length) In this case; I got the common.length=2. But Actually I need the length "common.length=1". The concept is I need to set the values of both Arrays in to Common Array and shows as one... :(
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.