Nested grouping with mongoose - javascript

I'm trying to implement a nested group query in mongodb and I'm getting stuck. How to group data by date and campaign_id here ? Each campaign_id should contain a nested array of creatives with data (views and clicks) for each creative.
The data looks like this:
Data:
[
{
"_id": ObjectId("61e3b88d3f14fb87161651c7"),
"campaign_id": 41,
"creative_id": 143,
"date": ISODate("2022-01-16T00:00:00Z"),
"views": 1
},
{
"_id": ObjectId("61e3b8cd3f14fb87161651ef"),
"campaign_id": 41,
"creative_id": 143,
"date": ISODate("2022-01-16T00:00:00Z"),
"views": 7070,
"clicks": 241
},
{
"_id": ObjectId("61e3b8cd3f14fb87161651f2"),
"campaign_id": 41,
"creative_id": 144,
"date": ISODate("2022-01-16T00:00:00Z"),
"views": 3474,
"clicks": 52
},
{
"_id": ObjectId("61e3b8cd3f14fb87161651f4"),
"campaign_id": 41,
"creative_id": 146,
"date": ISODate("2022-01-16T00:00:00Z"),
"views": 3595,
"clicks": 52
},
{
"_id": ObjectId("61e3b8cd3f14fb87161651f6"),
"campaign_id": 41,
"creative_id": 145,
"date": ISODate("2022-01-16T00:00:00Z"),
"views": 3561,
"clicks": 227
},
{
"_id": ObjectId("61e4b18a3f14fb871616d8b0"),
"campaign_id": 41,
"creative_id": 143,
"date": ISODate("2022-01-17T00:00:00Z"),
"views": 9528,
"clicks": 104
},
{
"_id": ObjectId("61e4b18a3f14fb871616d8b2"),
"campaign_id": 41,
"creative_id": 145,
"date": ISODate("2022-01-17T00:00:00Z"),
"views": 4699,
"clicks": 100
},
{
"_id": ObjectId("61e4b18a3f14fb871616d8b5"),
"campaign_id": 41,
"creative_id": 146,
"date": ISODate("2022-01-17T00:00:00Z"),
"views": 4759,
"clicks": 36
},
{
"_id": ObjectId("61e4b18e3f14fb871616d8ca"),
"campaign_id": 41,
"creative_id": 144,
"date": ISODate("2022-01-17T00:00:00Z"),
"views": 4822,
"clicks": 27
}
]
This is the expected result after grouping:
Expected result:
{
"_id": {
"campaign": 955,
"date": ISODate("2022-01-16T00:00:00Z")
},
"creative": [
143: {clicks: XXXX, views:XXXX}
146: {clicks: XXXX, views:XXXX},
142: {clicks: XXXX, views:XXXX},
...
]
},
https://mongoplayground.net/p/oZNxQNb250R

You can do something like this:
db.collection.aggregate([
{
"$sort": {
"date": 1
}
},
{
"$group": {
"_id": {
date: "$date",
campaign: "$campaign_id",
creative_id: "$creative_id"
},
views: {
$sum: "$views"
},
clicks: {
$sum: "$clicks"
}
}
},
{
"$group": {
"_id": {
date: "$_id.date",
campaign: "$_id.campaign"
},
data: {
$push: {
k: {
"$toString": "$_id.creative_id"
},
v: {
views: "$views",
clicks: "$clicks"
}
}
}
}
},
{
$project: {
_id: 1,
creative: {
"$arrayToObject": "$data"
}
}
}
])
Mongo Playground
Notice I added an additional $group stage, this is done to group by creative_id and sum all it's views and clicks. if this stage is redundant you can just eliminate this stage however it works either way.

Related

How to create running totals in CouchDB (like SQL window functions)

Edited the question for clarity:
Assume I have the following documents in CouchDB:
{
"_id": "1",
"date": 1672119898,
"amount": 120,
"owner": "user_1"
},
{
"_id": "2",
"date": 1672119897,
"amount": 25,
"owner": "user_1"
},
{
"_id": "3",
"date": 1672119895,
"amount": 40,
"owner": "user_2"
},
{
"_id": "4",
"date": 1672119899,
"amount": 70,
"owner": "user_1"
},
{
"_id": "5",
"date": 1672119891,
"amount": 33,
"owner": "user_1"
},
{
"_id": "6",
"date": 1672119893,
"amount": 11,
"owner": "user_2"
}
And I'd like to create a view that returns the running total: ordered by ascending date, running total by owner.
Easy to do in SQL with window functions. But not sure how to achieve the same with CouchDB. I have no code to show.
Expected Output with running totals by owner, sorted by date descending:
[
{
"_id": "4",
"date": 1672119899,
"amount": 70,
"owner": "user_1",
"totalAmount": 248 // 178 + 70
},
{
"_id": "1",
"date": 1672119898,
"amount": 120,
"owner": "user_1",
"totalAmount": 178 // 120 + 58
},
{
"_id": "2",
"date": 1672119897,
"amount": 25,
"owner": "user_1",
"totalAmount": 58 // 25 + 33
},
{
"_id": "5",
"date": 1672119891,
"amount": 33,
"owner": "user_1",
"totalAmount": 33
},
{
"_id": "3",
"date": 1672119895,
"amount": 40,
"owner": "user_2",
"totalAmount": 51 // 40 + 51
},
{
"_id": "6",
"date": 1672119893,
"amount": 11,
"owner": "user_2",
"totalAmount": 11
}
]

