Group array by StudentId and find sum of subarrays with same element - javascript

[
{
"id": 1,
"subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
"present": "false",
"date": "2018-03-04",
"createdAt": "2018-03-04T14:06:18.000Z",
"updatedAt": "2018-03-04T14:06:18.000Z",
"SemesterId": 1,
"BatchId": 1,
"StudentId": 1,
"Student": {
"id": 1,
"rollnumber": 11111,
"studentname": "LAWANNA HAUGHT",
"dateofbirth": "1997-1-01",
"mobilenumber": "9875449112",
"createdAt": "2018-03-03T14:59:01.000Z",
"updatedAt": "2018-03-03T14:59:01.000Z",
"BatchId": 1
}
},
{
"id": 7,
"subject": "COMPUTER ORGANIZATION",
"present": "true",
"date": "2018-03-04",
"createdAt": "2018-03-04T14:09:12.000Z",
"updatedAt": "2018-03-04T14:09:12.000Z",
"SemesterId": 1,
"BatchId": 1,
"StudentId": 1,
"Student": {
"id": 1,
"rollnumber": 11111,
"studentname": "LAWANNA HAUGHT",
"dateofbirth": "1997-1-01",
"mobilenumber": "9875449112",
"createdAt": "2018-03-03T14:59:01.000Z",
"updatedAt": "2018-03-03T14:59:01.000Z",
"BatchId": 1
}
},
{
"id": 2,
"subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
"present": "true",
"date": "2018-03-04",
"createdAt": "2018-03-04T14:06:18.000Z",
"updatedAt": "2018-03-04T14:06:18.000Z",
"SemesterId": 1,
"BatchId": 1,
"StudentId": 2,
"Student": {
"id": 2,
"rollnumber": 11112,
"studentname": "KAILA HALBROOK",
"dateofbirth": "1997-1-01",
"mobilenumber": "9875449113",
"createdAt": "2018-03-03T14:59:01.000Z",
"updatedAt": "2018-03-03T14:59:01.000Z",
"BatchId": 1
}
},
{
"id": 8,
"subject": "COMPUTER ORGANIZATION",
"present": "false",
"date": "2018-03-04",
"createdAt": "2018-03-04T14:09:12.000Z",
"updatedAt": "2018-03-04T14:09:12.000Z",
"SemesterId": 1,
"BatchId": 1,
"StudentId": 2,
"Student": {
"id": 2,
"rollnumber": 11112,
"studentname": "KAILA HALBROOK",
"dateofbirth": "1997-1-01",
"mobilenumber": "9875449113",
"createdAt": "2018-03-03T14:59:01.000Z",
"updatedAt": "2018-03-03T14:59:01.000Z",
"BatchId": 1
}
}
]
This is an attendance record of one batch.
I'm trying to transform this array to the following form.
All the records with a particular StudentId must be together with one StudentId and studentname.
[
{
"StudentId": 1,
"studentname": "LAWANNA HAUGHT",
"rollnumber": 11111,
"attendance": [
{
"subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
"present": 0,
"absent": 1,
"total": 1
},
{
"subject": "COMPUTER ORGANIZATION",
"present": 1,
"absent": 0,
"total": 1
}
]
},
{
"StudentId": 2,
"studentname": "KAILA HALBROOK",
"rollnumber": 11111,
"attendance": [
{
"subject": "FUNDAMENTALS OF INFORMATION TECHNOLOGY",
"present": 1,
"absent": 0,
"total": 1
},
{
"subject": "COMPUTER ORGANIZATION",
"present": 0,
"absent": 1,
"total": 1
}
]
}
]
present is the number of rows with same StudentId and subjectname and present=true
absent is the number of rows with same StudentId and subjectname and present=false
total is the number of rows with same StudentId and subjectname.
This is the code I have written with the help of one StackOverflow answer and using underscore.js.
But it finds present, absent and total values of all, not by StudentId and subject.
var groups = _.groupBy(list, 'StudentId');
var result = _.map(groups, function(student){
return {
StudentId: student[0].StudentId,
studentname: student[0].Student.studentname,
rollnumber: student[0].Student.rollnumber,
attendances: _.map(student, function(item){
return {
subject:item.subject,
present: _.reduce(item, function(memo, att){
if(att.present=='true')
return memo + 1;
else
return memo;
}, 0),
absent: _.reduce(item, function(memo, att){
if(att.present=='false')
return memo + 1;
else
return memo;
}, 0),
total: _.reduce(item, function(memo, att){
return memo + 1;
}, 0)
}
})
})
}
}
);

