Javascript将对象合并为一个数组

时间:2018-11-12 13:23:47

标签: javascript arrays object concatenation javascript-objects

有人可以帮我提供一些关于如何将对象数组转换为单个数组的代码的方法吗,因为我有一个setInterval,每隔2分钟就会调用api并将结果推送到变量prod,现在变量已变为一个对象数组,我只想使其成为单个数组。这就是产品的样子

var prod = [
    [{
        "order_id": "283828",
        "product_id": "126354",
        "order_qty": "1",
        "customer_note": "",
        "out_of_stock": "pick_specific_replacement",
        "status": "0",
        "id": "317",
        "price": "98.99"
    }],
    [{
        "order_id": "283828",
        "product_id": "126354",
        "order_qty": "1",
        "customer_note": "",
        "out_of_stock": "pick_specific_replacement",
        "status": "0",
        "id": "317",
        "price": "98.99"
    }]
]

我想把它变成

var prod = [
    {
        "order_id": "283828",
        "product_id": "126354",
        "order_qty": "1",
        "customer_note": "",
        "out_of_stock": "pick_specific_replacement",
        "status": "0",
        "id": "317",
        "price": "98.99"
    },
    {
        "order_id": "283828",
        "product_id": "126354",
        "order_qty": "1",
        "customer_note": "",
        "out_of_stock": "pick_specific_replacement",
        "status": "0",
        "id": "317",
        "price": "98.99"
    }
]

4 个答案:

答案 0 :(得分:2)

这应该有效:

prod = prod.map(a => a[0]);

而不是修补结果,而应避免问题的根源。我假设您的代码中某处有prod.push(something)之类的东西。应将其更改为prod.push(something[0])。可以肯定的是,您需要共享用于填充prod数组的代码。

答案 1 :(得分:1)

您可以将.concat()与扩展语法一起使用:

let data = [
    [{"order_id": "283828", "product_id": "126354", "order_qty": "1", "customer_note": "", "out_of_stock": "pick_specific_replacement", "status": "0", "id": "317", "price": "98.99"}],
    [{"order_id": "283828", "product_id": "126354", "order_qty": "1", "customer_note": "", "out_of_stock": "pick_specific_replacement", "status": "0", "id": "317", "price": "98.99"}]
];

let result = [].concat(...data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

不是将每个API调用推送到数组,而是将结果连接到数组;

prod.push(response);

会变成:

prod = prod.concat(response);

答案 3 :(得分:0)

有几种不同的方法可以实现这一目标:

使用Array.prototype.reduce

const data = [[{a: 1}],[{b: 2}]];
const result = data.reduce((r,c) => [...r, ...c])
console.log(result)

使用Array.prototype.reduceArray.prototype.concat

const data = [[{a: 1}],[{b: 2}]];
const result = data.reduce((r,c) => r.concat(c))
console.log(result)

仅将Array.prototype.concatES6 spread syntax一起使用:

const data = [[{a: 1}],[{b: 2}]];
const result = [].concat(...data)
console.log(result)

使用recursionArray.prototype.reduce进行递归展平,而不是仅进行一级展平:

const data = [[{a: 1}],[{b: 2}], [[{c: 1}]], [[[{d: 1}]]]];
const flattenDeep = (arr, accumulator = []) =>
   arr.reduce((r,c) => 
   (Array.isArray(c) ? flattenDeep(c, r) : r.push(c), r), accumulator)

console.log(flattenDeep(data))

使用lodash flattenflattenDeep

const data = [[{a: 1}],[{b: 2}]];
console.log('flatten: ', _.flatten(data))

const data2 = [[{a: 1}],[{b: 2}], [[{c: 1}]], [[[{d: 1}]]]];
console.log('flattenDeep: ', _.flattenDeep(data2))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>