Merging two nested JSON objects based on conditionals in javascript - javascript

I'm trying to figure out how to merge two JSON objects and create a merged object based on conditionals.
I want to loop through leadlistArray, get the rules.id for each entry and match it against rulesArray. The final outcome should be the finalArray.
leadlistArray = {
"status": "success",
"data": {
"leadlists": [
{
"rules": [
{
"$oid": "53866d4d1fd21c3cc41f52da"
}
],
"name": "List 1: only general rule"
},
{
"rules": [
{
"$oid": "53866d4d1fd21c3cc41f52da"
},
{
"$oid": "53866d9c1fd21c3cc79bf7ce"
},
{
"$oid": "53866d9c1fd21c3cc79bf2cd"
}
],
"name": "List 2: general and web-based rule"
},
{
"rules": [
{
"$oid": "53866d9c1fd21c3cc79bf7ce"
}
],
"name": "List 3: only web-based rule"
}
]
}
}
Rules array:
rulesArray = {
"status": "success",
"data": {
"rules": [
{
"description": "optimizely no!",
"start_time": "",
"values": [
"optimizely"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d4d1fd21c3cc41f52da"
},
"type": 0,
"condition": 1
},
{
"description": "google_plus no!",
"start_time": "",
"values": [
"google_plus"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d9c1fd21c3cc79bf2cd"
},
"type": 1,
"condition": 1
},
{
"description": "google_plus yes!",
"start_time": "",
"values": [
"google_plus"
],
"end_time": "",
"operator": "",
"_id": {
"$oid": "53866d9c1fd21c3cc79bf7ce"
},
"type": 1,
"condition": 0
}
]
}
}
the finalArray should, for each list, group the rules descriptions by rule type:
finalArray = [
{'name': 'List 1: only general rule'
,'rules':
{0: [
{'description': 'optimizely no!'}
]}
},
{'name': 'List 2: general and web-based rule'
,'rules':
{0: [
{'description': 'optimizely no!'}
], 1: [
{'description': 'google_plus yes!'},
{'description': 'google_plus no!'}
]}
},
{'name': 'List 3: only web-based rule'
,'rules': {
1: [
{'description': 'google_plus yes!'}
]}
}
]

Try this http://jsfiddle.net/eN52v/
var rules = rulesArray.data.rules
.reduce(function(acc, item){
return acc[item._id.$oid] = {
description: item.description,
type: item.type}, acc;
}, {}), //cache rules by id to avoid array lookups.
finalArray = leadlistArray.data.leadlists.map(function(item) {
return {
name: item.name,
rules: item.rules.reduce(function(acc, rule){ //Why not an array?
var ruleDesc = rules[rule.$oid],
type = ruleDesc.type;
(acc[type] || (acc[type] = [])).push({description: ruleDesc.description});
return acc;}, {})
}});

Related

Creating new json by taking url addresses from existing json and split it up Javascript