You could use a nested approach by looking for strunden and later for looking for attendance.
var data = [{ id: 1, subject: "FUNDAMENTALS OF INFORMATION TECHNOLOGY", present: "false", date: "2018-03-04", createdAt: "2018-03-04T14:06:18.000Z", updatedAt: "2018-03-04T14:06:18.000Z", SemesterId: 1, BatchId: 1, StudentId: 1, Student: { id: 1, rollnumber: 11111, studentname: "LAWANNA HAUGHT", dateofbirth: "1997-1-01", mobilenumber: "9875449112", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 7, subject: "COMPUTER ORGANIZATION", present: "true", date: "2018-03-04", createdAt: "2018-03-04T14:09:12.000Z", updatedAt: "2018-03-04T14:09:12.000Z", SemesterId: 1, BatchId: 1, StudentId: 1, Student: { id: 1, rollnumber: 11111, studentname: "LAWANNA HAUGHT", dateofbirth: "1997-1-01", mobilenumber: "9875449112", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 2, subject: "FUNDAMENTALS OF INFORMATION TECHNOLOGY", present: "true", date: "2018-03-04", createdAt: "2018-03-04T14:06:18.000Z", updatedAt: "2018-03-04T14:06:18.000Z", SemesterId: 1, BatchId: 1, StudentId: 2, Student: { id: 2, rollnumber: 11112, studentname: "KAILA HALBROOK", dateofbirth: "1997-1-01", mobilenumber: "9875449113", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }, { id: 8, subject: "COMPUTER ORGANIZATION", present: "false", date: "2018-03-04", createdAt: "2018-03-04T14:09:12.000Z", updatedAt: "2018-03-04T14:09:12.000Z", SemesterId: 1, BatchId: 1, StudentId: 2, Student: { id: 2, rollnumber: 11112, studentname: "KAILA HALBROOK", dateofbirth: "1997-1-01", mobilenumber: "9875449113", createdAt: "2018-03-03T14:59:01.000Z", updatedAt: "2018-03-03T14:59:01.000Z", BatchId: 1 } }],
grouped = [];
data.forEach(({ subject, present, StudentId, Student: { rollnumber, studentname } }) => {
var student = grouped.find(g => StudentId === g.StudentId),
attendance;
if (!student) {
student = { StudentId, studentname, rollnumber, attendance: [] };
grouped.push(student);
}
attendance = student.attendance.find(a => a.subject === subject);
if (!attendance) {
attendance = { subject, present: 0, absent: 0, total: 0 };
student.attendance.push(attendance);
}
attendance[{ true: 'present', false: 'absent' }[present]]++;
attendance.total++;
});
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Okay, this came out a little weird. If you are coding the backend, it may make sense to handle this on the query end. That being said, you can use reduce to "collapse" the array into an object based on the StudentId. In this process, you can build out the shape of what the record should look like. I don't this is a standard way of using reduce since you are transforming an array. That being said, I couldn't think of an easy way of using map. The result of this operation gives us an object, we can get it back to an array using Object.values(). I don't understand the use case for present, absent, and total.
Here is the JSFiddle.
function isolateRecords(data) {
return Object.values(data.reduce((accum, current) => {
let attenance = {
subject: current.subject,
present: current.present === "true" ? 1 : 0,
absent: current.present === "false" ? 1 : 0,
total: 0
};
attenance.total = attenance.present + attenance.absent;
if (accum[current.StudentId]) {
accum[current.StudentId].attendance.push(attenance);
} else {
accum[current.StudentId] = {
StudentId: current.StudentId,
studentname: current.Student.studentname,
rollnumber: current.Student.rollnumber,
attendance: [attenance]
}
}
return accum;
}, {}));
}
isolateRecords(myOriginalArrayOfRecords);

Related

How to change the format of an array of objects based on ParentID

I am trying to change the format of an array of objects based on a field called ParentID.
Here is a sample of data:
var JsonVal = "data": {
"Factorid": 197325,
"orders": [
{
"pID": 794522,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428539",
"UnitPrice": "300000",
},
{
"pID": 794525,
"Count": 1,
"ParentID": 794551,
"Description": "",
"productid": "1428543",
"UnitPrice": "600000",
},
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
} ,
{
"pID": 794526,
"Count": 3,
"ParentID": 794550,
"Description": "",
"productid": "1428548",
"UnitPrice": "600000",
},
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
},
]
}
As you can see every object has a ParentID. I want to say that if ParentID is 0, it will be known as a parent and add a field call children which its value is null. However if ParentID is not 0 , it will mean that this Object is a child of another Object that its pID is the same as ParentID child Object.The final JSON should be like this :
"data": {
"Factorid": 197325,
"orders": [
{
"pID": 794550,
"Count": 2,
"ParentID": 0,
"Description": "",
"productid": "1428648",
"UnitPrice": "60000",
"children": "[{
'pID': 794526,
'Count':3,
'ParentID': 794550,
'Description': '',
'productid': '1428548',
'UnitPrice': '600000',
}]",
},
{
"pID": 794551,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428647",
"UnitPrice": "250000",
"children":"[
{
'pID': 794522,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428539',
'UnitPrice': '300000',
},
{
'pID': 794525,
'Count': 1,
'ParentID': 794551,
'Description': '',
'productid': '1428543',
'UnitPrice': '600000',
},
]",
} ,
{
"pID": 794527,
"Count": 1,
"ParentID": 0,
"Description": "",
"productid": "1428542",
"UnitPrice": "400000",
"children":"",
},
]
}
children should be added to any object which is parent and also children is always a string field.
I wrote a code but I do not know how to handle it?
JsonVal.orders.forEach(orders => {
if(orders.ParentID == 0){
orders.children="";
}else{
if(orders.ParentID == new_rows){
JsonVal.orders = JsonVal.orders.filter(item => item.ParentID == orders.ParentID);
}
}
});
You basically filter your list of orders if it is a "rootOrder" which means that it has one or multiple children.
Next, you filter your list again by matching the ParentId to the root.pID
// input
const JsonVal = {
data: {
Factorid: 197325,
orders: [
{
pID: 794522,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428539",
UnitPrice: "300000",
},
{
pID: 794525,
Count: 1,
ParentID: 794551,
Description: "",
productid: "1428543",
UnitPrice: "600000",
},
{
pID: 794550,
Count: 2,
ParentID: 0,
Description: "",
productid: "1428648",
UnitPrice: "60000",
},
{
pID: 794551,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428647",
UnitPrice: "250000",
},
{
pID: 794526,
Count: 3,
ParentID: 794550,
Description: "",
productid: "1428548",
UnitPrice: "600000",
},
{
pID: 794527,
Count: 1,
ParentID: 0,
Description: "",
productid: "1428542",
UnitPrice: "400000",
},
],
},
};
function mapOrders(orders) {
const rootOrders = orders.filter((order) => order.ParentID === 0);
return rootOrders.map((rootOrder) => {
const children = orders.filter((order) => order.ParentID === rootOrder.pID);
return {
...rootOrder,
children: children.length ? children : "",
};
});
}
// output
const mappedJsonVal = {
...JsonVal,
data: {
...JsonVal.data,
orders: mapOrders(JsonVal.data.orders),
},
};

