我在Node JS工作。我有一个JSON对象数组。在一个JSON对象中有不同的键,例如A,B,C,D,E我必须将来自不同键的对象数组合并为一个键。我提供的JSON对象如下。
{
"out-for-delivery": [
{
"order_id": 1,
"address": {
"user_id": 1,
"first_name": "Mark",
"last_name": "Stevauh",
"landmark": "Krti",
"mobile": "123456789",
"type": "work",
"city_id": 4644,
"postcode": 0,
"address_type": "address",
"locker_id": 0,
"added_on": "2018-04-30T12:34:11.000Z",
"updated_on": "2018-04-30T12:34:11.000Z",
"address_1": "Gm is",
"address_2": "Bc"
},
"status": "out-for-delivery"
},
{
"order_id": 7,
"address": {
"user_id": 5,
"first_name": "",
"last_name": "",
"landmark": "",
"mobile": "123456789",
"type": "other",
"city_id": 34,
"postcode": 0,
"address_type": "locker",
"locker_id": 2,
"added_on": "2018-05-02T09:32:38.000Z",
"updated_on": "2018-05-02T09:32:38.000Z",
"address_1": "jhdasd ahdkjh asd",
"address_2": "asfjksfdj asakjsf"
},
"status": "out-for-delivery"
},
{
"order_id": 6,
"address": {
"user_id": 5,
"first_name": "Shruti",
"last_name": "Shinde",
"landmark": "test",
"mobile": "123456789",
"type": "work",
"city_id": 45,
"postcode": 0,
"address_type": "address",
"locker_id": 0,
"added_on": "2018-05-02T09:35:25.000Z",
"updated_on": "2018-05-02T09:35:25.000Z",
"address_1": "test",
"address_2": "test"
},
"status": "out-for-delivery"
}
],
"retry-delivery": [
{
"order_id": 13,
"address": null,
"status": "retry-delivery"
},
{
"order_id": 8,
"address": {
"user_id": 5,
"first_name": "TEST",
"last_name": "Shinde",
"landmark": "test",
"mobile": "9773071307",
"type": "work",
"city_id": 666,
"postcode": 0,
"address_type": "address",
"locker_id": 0,
"added_on": "2018-05-02T09:35:25.000Z",
"updated_on": "2018-05-02T09:35:25.000Z",
"address_1": "test",
"address_2": "TEST"
},
"status": "retry-delivery"
}
],
"complete": [
{
"order_id": 2,
"address": null,
"status": "complete"
}
],
"cancelled": [
{
"order_id": 15,
"address": null,
"status": "cancelled"
}
]
}
我想将键B,C中的对象数组合并到键D.所以我该如何实现它。提前致谢。真的很抱歉,我的问题变化不大。我只改变了之前的A,B,C,D键。
答案 0 :(得分:0)
只需使用扩展语法:
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br/>
<div id="numberPanel">
</div>
</body>
</html>
&#13;
答案 1 :(得分:0)
您可以使用Object.values
获取值,并将它们连接到结果集。
var data = { A: [{ order_id: 1, address: { user_id: 1, first_name: "Mark", last_name: "Stevauh", landmark: "Krti", mobile: "123456789", type: "work", city_id: 4644, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-04-30T12:34:11.000Z", updated_on: "2018-04-30T12:34:11.000Z", address_1: "Gm is", address_2: "Bc" }, status: "A" }, { order_id: 7, address: { user_id: 5, first_name: "", last_name: "", landmark: "", mobile: "123456789", type: "other", city_id: 34, postcode: 0, address_type: "locker", locker_id: 2, added_on: "2018-05-02T09:32:38.000Z", updated_on: "2018-05-02T09:32:38.000Z", address_1: "jhdasd ahdkjh asd", address_2: "asfjksfdj asakjsf" }, status: "A" }, { order_id: 6, address: { user_id: 5, first_name: "Shruti", last_name: "Shinde", landmark: "test", mobile: "123456789", type: "work", city_id: 45, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-05-02T09:35:25.000Z", updated_on: "2018-05-02T09:35:25.000Z", address_1: "test", address_2: "test" }, status: "A" }], B: [{ order_id: 13, address: null, status: "B" }, { order_id: 8, address: { user_id: 5, first_name: "TEST", last_name: "Shinde", landmark: "test", mobile: "9773071307", type: "work", city_id: 666, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-05-02T09:35:25.000Z", updated_on: "2018-05-02T09:35:25.000Z", address_1: "test", address_2: "TEST" }, status: "B" }], C: [{ order_id: 2, address: null, status: "C" }], D: [{ order_id: 15, address: null, status: "D" }] },
array = Object.values(data).reduce((r, a) => r.concat(a), []);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
您可以使用Object.values()
获取对象的所有值,并使用spread syntax
和array#concat
获取数组。
const data = { A: [{ order_id: 1, address: { user_id: 1, first_name: "Mark", last_name: "Stevauh", landmark: "Krti", mobile: "123456789", type: "work", city_id: 4644, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-04-30T12:34:11.000Z", updated_on: "2018-04-30T12:34:11.000Z", address_1: "Gm is", address_2: "Bc" }, status: "A" }, { order_id: 7, address: { user_id: 5, first_name: "", last_name: "", landmark: "", mobile: "123456789", type: "other", city_id: 34, postcode: 0, address_type: "locker", locker_id: 2, added_on: "2018-05-02T09:32:38.000Z", updated_on: "2018-05-02T09:32:38.000Z", address_1: "jhdasd ahdkjh asd", address_2: "asfjksfdj asakjsf" }, status: "A" }, { order_id: 6, address: { user_id: 5, first_name: "Shruti", last_name: "Shinde", landmark: "test", mobile: "123456789", type: "work", city_id: 45, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-05-02T09:35:25.000Z", updated_on: "2018-05-02T09:35:25.000Z", address_1: "test", address_2: "test" }, status: "A" }], B: [{ order_id: 13, address: null, status: "B" }, { order_id: 8, address: { user_id: 5, first_name: "TEST", last_name: "Shinde", landmark: "test", mobile: "9773071307", type: "work", city_id: 666, postcode: 0, address_type: "address", locker_id: 0, added_on: "2018-05-02T09:35:25.000Z", updated_on: "2018-05-02T09:35:25.000Z", address_1: "test", address_2: "TEST" }, status: "B" }], C: [{ order_id: 2, address: null, status: "C" }], D: [{ order_id: 15, address: null, status: "D" }] },
result = [].concat(...Object.values(data));
console.log(result);
&#13;