i have following two array objects

27A: {id: 228, swift_code: "27", field_description: "Sequence of Total", 
line_description: "1/1 (Number)(Total)", commercial: false}

31B: {id: 230, swift_code: "31C", field_description: "Date of Issue", 
line_description: "dd/mm/yyyy", commercial: false}

31C: {id: 242, swift_code: "31D", field_description: "Expiry Date/Place 
(dd/mm/yyyy)", line_description: "IN SINGAPORE", commercial: true}

my second array

67: {id: 228, swift_code: "27", field_description: "Sequence of Total", 
line_description: "1/1 (Number)(Total)", commercial: false}

44: {id: 230, swift_code: "31C", field_description: "Date of Issue", 
line_description: "dd/mm/yyyy", commercial: false}

12: {id: 242, swift_code: "31D", field_description: "Expiry Date/Place 
(dd/mm/yyyy)", line_description: "IN SINGAPORE", commercial: true}

i want to combined these two array together like bellow without modifying the order which means first array come first second array second

like bellow

27A: {id: 228, swift_code: "27", field_description: "Sequence of Total", 
line_description: "1/1 (Number)(Total)", commercial: false}

31B: {id: 230, swift_code: "31C", field_description: "Date of Issue", 
line_description: "dd/mm/yyyy", commercial: false}

31C: {id: 242, swift_code: "31D", field_description: "Expiry Date/Place 
(dd/mm/yyyy)", line_description: "IN SINGAPORE", commercial: true}

67: {id: 228, swift_code: "27", field_description: "Sequence of Total", 
line_description: "1/1 (Number)(Total)", commercial: false}

44: {id: 230, swift_code: "31C", field_description: "Date of Issue", 
line_description: "dd/mm/yyyy", commercial: false}

12: {id: 242, swift_code: "31D", field_description: "Expiry Date/Place 
(dd/mm/yyyy)", line_description: "IN SINGAPORE", commercial: true}

i have used merge and array copy but its modifying the order

angular.merge(arraiobj1, arraiobj2); 

2 Answers

1
Yvan On Best Solutions

I think you're talking about objects, not arrays. Arrays have only numeric indexes.

But JSON doesn't keep order, contrary to PHP and the like.

Javascript - maintain key order when going from object -> array

See https://stackoverflow.com/a/31409887/781153 for instance.

If you intended for this to describe an ordered set, you're not following the rules of JSON. The data happens to be ordered (because that's how character sequences work), but JSON semantics freely allow an implementation to disregard the order of name/value pairs completely when considering the data present in the input string.

As a practical matter, most JavaScript engines will usually choose to report key names in the order their properties were created. However, this behavior is not required by any specification (and is sometimes inconsistent in edge cases), and so could legally differ between engines and versions.

The best way to handle your order, is to use numeric array.

0
Googler Bharat On

You can use angular.extend(), it will append your first object into second.

angular.extend(arraiobj1, arraiobj2);