Remove duplicate objects from an array but merge nested objects

Currently have an array of objects which contain game releases. However game releases can happen on multiple platforms and these appear as separate objects within the array. I'm looking to remove duplicate games by comparing the game id but merge the platforms object
I have tried using the reduce function which successfully removes duplicate objects by game id but I'm unable to adapt this to merge platforms
const filteredArr = data.reduce((acc, current) => {
const x = acc.find(item => item.game.id === current.game.id);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
Current Array:
const data = [{
"id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {"id": 48, "name": "PlayStation 4"},
"region": 8,
"y": 2019
}, {
"id": 12,
"date": 1553212800,
"game": {
"id": 76832,
"name": "Spiderman",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {"id": 6, "name": "PC (Microsoft Windows)"},
"region": 8,
"y": 2019
}, {
"id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {"id": 48, "name": "Xbox"},
"region": 8,
"y": 2019
}]
Expected format after merge:
[{
"id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platforms": ["PlayStation", "Xbox"],
"region": 8,
"y": 2019
}, {
"id": 12,
"date": 1553212800,
"game": {
"id": 76832,
"name": "Spiderman",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platforms": ["Playstation"],
"region": 8,
"y": 2019
}]
You were really close, you just need to change the logic a bit. You can try something like the following; an example - https://repl.it/#EQuimper/ScaryBumpyCircle
const filteredArr = data.reduce((acc, current) => {
const x = acc.find(item => item.game.id === current.game.id);
if (!x) {
current.platform = [current.platform]
acc.push(current);
} else {
x.platform.push(current.platform);
}
return acc;
}, []);
The return value is
[
{
"id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": [
{
"id": 48,
"name": "PlayStation 4"
},
{
"id": 48,
"name": "Xbox"
}
],
"region": 8,
"y": 2019
},
{
"id": 12,
"date": 1553212800,
"game": {
"id": 76832,
"name": "Spiderman",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": [
{
"id": 6,
"name": "PC (Microsoft Windows)"
}
],
"region": 8,
"y": 2019
}
]
If you want to have just an array of platform strings, go with
const filteredArr = data.reduce((acc, current) => {
const x = acc.find(item => item.game.id === current.game.id);
if (!x) {
current.platform = [current.platform.name]
acc.push(current);
} else {
x.platform.push(current.platform.name);
}
return acc;
}, []);
And now the return value is
[
{
"id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": [
"PlayStation 4",
"Xbox"
],
"region": 8,
"y": 2019
},
{
"id": 12,
"date": 1553212800,
"game": {
"id": 76832,
"name": "Spiderman",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": [
"PC (Microsoft Windows)"
],
"region": 8,
"y": 2019
}
]
You could separate platform of the object and look if you have an object with the same id and add the platfor tho the array, of not create a new data set.
const
data = [{ id: 157283, date: 1553212800, game: { id: 76882, name: "Sekiro: Shadows Die Twice", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 48, name: "PlayStation 4" }, region: 8, y: 2019 }, { id: 12, date: 1553212800, game: { id: 76832, name: "Spiderman", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 6, name: "PC (Microsoft Windows)" }, region: 8, y: 2019 }, { id: 157283, date: 1553212800, game: { id: 76882, name: "Sekiro: Shadows Die Twice", popularity: 41.39190295640344 }, human: "2019-Mar-22", m: 3, platform: { id: 48, name: "Xbox" }, region: 8, y: 2019 }],
result = data.reduce((r, { platform, ...o }) => {
var temp = r.find(({ id }) => id === o.id);
if (!temp) r.push(temp = { ...o, platforms: [] });
temp.platforms.push(platform);
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Please take a look:
const data = [ { "id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {
"id": 48,
"name": "PlayStation 4"
},
"region": 8,
"y": 2019
},
{
"id": 12,
"date": 1553212800,
"game": {
"id": 76832,
"name": "Spiderman",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {
"id": 6,
"name": "PC (Microsoft Windows)"
},
"region": 8,
"y": 2019
},{ "id": 157283,
"date": 1553212800,
"game": {
"id": 76882,
"name": "Sekiro: Shadows Die Twice",
"popularity": 41.39190295640344
},
"human": "2019-Mar-22",
"m": 3,
"platform": {
"id": 48,
"name": "Xbox"
},
"region": 8,
"y": 2019
},
]
const filteredArr = data.reduce((acc, current) => {
const x = acc.find(item => item.game.id === current.game.id);
if (!x) {
current.platform = [current.platform.name]
return acc.concat([current]);
} else {
x.platform.push(current.platform.name);
return acc;
}
}, []);
console.log(filteredArr);
Here is another solution, using forEach instead of reduce. This makes use of a lookup hash that makes if faster for larger amounts of data then using find.
const data = [{"id": 157283, "date": 1553212800, "game": {"id": 76882, "name": "Sekiro: Shadows Die Twice", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 48, "name": "PlayStation 4"}, "region": 8, "y": 2019}, {"id": 12, "date": 1553212800, "game": {"id": 76832, "name": "Spiderman", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 6, "name": "PC (Microsoft Windows)"}, "region": 8, "y": 2019}, {"id": 157283, "date": 1553212800, "game": {"id": 76882, "name": "Sekiro: Shadows Die Twice", "popularity": 41.39190295640344}, "human": "2019-Mar-22", "m": 3, "platform": {"id": 48, "name": "Xbox"}, "region": 8, "y": 2019}];
let result = {};
data.forEach(({platform, ...release}) => {
release.platforms = [platform.name];
const releaseLookup = result[release.game.id];
if (!releaseLookup) {
result[release.game.id] = release;
} else {
releaseLookup.platforms.push(...release.platforms);
}
});
console.log(Object.values(result));

How to filter nested array with values from another nested array using array.filter and array.some etc

I have the following code which I am using to filter one nested array with values from another nested array, but I am looking for the more fastest method.
const length = this.addedStateItems.length;
for (let i = 0; i < length; i++) {
if (this.addedStateItems[i].cities.length > 0) {
const tempCity: Array<BWCity> = [];
for (let k = 0; k < addedStates.length; k++) {
for (let l = 0; l < addedStates[k].Cities.length; l++) {
const city = this.addedStateItems[i].cities.find(item =>
item.name.toUpperCase().split(' ').join('_') === addedStates[k].Cities[l].CityName
);
if (city) {
tempCity.push(city);
}
}
}
this.addedStateItems[i].cities = tempCity;
}
}
JSON sample is as below:
this.addedStateItems = [
{
"isSelected": false,
"cities": [],
"name": "Alabama",
"code": "AL",
"sequence": 2,
"access": 2,
"id": 2
},
{
"isSelected": false,
"cities": [],
"name": "Alaska",
"code": "AK",
"sequence": 3,
"access": 3,
"id": 3
},
{
"isSelected": false,
"cities": [],
"name": "Arizona",
"code": "AZ",
"sequence": 4,
"access": 4,
"id": 4
},
{
"isSelected": false,
"cities": [],
"name": "Arkansas",
"code": "AR",
"sequence": 5,
"access": 5,
"id": 5
},
{
"isSelected": false,
"cities": [],
"name": "California",
"code": "CA",
"sequence": 6,
"access": 6,
"id": 6
},
{
"isSelected": false,
"cities": [],
"name": "Colorado",
"code": "CO",
"sequence": 7,
"access": 7,
"id": 7
},
{
"isSelected": false,
"cities": [],
"name": "Connecticut",
"code": "CT",
"sequence": 8,
"access": 8,
"id": 8
},
{
"isSelected": false,
"cities": [],
"name": "Delaware",
"code": "DE",
"sequence": 9,
"access": 9,
"id": 9
},
{
"isSelected": false,
"cities": [],
"name": "District of Columbia",
"code": "DC",
"sequence": 10,
"access": 10,
"id": 10
},
{
"isSelected": false,
"cities": [],
"name": "Florida",
"code": "FL",
"sequence": 11,
"access": 11,
"id": 11
},
{
"isSelected": false,
"cities": [],
"name": "Georgia",
"code": "GA",
"sequence": 12,
"access": 12,
"id": 12
},
{
"isSelected": false,
"cities": [],
"name": "Hawaii",
"code": "HI",
"sequence": 13,
"access": 13,
"id": 13
},
{
"isSelected": false,
"cities": [],
"name": "Idaho",
"code": "ID",
"sequence": 14,
"access": 14,
"id": 14
},
{
"isSelected": false,
"cities": [],
"name": "Illinois",
"code": "IL",
"sequence": 15,
"access": 15,
"id": 15
},
{
"isSelected": false,
"cities": [],
"name": "Indiana",
"code": "IN",
"sequence": 16,
"access": 16,
"id": 16
},
{
"isSelected": false,
"cities": [],
"name": "Iowa",
"code": "IA",
"sequence": 17,
"access": 17,
"id": 17
},
{
"isSelected": false,
"cities": [],
"name": "Kansas",
"code": "KS",
"sequence": 18,
"access": 18,
"id": 18
},
{
"isSelected": false,
"cities": [],
"name": "Kentucky",
"code": "KY",
"sequence": 19,
"access": 19,
"id": 19
},
{
"isSelected": false,
"cities": [],
"name": "Louisiana",
"code": "LA",
"sequence": 20,
"access": 20,
"id": 20
},
{
"isSelected": false,
"cities": [],
"name": "Maine",
"code": "ME",
"sequence": 21,
"access": 21,
"id": 21
},
{
"isSelected": false,
"cities": [],
"name": "Maryland",
"code": "MD",
"sequence": 22,
"access": 22,
"id": 22
},
{
"isSelected": false,
"cities": [],
"name": "Massachusetts",
"code": "MA",
"sequence": 23,
"access": 23,
"id": 23
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 9
}
],
"name": "MI Generic",
"code": "MIG",
"id": 1
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 10
}
],
"name": "Port Huron",
"code": "POH",
"id": 9
},
{
"isSelected": false,
"citySequences": [],
"name": "Hudson",
"code": "HUD",
"id": 31
}
],
"name": "Michigan",
"code": "MI",
"sequence": 24,
"access": 24,
"id": 24
},
{
"isSelected": false,
"cities": [],
"name": "Minnesota",
"code": "MN",
"sequence": 25,
"access": 25,
"id": 25
},
{
"isSelected": false,
"cities": [],
"name": "Mississippi",
"code": "MS",
"sequence": 26,
"access": 26,
"id": 26
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
}
],
"name": "Kansas City",
"code": "KCM",
"id": 29
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
}
],
"name": "St Louis",
"code": "STL",
"id": 30
}
],
"name": "Missouri",
"code": "MO",
"sequence": 27,
"access": 27,
"id": 27
},
{
"isSelected": false,
"cities": [],
"name": "Montana",
"code": "MT",
"sequence": 28,
"access": 28,
"id": 28
},
{
"isSelected": false,
"cities": [],
"name": "Nebraska",
"code": "NE",
"sequence": 29,
"access": 29,
"id": 29
},
{
"isSelected": false,
"cities": [],
"name": "New Hampshire",
"code": "NH",
"sequence": 31,
"access": 31,
"id": 31
},
{
"isSelected": false,
"cities": [],
"name": "New Jersey",
"code": "NJ",
"sequence": 32,
"access": 32,
"id": 32
},
{
"isSelected": false,
"cities": [],
"name": "New Mexico",
"code": "NM",
"sequence": 33,
"access": 33,
"id": 33
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "New York City",
"code": "NYC",
"id": 11
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
}
],
"name": "Yonkers",
"code": "NYY",
"id": 12
}
],
"name": "New York",
"code": "NY",
"sequence": 34,
"access": 34,
"id": 34
},
{
"isSelected": false,
"cities": [],
"name": "North Carolina",
"code": "NC",
"sequence": 35,
"access": 35,
"id": 36
},
{
"isSelected": false,
"cities": [],
"name": "North Dakota",
"code": "ND",
"sequence": 36,
"access": 36,
"id": 37
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 12
},
{
"systemId": 3,
"sequence": 3
},
{
"systemId": 4,
"sequence": 3
},
{
"systemId": 5,
"sequence": 3
}
],
"name": "OH Generic",
"code": "OHG",
"id": 13
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "OH RITA",
"code": "OHR",
"id": 14
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 3,
"sequence": 2
},
{
"systemId": 4,
"sequence": 2
},
{
"systemId": 5,
"sequence": 2
}
],
"name": "OH CCA",
"code": "CCA",
"id": 15
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 3
}
],
"name": "Akron",
"code": "AKR",
"id": 16
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 4
}
],
"name": "Cincinnati",
"code": "CIN",
"id": 17
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 6
}
],
"name": "Columbus",
"code": "COL",
"id": 19
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 7
}
],
"name": "Toledo",
"code": "TOL",
"id": 20
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 8
}
],
"name": "Springfield",
"code": "SPR",
"id": 21
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 9
}
],
"name": "Wyoming",
"code": "WYO",
"id": 22
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 10
}
],
"name": "Indian_Hill",
"code": "IHL",
"id": 23
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 11
}
],
"name": "Mansfield",
"code": "MAN",
"id": 24
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 12
}
],
"name": "Crestline",
"code": "CRE",
"id": 25
}
],
"name": "Ohio",
"code": "OH",
"sequence": 37,
"access": 37,
"id": 38
},
{
"isSelected": false,
"cities": [],
"name": "Oklahoma",
"code": "OK",
"sequence": 38,
"access": 38,
"id": 39
},
{
"isSelected": false,
"cities": [],
"name": "Oregon",
"code": "OR",
"sequence": 39,
"access": 39,
"id": 40
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 1
}
],
"name": "PA Generic",
"code": "PAG",
"id": 26
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
},
{
"systemId": 3,
"sequence": 1
},
{
"systemId": 4,
"sequence": 1
},
{
"systemId": 5,
"sequence": 1
}
],
"name": "Philadelphia",
"code": "PHI",
"id": 27
}
],
"name": "Pennsylvania",
"code": "PA",
"sequence": 40,
"access": 40,
"id": 41
},
{
"isSelected": false,
"cities": [],
"name": "Rhode Island",
"code": "RI",
"sequence": 41,
"access": 41,
"id": 42
},
{
"isSelected": false,
"cities": [],
"name": "South Carolina",
"code": "SC",
"sequence": 42,
"access": 42,
"id": 43
},
{
"isSelected": false,
"cities": [],
"name": "Tennessee",
"code": "TN",
"sequence": 44,
"access": 44,
"id": 45
},
{
"isSelected": false,
"cities": [],
"name": "Texas",
"code": "TX",
"sequence": 45,
"access": 45,
"id": 46
},
{
"isSelected": false,
"cities": [],
"name": "Utah",
"code": "UT",
"sequence": 46,
"access": 46,
"id": 47
},
{
"isSelected": false,
"cities": [],
"name": "Vermont",
"code": "VT",
"sequence": 47,
"access": 47,
"id": 48
},
{
"isSelected": false,
"cities": [],
"name": "Virginia",
"code": "VA",
"sequence": 48,
"access": 48,
"id": 49
},
{
"isSelected": false,
"cities": [],
"name": "West Virginia",
"code": "WV",
"sequence": 50,
"access": 50,
"id": 51
},
{
"isSelected": false,
"cities": [],
"name": "Wisconsin",
"code": "WI",
"sequence": 51,
"access": 51,
"id": 52
}
]
Second JSON:
addedStates = [
{
"StateName": "DISTRICT_OF_COLUMBIA",
"Cities": []
},
{
"StateName": "COLORADO",
"Cities": []
},
{
"StateName": "ALABAMA",
"Cities": []
},
{
"StateName": "OHIO",
"Cities": [
{
"CityName": "AKRON"
},
{
"CityName": "CINCINNATI"
}
]
},
{
"StateName": "NEW_YORK",
"Cities": [
{
"CityName": "YONKERS"
}
]
}
]
After filtering first JSON with second JSON I expect this:
result = [
{
"isSelected": false,
"cities": [],
"name": "Alabama",
"code": "AL",
"sequence": 2,
"access": 2,
"id": 2
},
{
"isSelected": false,
"cities": [],
"name": "Colorado",
"code": "CO",
"sequence": 7,
"access": 7,
"id": 7
},
{
"isSelected": false,
"cities": [],
"name": "District of Columbia",
"code": "DC",
"sequence": 10,
"access": 10,
"id": 10
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 2
},
{
"systemId": 2,
"sequence": 1
}
],
"name": "Yonkers",
"code": "NYY",
"id": 12
}
],
"name": "New York",
"code": "NY",
"sequence": 34,
"access": 34,
"id": 34
},
{
"isSelected": false,
"cities": [
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 3
}
],
"name": "Akron",
"code": "AKR",
"id": 16
},
{
"isSelected": false,
"citySequences": [
{
"systemId": 1,
"sequence": 4
}
],
"name": "Cincinnati",
"code": "CIN",
"id": 17
}
],
"name": "Ohio",
"code": "OH",
"sequence": 37,
"access": 37,
"id": 38
}
]

