-1

I just want to create an object which contains all the values of an array on a sigle key .

arr1 = [{ x: 12, y:13},{ x1: 14, y2:15}];
arr2 = [{ xx: 18, yy:18},{ xx1: 17, yy2:16}];

// result = { finalObj :[{ x: 12, y:13},{ x1: 14, y2:15}],[{ xx: 18, yy:18},{ xx1: 17, yy2:16}]}

Although i can get final array by :

const finalArr = arr1.concat(arr2);

But how to get the end result . Thanks in advance .

Arpan
  • 45
  • 1
  • 8
  • 5
    so `{ finalObj : arr1.concat(arr2) }` – epascarello Aug 28 '20 at 13:25
  • Just use JSON serialisation/deserialisation. create an object with the key and having a value of an empty array.. add things to the array and then use JSON.. http://www.tutorialspark.com/javascript/JavaScript_JSON_Parsing_Serialization.php – JGFMK Aug 28 '20 at 13:27
  • 1
    the object in your exemple is not correct, it should be either `{ finalObj :[{ x: 12, y:13},{ x1: 14, y2:15}, { xx: 18, yy:18},{ xx1: 17, yy2:16}]}` either `{ finalObj :[[{ x: 12, y:13},{ x1: 14, y2:15}],[{ xx: 18, yy:18},{ xx1: 17, yy2:16}]]}` – CharybdeBE Aug 28 '20 at 13:28
  • @JGFMK [JSON](https://www.json.org/json-en.html)? Where did you find [JSON](https://www.json.org/json-en.html) (or a reference to it) in the question? o.O – Andreas Aug 28 '20 at 13:28
  • @Andreas `[{ x: 12, y:13},{ x1: 14, y2:15}]`; is as JSON array with two JSON objects. Objects are of the form `{}` ... arrays `[]`... – JGFMK Aug 28 '20 at 13:30
  • @JGFMK [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Aug 28 '20 at 13:31
  • @Andreas - all you need to know are parse/stringify... and you can get to what you need that way... – JGFMK Aug 28 '20 at 13:35

1 Answers1

1

Concat two arrays

{ finalObj: [...arr1, ...arr2] }
Hamza Zaidi
  • 672
  • 5
  • 8