I have a json file.
[
{
"line": 1,
"elements": [
{
"start_timestamp": "2022-10-17T20:07:41.706Z",
"steps": [
{
"result": {
"duration": 12216552000,
"status": "passed"
},
"line": 5,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
},
{
"result": {
"duration": 2074982200,
"status": "passed"
},
"line": 6,
"name": "m1e1",
"match": {
"location": "seleniumgluecode.book.user_enters_Usernamee()"
},
"keyword": "When "
}
],
"tags": [
{
"name": "#Smokee"
}
]
},
{
"start_timestamp": "2022-10-17T20:08:12.284Z",
"steps": [
{
"result": {
"duration": 12090584100,
"status": "passed"
},
"line": 12,
"name": "m0e2",
"match": {
"location": "seleniumgluecode.book2.user_is_on_homepageee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "Login Featuree",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featuree",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe/squad1/kitab.feature",
"tags": []
},
{
"line": 1,
"elements": [
{
"start_timestamp": "2022-10-17T20:08:34.480Z",
"steps": [
{
"result": {
"duration": 11366098500,
"status": "passed"
},
"line": 5,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "Login Featureefghfgh",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featureefghfgh",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature",
"tags": []
},
{
"line": 19,
"elements": [
{
"start_timestamp": "2022-10-17T20:09:40.836Z",
"steps": [
{
"result": {
"duration": 12761711100,
"status": "passed"
},
"line": 23,
"name": "m0e",
"match": {
"location": "seleniumgluecode.book.user_is_on_homepagee()"
},
"keyword": "Given "
}
],
"tags": [
{
"name": "#Smokee"
}
]
}
],
"name": "X Feature",
"description": " Verify if user is able to Login in to the sitee",
"id": "login-featuree",
"keyword": "Feature",
"uri": "file:src/test/java/features/tribe2/test.feature",
"tags": []
}
]
I am getting url addresses in this array
document.addEventListener("DOMContentLoaded", () => {
var i = report.length;
var array = [];
for(x = 0; x < i; x++){
array.push(report[x].uri.split("/"));
}
console.log(array2);
});
This return me :
0:
(7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
1:
(7) ['file:src', 'test', 'java', 'features', 'tribe1', 'squad2', 'kitab2.feature']
2:
(6) ['file:src', 'test', 'java', 'features', 'tribe2, kitab3.feature']
I don't need file:src, test, java, features. Deleting them in 3 arrays and getting a unique array like this:
0:
(3) ['tribe1', 'squad1', 'kitab.feature']
1:
(3) ['tribe1', 'squad2', 'kitab2.feature']
2:
(2) ['tribe2, kitab3.feature']
Finally, if there are 2 elements before the .feature, I need to create a new array by considering 1 as squad and 2 as tribe. Like this:
Diagram
[tribe1
squad1
elem
1
2
name
url
squad2
elem
1
2
name
url
tribe2
elem
1
2
name
url
]
How can I do that?. Thank you.
You should try destructing the array. An example is shown below:
[a,b,c,d, ...rest] = ['file:src', 'test', 'java', 'features', 'tribe1', 'squad1', 'kitab.feature']
console.log(rest);
You can transform or create a new array based on input array with this simple logic with the help of Array.map() along with String.split() and Array.splice() method.
Live Demo :
const arr = [
{
"line": 1,
"uri": "file:src/test/java/features/tribe/squad1/kitab.feature"
},
{
"line": 1,
"uri": "file:src/test/java/features/tribe1/squad2/kitab2.feature"
},
{
"line": 19,
"uri": "file:src/test/java/features/tribe2/test.feature"
}
];
const res = arr.map(({ uri}) => uri.split('/').splice(4));
console.log(res);

Add JSON to empty array issue

I have a top level JSON structure as follows:
{
"video": [],
"messages": [],
"notifications": []
}
and i have a database output (in variable "result") as follows i want to push into the "video" array:
[
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
My code is:
let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(result)
It works but i am ending up with too many arrays (i think) - it looks as follows:
{
"video": [
[
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
],
"messages": [],
"notifications": []
}
I want "video": [ { ... }, { ... } ] whereas i have "video": [[ { ... }, { ... } ]]
How can i resolve this?
You can use the spread operator as follows:
let result = [
{
"_id": "5f98ab906439155cfc6f9afb",
"status": "NOT_STARTED",
"date": "2020-10-27T23:21:52.683Z",
"callInvitees": [
{
"username": "user1"
},
{
"username": "user2"
}
]
},
{
"_id": "5f98aba0789e163e0c78908f",
"status": "NOT_STARTED",
"date": "2020-10-27T23:22:08.048Z",
"callInvitees": [
{
"username": "user1"
}
]
}
]
let dashboardJSON = { "video": [], "messages": [], "notifications": [] };
dashboardJSON.video.push(...result)
console.log(dashboardJSON);
Just use Array.prototype.map() method. Map method returns a new array with the result by using the provided function.
const ret = {
video: [],
messages: [],
notifications: [],
};
const data = [
{
_id: '5f98ab906439155cfc6f9afb',
status: 'NOT_STARTED',
date: '2020-10-27T23:21:52.683Z',
callInvitees: [
{
username: 'user1',
},
{
username: 'user2',
},
],
},
{
_id: '5f98aba0789e163e0c78908f',
status: 'NOT_STARTED',
date: '2020-10-27T23:22:08.048Z',
callInvitees: [
{
username: 'user1',
},
],
},
];
ret.video = data.map((x) => x);
console.log(ret);

filter on the basis of a value in an array in where

I'm querying data, where the structure is something like this.
I have two models, FoodDrinks and Cuisines. They have many to many relation.
Now on the following query
{
"filter": {
"include": "cuisines"
}
}
I am getting the following results.
[
{
"id": 1,
"name": "Biryani",
"cuisines": [
{
"id": 1,
"name": "Mughlai"
},
{
"id": 2,
"name": "North Indian"
},
{
"id": 3,
"name": "Afghani"
}
]
},
{
"id": 2,
"name": "Chhole Bhature",
"cuisines": [
{
"id": 2,
"name": "North Indian"
}
]
},
{
"id": 3,
"name": "Amritsari Naan",
"cuisines": [
{
"id": 2,
"name": "North Indian"
},
{
"id": 6,
"name": "Punjabi"
},
]
}
]
Now I want only those food drinks which have cuisines from the following array.
let cuisinesIDs = ["1", "2"]
What will the following query for that?
I guess something like the below should work:
{
"filter": {
"include": "cuisines"
},
"where": {
"cuisines.id": {
"inq": cuisinesIDs
}
}
}
It might be the same answer as #Behrooz, but I would try:
{
"filter": {
"include": {
relation: 'cuisines',
scope: {
where: {
id: {inq: cuisinesIDs}
}
}
}
}
}

$group with nested array element

would like to know how to use MongoDB's aggregation to collectively summarize a count of each reason_id:
I would like to get known that there are 2 counts for having "reason_id = KW7Kcsv7835YZeE3n", and 1 count for having "reason_id = KNcKQCjhFzha3oLfE".
Here is my data:
[
{
"_id": "2DLQFJLbZScBXpSam",
"toilet_id": "bJsyfh3TCvpTzE2mJ",
"reason_ids": [
"KNcKQCjhFzha3oLfE",
"KW7Kcsv7835YZeE3n"
],
"score_id": null,
"toilet": {
"_id": "bJsyfh3TCvpTzE2mJ",
"name": "Toilet_M_1",
"gender": "m",
"mac_address": "11:11:11:11:11:11"
}
},
{
"_id": "akjsbcjascklsacas",
"toilet_id": "bJsyfh3TCvpTzE2mJ",
"reason_ids": [
"KW7Kcsv7835YZeE3n"
],
"score_id": null,
"toilet": {
"_id": "fsgndgklndsdsdsd",
"name": "Toilet_F_1",
"gender": "f",
"mac_address": "11:11:11:11:11:11"
}
},
]
You can try this
db.collection.aggregate([
{
"$unwind": "$reason_ids"
},
{
"$group": {
"_id": "$reason_ids",
"count": {
"$sum": 1
}
}
}
])
Output
[
{
"_id": "KW7Kcsv7835YZeE3n",
"count": 2
},
{
"_id": "KNcKQCjhFzha3oLfE",
"count": 1
}
]
Try it here

Javascript(NodeJs) - Conversion of Arrays and Objects to bulk insert in mysql(Sequelize)

This is my json:
{
"senderName": "ifelse",
"message": "Hi",
"groups": [
{
"id": 14,
"groupname": "Angular",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}],
"select": true
}],
"draftData": {
"contacts": [
]
}
}
How to make the above json into:
[{phoneno: 123456789; sender: ifelse ; message: Hi},{phoneno: 11111111; sender: ifelse ; message: Hi}]
I want to take phoneno data from gsm object key
Which is best method to do this? for or forEach or anyother?
I guess, this is what you want. Use map to convert contactgroups to new array with phoneno.
var data = {
"senderName": "ifelse",
"message": "Hi",
"groups": [{
"id": 14,
"groupname": "Angular",
"contactgroups": [{
"id": 1,
"contact": {
"id": 1,
"gsm": "123456789"
}
},
{
"id": 3,
"contact": {
"id": 2,
"gsm": "111111111"
}
}
],
"select": true
}],
"draftData": {
"contacts": []
}
}
var result = data.groups[0].contactgroups.map(i => {
return {
phoneno: i.contact.gsm,
sender: data.senderName,
message: data.message
}
})
console.log(result);

Categories