Loop through object javascript

This is my response body. I want to select only Collaborator's "supportNotifications" key-value set to true, how can I do that?
{
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
}
let response = {
"status": true,
"date": "2022-04-06T16:33:13.630Z",
"data": {
"collaborators": [
{
"name": "Paul",
"lastCheckin": "2022-03-31T19:54:50.584Z",
"sales": 1,
"createdAt": "2022-03-30T14:47:48.478Z",
"id": "62446d4ab7f4da001e8f40dc",
"supportNotification": true,
"collaboratorId": 1
},
{
"name": "John",
"lastCheckin": null,
"sales": 0,
"createdAt": "2022-03-01",
"id": "62446ea4b7f4da001e8f40df",
"supportNotification": false,
"collaboratorId": 6
}
],
"count": 2
}
};
const collaborators = response.data.collaborators.filter(c => c.supportNotification);
This will give you each collaborator object that has supportNotification = true. Is this the result you are trying to select?
var body = {
status: true,
date: "2022-04-06T16:33:13.630Z",
data: {
collaborators: [
{
name: "Paul",
lastCheckin: "2022-03-31T19:54:50.584Z",
sales: 1,
createdAt: "2022-03-30T14:47:48.478Z",
id: "62446d4ab7f4da001e8f40dc",
supportNotification: true,
collaboratorId: 1,
},
{
name: "John",
lastCheckin: null,
sales: 0,
createdAt: "2022-03-01",
id: "62446ea4b7f4da001e8f40df",
supportNotification: false,
collaboratorId: 6,
},
],
count: 2,
},
};
var result = [];
body.data.collaborators.forEach((item) => {
if (item.supportNotification === true) {
result.push(item);
}
});
console.log(result);

