0

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); 
3
  • Can u try push? Commented Apr 11, 2019 at 9:38
  • it is also not working Commented Apr 11, 2019 at 9:43
  • const mergedArr = [...firstArr, ...secArr] Commented Apr 11, 2019 at 10:17

2 Answers 2

1

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.

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

Comments

0

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

angular.extend(arraiobj1, arraiobj2); 

2 Comments

nope it is still modifying the order of the array
Is it an array or object? because, in the object, the order won't matter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.