JavaScript:将多个Arrays对象合并到单个对象Array

时间:2018-02-11 11:27:42

标签: javascript arrays merge

我在变量

中收到以下数据
let data = [
    [{option: “foo0", value: “bar0”}],
    [{option: “foo1", value: “bar1”}],
    [{option: “foo2", value: “bar2”}],
    [{option: “foo3", value: “bar3”}],
 ]

我需要将响应更改为

[
    {option: “foo0", value: “bar0”},
    {option: “foo1", value: “bar1”},
    {option: “foo2", value: “bar2”},
    {option: “foo3", value: “bar3”},
]

我怎么能在JS中做到这一点

2 个答案:

答案 0 :(得分:5)

试试这个:

let data = [
  [{
    option: "foo0",
    value: "bar0"
  }],
  [{
    option: "foo1",
    value: "bar1"
  }],
  [{
    option: "foo2",
    value: "bar2"
  }],
  [{
    option: "foo3",
    value: "bar3"
  }],
];

let result = data.map(v => v[0]);

console.log(result);

答案 1 :(得分:3)

您可以按spreading将数组展平为concat

return DB::select(DB::raw('call store_procedure_function(?)', [$parameter]))