How do rearrange the nested array

Below is my response which is getting from the MySQL database,
I want to rearrange childInterest like in the second desired output
"childInterest": [
{
"id": 1,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.851Z",
"updated_at": "2022-03-04T07:30:31.851Z",
"interest": {
"id": 6,
"interestId": 3,
"name": "collecting stamps",
"level": 2,
"createdAt": "2022-01-27T14:22:22.586Z",
"updatedAt": "2022-01-27T14:22:22.586Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 2,
"parentInterestId": 3,
"created_at": "2022-03-04T07:30:31.863Z",
"updated_at": "2022-03-04T07:30:31.863Z",
"interest": {
"id": 7,
"interestId": 3,
"name": "chess play",
"level": 2,
"createdAt": "2022-01-27T14:22:48.734Z",
"updatedAt": "2022-01-27T14:22:48.734Z",
"heirarchy": {
"id": 3,
"interestId": null,
"name": "hobbies",
"level": 1,
"createdAt": "2022-01-27T14:20:40.362Z",
"updatedAt": "2022-01-27T14:20:40.362Z"
}
}
},
{
"id": 5,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.499Z",
"updated_at": "2022-03-04T08:06:03.499Z",
"interest": {
"id": 8,
"interestId": 4,
"name": "cricket",
"level": 2,
"createdAt": "2022-01-27T14:23:13.042Z",
"updatedAt": "2022-01-27T14:23:13.042Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
},
{
"id": 6,
"parentInterestId": 4,
"created_at": "2022-03-04T08:06:03.519Z",
"updated_at": "2022-03-04T08:06:03.519Z",
"interest": {
"id": 9,
"interestId": 4,
"name": "coco",
"level": 2,
"createdAt": "2022-01-27T14:23:22.940Z",
"updatedAt": "2022-01-27T14:23:22.940Z",
"heirarchy": {
"id": 4,
"interestId": null,
"name": "outdoor",
"level": 1,
"createdAt": "2022-01-27T14:21:00.701Z",
"updatedAt": "2022-01-27T14:21:00.701Z"
}
}
}
],
And I want a response like below
"childInterest": [
{
title: "Hobbies",
titleitems: [
"Music",
"Video"
]
},
{
title: "Indoor",
titleItems: [
"Carrom",
"Table Tenis"
]
},
{
title: "Outdoor",
titleItems: [
"Cricket",
"Kabbadi"
]
}
]
}
],
In 1st array whatever names hobbies, outdoor is there should come under single object inside that array should hobbies names like music video etc.
You probably want to take a look at the map() function:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
My solution.
I won't be explaining steps by steps.
let childInterest = [
{
id: 1,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.851Z",
updated_at: "2022-03-04T07:30:31.851Z",
interest: {
id: 6,
interestId: 3,
name: "collecting stamps",
level: 2,
createdAt: "2022-01-27T14:22:22.586Z",
updatedAt: "2022-01-27T14:22:22.586Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 2,
parentInterestId: 3,
created_at: "2022-03-04T07:30:31.863Z",
updated_at: "2022-03-04T07:30:31.863Z",
interest: {
id: 7,
interestId: 3,
name: "chess play",
level: 2,
createdAt: "2022-01-27T14:22:48.734Z",
updatedAt: "2022-01-27T14:22:48.734Z",
heirarchy: {
id: 3,
interestId: null,
name: "hobbies",
level: 1,
createdAt: "2022-01-27T14:20:40.362Z",
updatedAt: "2022-01-27T14:20:40.362Z",
},
},
},
{
id: 5,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.499Z",
updated_at: "2022-03-04T08:06:03.499Z",
interest: {
id: 8,
interestId: 4,
name: "cricket",
level: 2,
createdAt: "2022-01-27T14:23:13.042Z",
updatedAt: "2022-01-27T14:23:13.042Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
{
id: 6,
parentInterestId: 4,
created_at: "2022-03-04T08:06:03.519Z",
updated_at: "2022-03-04T08:06:03.519Z",
interest: {
id: 9,
interestId: 4,
name: "coco",
level: 2,
createdAt: "2022-01-27T14:23:22.940Z",
updatedAt: "2022-01-27T14:23:22.940Z",
heirarchy: {
id: 4,
interestId: null,
name: "outdoor",
level: 1,
createdAt: "2022-01-27T14:21:00.701Z",
updatedAt: "2022-01-27T14:21:00.701Z",
},
},
},
];
let newOne = Object.values(
childInterest.reduce(
(
acc,
{
interest: {
name: titleItems,
heirarchy: { name: title },
},
}
) => {
const key = [title];
acc[key] ||= { titleItems: [], title };
acc[key].titleItems.push(titleItems);
return acc;
},
{}
)
);
console.log(newOne);

How to use reduce to retrieve values from deep nested array objects

I'm trying to get a list of all users with the author.username property. So that means access posts author, comments author, and commentReplies author. I want an returned array with a list of usernames.
expected output
["blankman", "blankman2", "barnowl", "barnowl2", "blankman3"]
the wrong output
["blankman", "blankman2blankman3", "barnowl2"]
This is how im doing it but i think im doing it wrong. as its not dynamic
const arr = [{
"id": 5,
"title": "buttercup",
"postContent": "dsdsfsfsfsfsfs",
"likedByMe": false,
"likeCounts": 0,
"userId": 1,
"createdAt": "2020-08-17T03:41:16.749Z",
"updatedAt": "2020-08-17T03:41:16.749Z",
"author": {
"username": "blankman",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
},
"Likes": [],
"Comments": [{
"id": 46,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl",
"gravatar": null,
"bio": null
}
}],
"author": {
"username": "blankman2",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
{
"id": 47,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl2",
"gravatar": null,
"bio": null
}
}],
"author": {
"username": "blankman3",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
],
"RePosts": [],
"RepostedByMe": false
}]
const findUsers = arr.reduce((acc, cv) => {
const users = [
...acc,
cv.author.username, // get first level author.username
cv.Comments.reduce((acc, cv) => acc.concat(cv.author.username), ""), // get comments author.username
cv.Comments.reduce((acc, cv) => cv.commentReplies.reduce((acc, cv) => acc.concat(cv.author.username), ""), "") // get comment replies author.username
]
return users
}, [])
console.log(findUsers)
You can do this iteratively or recursively:
const arr = [{
"id": 5,
"title": "buttercup",
"postContent": "dsdsfsfsfsfsfs",
"likedByMe": false,
"likeCounts": 0,
"userId": 1,
"createdAt": "2020-08-17T03:41:16.749Z",
"updatedAt": "2020-08-17T03:41:16.749Z",
"author": {
"username": "blankman",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
},
"Likes": [],
"Comments": [
{
"id": 46,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [
{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl",
"gravatar": null,
"bio": null
}
}
],
"author": {
"username": "blankman2",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
{
"id": 47,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [
{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl2",
"gravatar": null,
"bio": null
}
}
],
"author": {
"username": "blankman3",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
],
"RePosts": [],
"RepostedByMe": false
}]
// No recursion
function getUsers(array) {
return array.flatMap(v => {
let comments = v.Comments;
return [v.author.username].concat(comments.flatMap(c => {
let replies = c.commentReplies;
return [c.author.username].concat(replies.flatMap(r => {
return r.author.username;
}));
}));
});
}
// Recursion
function recursGetUsers(array) {
if (!array) return [];
return array.flatMap(v => {
return [v.author.username]
.concat(recursGetUsers(v.Comments))
.concat(recursGetUsers(v.commentReplies));
});
}
console.log(getUsers(arr));
console.log(recursGetUsers(arr));
You can use several nested flatMap and map operations.
const arr = [{ "id": 5, "title": "buttercup", "postContent": "dsdsfsfsfsfsfs", "likedByMe": false, "likeCounts": 0, "userId": 1, "createdAt": "2020-08-17T03:41:16.749Z", "updatedAt": "2020-08-17T03:41:16.749Z", "author": { "username": "blankman", "gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", "bio": null }, "Likes": [], "Comments": [{ "id": 46, "comment_body": "fsfsfsfsfsf", "gifUrl": "", "userId": 1, "postId": 5, "createdAt": "2020-08-18T04:46:08.946Z", "updatedAt": "2020-08-18T04:46:08.946Z", "commentReplies": [{ "id": 18, "replyBody": "fsfsffsffsffsf", "userId": 2, "commentId": 46, "postId": 5, "createdAt": "2020-08-21T16:40:47.205Z", "updatedAt": "2020-08-21T16:40:47.205Z", "author": { "username": "barnowl", "gravatar": null, "bio": null } }], "author": { "username": "blankman2", "gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", "bio": null } }, { "id": 47, "comment_body": "fsfsfsfsfsf", "gifUrl": "", "userId": 1, "postId": 5, "createdAt": "2020-08-18T04:46:08.946Z", "updatedAt": "2020-08-18T04:46:08.946Z", "commentReplies": [{ "id": 18, "replyBody": "fsfsffsffsffsf", "userId": 2, "commentId": 46, "postId": 5, "createdAt": "2020-08-21T16:40:47.205Z", "updatedAt": "2020-08-21T16:40:47.205Z", "author": { "username": "barnowl2", "gravatar": null, "bio": null } }], "author": { "username": "blankman3", "gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", "bio": null } }, ], "RePosts": [], "RepostedByMe": false }];
const res = arr.flatMap(x => [x.author.username].concat(x.Comments.flatMap(y => y.commentReplies.map(z => z.author.username).concat(y.author.username))));
console.log(res);
If your schema is settled down, your approach is a solution and almost reaching the goal (though recursion is another solution). the problem in your codes is uses Array.concat then return String. You should return one [] same as the most outer reduce.
Check below snippet:
const arr = [{
"id": 5,
"title": "buttercup",
"postContent": "dsdsfsfsfsfsfs",
"likedByMe": false,
"likeCounts": 0,
"userId": 1,
"createdAt": "2020-08-17T03:41:16.749Z",
"updatedAt": "2020-08-17T03:41:16.749Z",
"author": {
"username": "blankman",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
},
"Likes": [],
"Comments": [
{
"id": 46,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [
{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl",
"gravatar": null,
"bio": null
}
}
],
"author": {
"username": "blankman2",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
{
"id": 47,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [
{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl2",
"gravatar": null,
"bio": null
}
}
],
"author": {
"username": "blankman3",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
],
"RePosts": [],
"RepostedByMe": false
}]
const findUsers = arr.reduce((acc, cv) => {
const users = [
...acc,
cv.author.username, // get first level author.username
...cv.Comments.reduce((acc, cv) => [...acc, cv.author.username], []), // get comments author.username
...cv.Comments.reduce((acc, cv) => [...acc, ...cv.commentReplies.reduce((acc, cv) => [...acc, cv.author.username], [])], []) // get comment replies author.username
]
return users
}, [])
console.log(findUsers)
This is the schema of your object:
author.username
Comments[i].author.username
Comments[i].commentReplies[i].author.username
You asked specifically for a way using .reduce so here is one solution using .reduce along with lots of destructuring:
const usernames = arr.reduce((names, { author: { username }, Comments }) => {
names.push(username);
Comments.forEach(({ author: { username }, commentReplies }) => {
names.push(username);
commentReplies.forEach(({ author: { username } }) => names.push(username));
});
return names;
}, []);
Live example:
'use strict';
const arr = [
{
"id": 5,
"title": "buttercup",
"postContent": "dsdsfsfsfsfsfs",
"likedByMe": false,
"likeCounts": 0,
"userId": 1,
"createdAt": "2020-08-17T03:41:16.749Z",
"updatedAt": "2020-08-17T03:41:16.749Z",
"author": {
"username": "blankman",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
},
"Likes": [],
"Comments": [{
"id": 46,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl",
"gravatar": null,
"bio": null
}
}],
"author": {
"username": "blankman2",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
{
"id": 47,
"comment_body": "fsfsfsfsfsf",
"gifUrl": "",
"userId": 1,
"postId": 5,
"createdAt": "2020-08-18T04:46:08.946Z",
"updatedAt": "2020-08-18T04:46:08.946Z",
"commentReplies": [{
"id": 18,
"replyBody": "fsfsffsffsffsf",
"userId": 2,
"commentId": 46,
"postId": 5,
"createdAt": "2020-08-21T16:40:47.205Z",
"updatedAt": "2020-08-21T16:40:47.205Z",
"author": {
"username": "barnowl2",
"gravatar": null,
"bio": null
}
}],
"author": {
"username": "blankman3",
"gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN",
"bio": null
}
},
],
"RePosts": [],
"RepostedByMe": false
}
];
// author.username
// Comments[i].author.username
// Comments[i].commentReplies[i].author.username
const usernames = arr.reduce((names, { author: { username }, Comments }) => {
names.push(username);
Comments.forEach(({ author: { username }, commentReplies }) => {
names.push(username);
commentReplies.forEach(({ author: { username } }) => names.push(username));
});
return names;
}, []);
console.log(usernames);
// => [ 'blankman', 'blankman2', 'barnowl', 'blankman3', 'barnowl2' ]
You would need a recursive function. With flatMap you can ensure that the recursively returned arrays are put together as a single flat array.
Here is a possible implementation that doesn't depend on property names like "Comments" and "commentReplies", but just scans all arrays:
const collect = arr => Array.isArray(arr) ? arr.flatMap(value =>
[value.author?.username].concat(Object.values(value).flatMap(collect))
).filter(Boolean) : [];
// demo
const arr = [{"id": 5,"title": "buttercup","postContent": "dsdsfsfsfsfsfs","likedByMe": false,"likeCounts": 0,"userId": 1,"createdAt": "2020-08-17T03:41:16.749Z","updatedAt": "2020-08-17T03:41:16.749Z","author": {"username": "blankman","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null},"Likes": [],"Comments": [{"id": 46,"comment_body": "fsfsfsfsfsf","gifUrl": "","userId": 1,"postId": 5,"createdAt": "2020-08-18T04:46:08.946Z","updatedAt": "2020-08-18T04:46:08.946Z","commentReplies": [{"id": 18,"replyBody": "fsfsffsffsffsf","userId": 2,"commentId": 46,"postId": 5,"createdAt": "2020-08-21T16:40:47.205Z","updatedAt": "2020-08-21T16:40:47.205Z","author": {"username": "barnowl","gravatar": null,"bio": null}}],"author": {"username": "blankman2","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null}},{"id": 47,"comment_body": "fsfsfsfsfsf","gifUrl": "","userId": 1,"postId": 5,"createdAt": "2020-08-18T04:46:08.946Z","updatedAt": "2020-08-18T04:46:08.946Z","commentReplies": [{"id": 18,"replyBody": "fsfsffsffsffsf","userId": 2,"commentId": 46,"postId": 5,"createdAt": "2020-08-21T16:40:47.205Z","updatedAt": "2020-08-21T16:40:47.205Z","author": {"username": "barnowl2","gravatar": null,"bio": null}}],"author": {"username": "blankman3","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null}},],"RePosts": [],"RepostedByMe": false}];
console.log(collect(arr));
If you find this functional expression syntax confusing, then here is something similar in a more verbose manner:
function collect(arr) {
if (!Array.isArray(arr)) return [];
let result = [];
for (let obj of arr) {
result.push(obj.author?.username);
for (let key in obj) {
result = result.concat(collect(obj[key]));
}
}
return result.filter(Boolean); // exclude null/undefined values
}
// demo
const arr = [{"id": 5,"title": "buttercup","postContent": "dsdsfsfsfsfsfs","likedByMe": false,"likeCounts": 0,"userId": 1,"createdAt": "2020-08-17T03:41:16.749Z","updatedAt": "2020-08-17T03:41:16.749Z","author": {"username": "blankman","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null},"Likes": [],"Comments": [{"id": 46,"comment_body": "fsfsfsfsfsf","gifUrl": "","userId": 1,"postId": 5,"createdAt": "2020-08-18T04:46:08.946Z","updatedAt": "2020-08-18T04:46:08.946Z","commentReplies": [{"id": 18,"replyBody": "fsfsffsffsffsf","userId": 2,"commentId": 46,"postId": 5,"createdAt": "2020-08-21T16:40:47.205Z","updatedAt": "2020-08-21T16:40:47.205Z","author": {"username": "barnowl","gravatar": null,"bio": null}}],"author": {"username": "blankman2","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null}},{"id": 47,"comment_body": "fsfsfsfsfsf","gifUrl": "","userId": 1,"postId": 5,"createdAt": "2020-08-18T04:46:08.946Z","updatedAt": "2020-08-18T04:46:08.946Z","commentReplies": [{"id": 18,"replyBody": "fsfsffsffsffsf","userId": 2,"commentId": 46,"postId": 5,"createdAt": "2020-08-21T16:40:47.205Z","updatedAt": "2020-08-21T16:40:47.205Z","author": {"username": "barnowl2","gravatar": null,"bio": null}}],"author": {"username": "blankman3","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null}},],"RePosts": [],"RepostedByMe": false}];
console.log(collect(arr));
You could take a recursion by handing over the key of the parent object.
It works with three steps,
check if the handed over value is an array and if not return an empty array,
check if parent key is author and if property username exist, then take the value of the username property.
get the entries of the object and get all usernames with a recursion by handing over the parent key.
const
getUsernames = (object, parent) => {
if (!object || typeof object !== 'object') return [];
const temp = [];
if (parent === 'author' && 'username' in object) temp.push(object.username);
return [
...temp,
...Object.entries(object).flatMap(([k, v]) => getUsernames(v, k))
];
},
data = [{ id: 5, title: "buttercup", postContent: "dsdsfsfsfsfsfs", likedByMe: false, likeCounts: 0, userId: 1, createdAt: "2020-08-17T03:41:16.749Z", updatedAt: "2020-08-17T03:41:16.749Z", author: { username: "blankman", gravatar: "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", bio: null }, Likes: [], Comments: [{ id: 46, comment_body: "fsfsfsfsfsf", gifUrl: "", userId: 1, postId: 5, createdAt: "2020-08-18T04:46:08.946Z", updatedAt: "2020-08-18T04:46:08.946Z", commentReplies: [{ id: 18, replyBody: "fsfsffsffsffsf", userId: 2, commentId: 46, postId: 5, createdAt: "2020-08-21T16:40:47.205Z", updatedAt: "2020-08-21T16:40:47.205Z", author: { username: "barnowl", gravatar: null, bio: null } }], author: { username: "blankman2", gravatar: "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", bio: null } }, { id: 47, comment_body: "fsfsfsfsfsf", gifUrl: "", userId: 1, postId: 5, createdAt: "2020-08-18T04:46:08.946Z", updatedAt: "2020-08-18T04:46:08.946Z", commentReplies: [{ id: 18, replyBody: "fsfsffsffsffsf", userId: 2, commentId: 46, postId: 5, createdAt: "2020-08-21T16:40:47.205Z", updatedAt: "2020-08-21T16:40:47.205Z", author: { username: "barnowl2", gravatar: null, bio: null } }], author: { username: "blankman3", gravatar: "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN", bio: null } }], RePosts: [], RepostedByMe: false }],
result = getUsernames(data);
console.log(result);

How to merge array of objects of different keys into one key from one array

I am working in Node JS. I have one JSON array of object. There are different key in one JSON object such as A, B, C , D, E I have to merge array of objects from different keys into one key. I am providing my JSON object as follows.
{
"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"
}
]
}
I want to merge array of objects from keys B , C into key D. So how can I achieve it. Thanks in advance. Really sorry I am doing little change in my question. I have only changed my keys which was previously A, B, C, D.
Just use spread syntax:
const obj = {
"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"
}]
};
obj.D = [...obj.B, ...obj.C, ...obj.D];
console.log(obj.D);
You can use Object.values() to get all values of your object, and using spread syntax and array#concat you can get an array.
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);
You could get the values with Object.values and concat them to the result set.
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; }

Categories