How to find all unique paths in tree structure

root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
I have an object array like tree structure like above, I want to get all unique paths in like this
Food->Dry Food Items->Local Dry Food Items
Food->Dry Food Items->Thai Dry Food Items
Food->Dry Food Items->Others
Food->Fruits
------
------
This is my object
[
{
"id": 1,
"name": "Food",
"parent_id": 0,
"children": [
{
"id": 5,
"name": "Dry Food Items",
"parent_id": 1,
"children": [
{
"id": 11,
"name": "Local Dry Food Items",
"parent_id": 5
},
{
"id": 12,
"name": "Thai Dry Food Items",
"parent_id": 5
},
{
"id": 60,
"name": "Others",
"parent_id": 5
}
]
},
{
"id": 6,
"name": "Fruits",
"parent_id": 1
},
{
"id": 7,
"name": "LG Branded",
"parent_id": 1
},
{
"id": 8,
"name": "Meat",
"parent_id": 1
},
{
"id": 9,
"name": "Sea food",
"parent_id": 1
},
{
"id": 10,
"name": "Vegetables",
"parent_id": 1,
"children": [
{
"id": 14,
"name": "Local Vegetables",
"parent_id": 10
},
{
"id": 15,
"name": "Thai Vegetables",
"parent_id": 10
}
]
},
{
"id": 38,
"name": "Frozen",
"parent_id": 1
},
{
"id": 39,
"name": "IP Kitchen",
"parent_id": 1,
"children": [
{
"id": 40,
"name": "IP Meat",
"parent_id": 39
},
{
"id": 41,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 42,
"name": "IP Ingredients",
"parent_id": 39
},
{
"id": 43,
"name": "IP Sauce",
"parent_id": 39
},
{
"id": 44,
"name": "IP Seafood",
"parent_id": 39
},
{
"id": 45,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 46,
"name": "IP Desert",
"parent_id": 39
}
]
}
]
},
{
"id": 2,
"name": "Beverage",
"parent_id": 0,
"children": [
{
"id": 16,
"name": "Bar",
"parent_id": 2
},
{
"id": 17,
"name": "Coffee & Tea",
"parent_id": 2
},
{
"id": 18,
"name": "In Can",
"parent_id": 2
},
{
"id": 19,
"name": "Water",
"parent_id": 2
},
{
"id": 47,
"name": "IP Bar",
"parent_id": 2
}
]
},
{
"id": 3,
"name": "Disposable",
"parent_id": 0,
"children": [
{
"id": 21,
"name": "Disposable",
"parent_id": 3
}
]
},
{
"id": 4,
"name": "SOE",
"parent_id": 0,
"children": [
{
"id": 20,
"name": "Cleaning Materials",
"parent_id": 4
},
{
"id": 22,
"name": "Chinaware",
"parent_id": 4
}
]
}
];
I get to all the nodes in the tree
function traverse(categories) {
categories.forEach(function (category) {
if (category.children && category.children.length) {
traverse(category.children);
}
else {
}
}, this);
}
You can use recursion and create a function using forEach loop.
var arr = [{"id":1,"name":"Food","parent_id":0,"children":[{"id":5,"name":"Dry Food Items","parent_id":1,"children":[{"id":11,"name":"Local Dry Food Items","parent_id":5},{"id":12,"name":"Thai Dry Food Items","parent_id":5},{"id":60,"name":"Others","parent_id":5}]},{"id":6,"name":"Fruits","parent_id":1},{"id":7,"name":"LG Branded","parent_id":1},{"id":8,"name":"Meat","parent_id":1},{"id":9,"name":"Sea food","parent_id":1},{"id":10,"name":"Vegetables","parent_id":1,"children":[{"id":14,"name":"Local Vegetables","parent_id":10},{"id":15,"name":"Thai Vegetables","parent_id":10}]},{"id":38,"name":"Frozen","parent_id":1},{"id":39,"name":"IP Kitchen","parent_id":1,"children":[{"id":40,"name":"IP Meat","parent_id":39},{"id":41,"name":"IP Starter","parent_id":39},{"id":42,"name":"IP Ingredients","parent_id":39},{"id":43,"name":"IP Sauce","parent_id":39},{"id":44,"name":"IP Seafood","parent_id":39},{"id":45,"name":"IP Starter","parent_id":39},{"id":46,"name":"IP Desert","parent_id":39}]}]},{"id":2,"name":"Beverage","parent_id":0,"children":[{"id":16,"name":"Bar","parent_id":2},{"id":17,"name":"Coffee & Tea","parent_id":2},{"id":18,"name":"In Can","parent_id":2},{"id":19,"name":"Water","parent_id":2},{"id":47,"name":"IP Bar","parent_id":2}]},{"id":3,"name":"Disposable","parent_id":0,"children":[{"id":21,"name":"Disposable","parent_id":3}]},{"id":4,"name":"SOE","parent_id":0,"children":[{"id":20,"name":"Cleaning Materials","parent_id":4},{"id":22,"name":"Chinaware","parent_id":4}]}]
function getNames(data) {
var result = [];
function loop(data, c) {
data.forEach(function (e) {
var name = !c.length ? e.name : c + '->' + e.name;
if (e.children) { loop(e.children, name); }
else {
result.push({ name: name });
}
});
}
loop(data, '');
return result;
}
console.log(getNames(arr))

