-1

I have an array containing a further 2 arrays of objects and I wanted to turn it into a single array of objects. I have posted the current code and result I want

I have tried the concat method but possibly implementing wrong?

var code = [
  [{Adults:"1", Price: "14.50"}],
  [{Adults:"1", Price: "20.50"}]
]

var result = [
  {Adults: "1", Price: "14.50" },
  {Adults: "1", Price: "20.50"}
]
Jblm
  • 1
  • 2

1 Answers1

1

You can use flat

var code = [
  [{Adults:"1", Price: "14.50"}],
  [{Adults:"1", Price: "20.50"}]
]

let op = code.flat()

console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 3
    Note that `flat` is not supported by sh`ie`t and edge – Void Spirit May 31 '19 at 18:57
  • @AntihypeBird edge will eventually start using chromium and you can use polyfill easily, it given [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat#Polyfill) – Code Maniac May 31 '19 at 18:58
  • it will for sure, just a note for current versions :) – Void Spirit May 31 '19 at 19:00
  • @CodeManiac just because edge will support eventually doesn't negate the fact that lots of enterprise users use old versions of IE still :) – John Ruddell May 31 '19 at 19:01
  • @JohnRuddell we have polyfill for such cases, there are lots of features which are not supported by `IE` – Code Maniac May 31 '19 at 19:03
  • As a top user under the js tag, you should put your efforts into curating this site instead of answering obvious dupes. – Jonas Wilms May 31 '19 at 19:30