AngularJS with Jquery Chosen Default Load

I'm using AngularJS and the jquery chosen plugin to populate a multiple select form. My Angular code has a service that is calling a node.js web service. The option list for the chosen select box is being populated from a json file while the value is being stored within model from the nodejs service. I've been using this link to guide me but now seem to be stuck.
I've included the code here.
My chosen options are being populated as such:
[
{"id": 1, "name": "00:00", "value": 0},
{"id": 2, "name": "00:15", "value": 900000},
{"id": 3, "name": "00:30", "value": 1800000}
...
]
But my model is being stored as:
meal.dinnerTimes = ["06:15","18:30"]
So when the model is loaded by the service and controller. The select box is always blank where I would expect to see
"06:15" and "18:30" already populated in this case
Any help would be appreciated
As you only have value with you while assigning model, use select as label for value in array syntax to have only value property assigned to the model.
Also note that you are not invoking callback function provided in MealSvc factory hence, value of model will never get set!
Plunker Demo
var app = angular.module("MealApp", ['MealService']);
var data = [{
"id": 1,
"name": "00:00",
"value": 0
}, {
"id": 2,
"name": "00:15",
"value": 900000
}, {
"id": 3,
"name": "00:30",
"value": 1800000
}, {
"id": 4,
"name": "00:45",
"value": 2700000
}, {
"id": 5,
"name": "01:00",
"value": 3600000
}, {
"id": 6,
"name": "01:15",
"value": 4500000
}, {
"id": 7,
"name": "01:30",
"value": 5400000
}, {
"id": 8,
"name": "01:45",
"value": 6300000
}, {
"id": 9,
"name": "02:00",
"value": 7200000
}, {
"id": 10,
"name": "02:15",
"value": 8100000
}, {
"id": 11,
"name": "02:30",
"value": 9000000
}, {
"id": 12,
"name": "02:45",
"value": 9900000
}, {
"id": 13,
"name": "03:00",
"value": 10800000
}, {
"id": 14,
"name": "03:15",
"value": 11700000
}, {
"id": 15,
"name": "03:30",
"value": 12600000
}, {
"id": 16,
"name": "03:45",
"value": 13500000
}, {
"id": 17,
"name": "04:00",
"value": 14400000
}, {
"id": 18,
"name": "04:15",
"value": 15300000
}, {
"id": 19,
"name": "04:30",
"value": 16200000
}, {
"id": 20,
"name": "04:45",
"value": 17100000
}, {
"id": 21,
"name": "05:00",
"value": 18000000
}, {
"id": 22,
"name": "05:15",
"value": 18900000
}, {
"id": 23,
"name": "05:30",
"value": 19800000
}, {
"id": 24,
"name": "05:45",
"value": 20700000
}, {
"id": 25,
"name": "06:00",
"value": 21600000
}, {
"id": 26,
"name": "06:15",
"value": 22500000
}, {
"id": 27,
"name": "06:30",
"value": 23400000
}, {
"id": 28,
"name": "06:45",
"value": 24300000
}, {
"id": 29,
"name": "07:00",
"value": 25200000
}, {
"id": 30,
"name": "07:15",
"value": 26100000
}, {
"id": 31,
"name": "07:30",
"value": 27000000
}, {
"id": 32,
"name": "07:45",
"value": 27900000
}, {
"id": 33,
"name": "08:00",
"value": 28800000
}, {
"id": 34,
"name": "08:15",
"value": 29700000
}, {
"id": 35,
"name": "08:30",
"value": 30600000
}, {
"id": 36,
"name": "08:45",
"value": 31500000
}, {
"id": 37,
"name": "09:00",
"value": 32400000
}, {
"id": 38,
"name": "09:15",
"value": 33300000
}, {
"id": 39,
"name": "09:30",
"value": 34200000
}, {
"id": 40,
"name": "09:45",
"value": 35100000
}, {
"id": 41,
"name": "10:00",
"value": 36000000
}, {
"id": 42,
"name": "10:15",
"value": 36900000
}, {
"id": 43,
"name": "10:30",
"value": 37800000
}, {
"id": 44,
"name": "10:45",
"value": 38700000
}, {
"id": 45,
"name": "11:00",
"value": 39600000
}, {
"id": 46,
"name": "11:15",
"value": 40500000
}, {
"id": 47,
"name": "11:30",
"value": 41400000
}, {
"id": 48,
"name": "11:45",
"value": 42300000
}, {
"id": 49,
"name": "12:00",
"value": 43200000
}, {
"id": 50,
"name": "12:15",
"value": 44100000
}, {
"id": 51,
"name": "12:30",
"value": 45000000
}, {
"id": 52,
"name": "12:45",
"value": 45900000
}, {
"id": 53,
"name": "13:00",
"value": 46800000
}, {
"id": 54,
"name": "13:15",
"value": 47700000
}, {
"id": 55,
"name": "13:30",
"value": 48600000
}, {
"id": 56,
"name": "13:45",
"value": 49500000
}, {
"id": 57,
"name": "14:00",
"value": 50400000
}, {
"id": 58,
"name": "14:15",
"value": 51300000
}, {
"id": 59,
"name": "14:30",
"value": 52200000
}, {
"id": 60,
"name": "14:45",
"value": 53100000
}, {
"id": 61,
"name": "15:00",
"value": 54000000
}, {
"id": 62,
"name": "15:15",
"value": 54900000
}, {
"id": 63,
"name": "15:30",
"value": 55800000
}, {
"id": 64,
"name": "15:45",
"value": 56700000
}, {
"id": 65,
"name": "16:00",
"value": 57600000
}, {
"id": 66,
"name": "16:15",
"value": 58500000
}, {
"id": 67,
"name": "16:30",
"value": 59400000
}, {
"id": 68,
"name": "16:45",
"value": 60300000
}, {
"id": 69,
"name": "17:00",
"value": 61200000
}, {
"id": 70,
"name": "17:15",
"value": 62100000
}, {
"id": 71,
"name": "17:30",
"value": 63000000
}, {
"id": 72,
"name": "17:45",
"value": 63900000
}, {
"id": 73,
"name": "18:00",
"value": 64800000
}, {
"id": 74,
"name": "18:15",
"value": 65700000
}, {
"id": 75,
"name": "18:30",
"value": 66600000
}, {
"id": 76,
"name": "18:45",
"value": 67500000
}, {
"id": 77,
"name": "19:00",
"value": 68400000
}, {
"id": 78,
"name": "19:15",
"value": 69300000
}, {
"id": 79,
"name": "19:30",
"value": 70200000
}, {
"id": 80,
"name": "19:45",
"value": 71100000
}, {
"id": 81,
"name": "20:00",
"value": 72000000
}, {
"id": 82,
"name": "20:15",
"value": 72900000
}, {
"id": 83,
"name": "20:30",
"value": 73800000
}, {
"id": 84,
"name": "20:45",
"value": 74700000
}, {
"id": 85,
"name": "21:00",
"value": 75600000
}, {
"id": 86,
"name": "21:15",
"value": 76500000
}, {
"id": 87,
"name": "21:30",
"value": 77400000
}, {
"id": 88,
"name": "21:45",
"value": 78300000
}, {
"id": 89,
"name": "22:00",
"value": 79200000
}, {
"id": 90,
"name": "22:15",
"value": 80100000
}, {
"id": 91,
"name": "22:30",
"value": 81000000
}, {
"id": 92,
"name": "22:45",
"value": 81900000
}, {
"id": 93,
"name": "23:00",
"value": 82800000
}, {
"id": 94,
"name": "23:15",
"value": 83700000
}, {
"id": 95,
"name": "23:30",
"value": 84600000
}, {
"id": 96,
"name": "23:45",
"value": 85500000
}];
app.directive('chosen', function() {
var linker = function(scope, element, attr) {
scope.$watch('availableTimes', function() {
element.triggerHandler('chosen:updated');
});
scope.$watch('MealSvc.get()', function() {
element.triggerHandler('chosen:updated');
});
element.chosen({
width: "95%"
});
};
return {
restrict: 'A',
link: linker
}
});
app.controller("MealCtrl", function MealCtrl($scope, $window, $http, MealSvc) {
$scope.times = [];
$scope.availableTimes = [];
$scope.fetchTimes = function() {
$scope.availableTimes = data;
}
$scope.fetchTimes();
MealSvc.get(function(res) {
$scope.times = res.dinnerTimes;
});
});
angular.module('MealService', []).factory('MealSvc', function($http) {
return {
get: function(response) {
response({
"name": "Second Tea",
"dinnerTimes": [46800000, 57600000]
})
}
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" data-semver="2.2.0" data-require="jquery#*"></script>
<link data-require="chosen#*" data-semver="1.0.0" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.min.css" />
<script data-require="chosen#*" data-semver="1.0.0" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.jquery.min.js"></script>
<script data-require="chosen#*" data-semver="1.0.0" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.0/chosen.proto.min.js"></script>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js" data-require="angular.js#1.2.x"></script>
<script src="app.js"></script>
</head>
<body>
<div class="container" ng-app="MealApp" ng-controller="MealCtrl">
<div class="row">
<select data-placeholder="Select Dinner Time" multiple class="chzn-select" chosen ng-model="times" ng-options="times.value as times.name for times in availableTimes"></select>
</div>
</div>
</body>
</html>

Categories

Resources