Meteor: How to structure Mongo data from Google Sheets - arrays

The BSON data list contains very unneeded values as shown below. The problem am I having is returning an array subset from Mongo so that I could display it in a template using Meteor.
Mongo stores the data as such:
meteor:PRIMARY> db.ga_spreadsheet.find().pretty()
{
"_id" : "7YLifh9C6Gp8HPCTm",
"spreadsheet" : "NBATEST",
"header" : null,
"cells" : {
"1" : {
"1" : {
"row" : "1",
"col" : "1",
"value" : "name"
},
"2" : {
"row" : "1",
"col" : "2",
"value" : "position"
},
"3" : {
"row" : "1",
"col" : "3",
"value" : "salary"
& client/view.js does the pull request
GASpreadsheet = new Mongo.Collection('GASpreadsheet');
if (Meteor.isClient) {
Meteor.call("spreadsheet/fetch","1kdRtyhrvXN1aEf35NwtA8pzHfjc9-lNwV76CE_Zr_g");
spreadsheetData = GASpreadsheet.findOne({spreadsheet: 'NBATEST'})
Template.nba.helpers({
'player': function(){
var currentUserId = Meteor.userId();
return GASpreadsheet.find({ createdBy: currentUserId },
{ sort: {score: -1, name: 1} });
},
cellState: function(x, y) {
if(screenArray[x][y] === 1){
return 'live';
}
else {
return 'dead';
}
},
'selectedClass': function(){
var playerId = this._id;
var selectedPlayer = Session.get('selectedPlayer');
if(playerId == selectedPlayer){
return "selected"
}
},
'selectedPlayer': function(){
var selectedPlayer = Session.get('selectedPlayer');
return GASpreadsheet.findOne({ _id: selectedPlayer });
}
});
}
view/main.html
<template name="nba">
<div class="gridWrapper">
{{#each row in rows}}
<div class="row">
{{#let rowIndex=#index}}
{{#each cell in row}}
<div class="cell {{cellState rowIndex #index}}">{{this}}</div>
{{/each}}
{{/let}}
</div>
{{/each}}
</div>
</template>

Related

AngularJS count matching occurrences in an array

Objective: I want to count the occurrences of an id matching between arrays
The code below finds the match but we need to count the result
var arr1 = [
{
"image" : "FF815_1.jpg",
"id" : "NO"
}, {
"image" : "FF815_0.jpg",
"id" : "NO"
}, {
"image" : "FF815_4.jpg",
"id" : "NO"
}, {
"image" : "PIOJD_2.jpg",
"id" : "NO"
}, {
"image" : "PIOJD_4.jpg",
"id": "JD"
} ];
var arr2 = [
{
"image" : "FF815_1.jpg",
"id" : "NO"
}, {
"image" : "FF815_0.jpg",
"id" : "NO"
}, {
"image" : "FF815_4.jpg",
"id" : "NO"
}, {
"image" : "PIOJD_2.jpg",
"id" : "JD"
}, {
"image" : "PIOJD_4.jpg",
"id" : "JD"
} ];
angular.forEach(arr1, function(value1, key1) {
angular.forEach(arr2, function(value2, key2) {
if (value1.id === value2.id) {
console.log(value2.id);
}
});
});
Heres a fiddle: https://jsfiddle.net/62s0ae7v/
Just add a simple counter like :
var counter = 0;
angular.forEach(arr1, function(value1, key1) {
angular.forEach(arr2, function(value2, key2) {
if (value1.image === value2.image) {
console.log(value2.image);
counter++;
}
});
});
console.log(counter);

how to create an array with subobjects of another array in angular

Hi guys i am really stuck here,
i am creating a basic comment rating system which has subcomment feature
as you see in comments array > "subcomments" entries are objects and i want create array with them but i dont know how because theyre in an array.
i tried this:
this.CommentsArray.forEach((item, i) => {
if([item]["subcomments"] != undefined) {
[item]["subcomments"].forEach((subitem, i) => {
subitem = this.subCommentsArray[i];
})
}
but its not working. any suggestions ?
thanks..
edit:
my data structure is like that:
"comments" : {
"d4894e47181e43b7a0fb8b7699" : {
"commentDate" : "2018-08-13T19:49:49.814Z",
"commentPhotoExistence" : false,
"commentRate" : "3",
"commentRateState" : false,
"commentTagState" : false,
"commentText" : "",
"commentUID" : "d4894e47181e43b7a0fb8b7699",
"commenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"commenterUsername" : "Test2",
"subcomments" : {
"7c9f985d39334573b0211d18f1" : {
"subcommentDate" : "2018-09-01T11:57:10.523Z",
"subcommentText" : "EXAMPLE TEXT1",
"subcommentUID" : "7c9f985d39334573b0211d18f1",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"a1197fb32eae483ba8e910a748" : {
"subcommentDate" : "2018-09-01T11:29:57.514Z",
"subcommentText" : "EXAMPLE TEST 2",
"subcommentUID" : "a1197fb32eae483ba8e910a748",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"ad648a09a67c431c821bf8f290" : {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"f7682639d4584642a18a9a4ad7" : {
"subcommentDate" : "2018-09-02T15:41:38.162Z",
"subcommentText" : "example4",
"subcommentUID" : "f7682639d4584642a18a9a4ad7",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
}
}
the problem here is the subcomment id's are randomly added. in normally if it was Array i can use .forEach for use every item but there are random uid's between subcomments and subcomment properties. So all i want is create an array like including them, array will be like;
0: { "subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
1: {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
2: {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
etc.
Your code is shadowing the variable i.
You're using is on your first line this.CommentsArray.forEach((item, i) => { then again on the subcomments.
this.CommentsArray.forEach((item, i) => {
if([item]["subcomments"] != undefined) {
[item]["subcomments"].forEach((subitem, z) => {
subitem = this.subCommentsArray[z];
})
}
});
But that's not the real problem. You're trying to iterate an object, not an array. The result will be coming back as;
{
"d48...669": {
"commentDate": '2019-08-13',
"subcomments": {
"7c9...8f1": {
},
"a11...748": {
}
}
}
}
You should iterate by the keys of the object;
Object.keys(this.CommentsArray).forEach(commentId => {
const comment = this.CommentsArray[commentId];
Object.keys(comment['subcomments']).forEach(subCommentId => {
const subComment = comment['subcomments'][subCommentId];
)};
});

filter the records using loadash angularjs

var arr = Array [ Object, Object ]
into arr {
"name" : "xyz",
"age" : "22",
"sports" : Array[
{
id:1,
name : "xyz",
},
{
id:2,
name : "yyy",
},
]
}
var obj = Object { name: "JXZ", id :1};
var response = _.filter(arr, function(user) {
return user.sports.id === obj.id;
});
filter the record that match with obj's id from arr's sports.id return array object.
I'm suppose to put loop but i guess that not right solution if please
Please try this below code.
<script>
var app = angular.module('app',[]);
app.controller('ctrl',function($scope){
$scope.arr = Array [ Object, Object ];
$scope.arr = {
"name" : "xyz",
"age" : "22",
"sports" : [
{
id:1,
name : "xyz",
},
{
id:2,
name : "yyy",
}
]
}
var obj = { name: "JXZ", id :1};
var filteredSports = _.filter($scope.arr.sports, function (sport) {
return sport.id == obj.id;
})
$scope.arr.sports = filteredSports;
console.log($scope.arr);
})
</script>

Angular js select specific data from json using date or filter it by date

I am new to angular js , how to select specific or certain data from json array.Return data from an array which date is equal to today , and also get data from json of this current week(all data of the current week), and also of the data of this current month. Thanks
$scope.Combination = [
{
"Name" : "HelloWorld",
"Type" : "Phyton",
"Date": "01/11/2018"
},
{
"Name" : "HelloWorld",
"Type" : "C#",
"Date": "01/08/2018"
},
{
"Name" : "HelloWorld",
"Type" : "JS",
"Date": "01/04/2018"
},
{
"Name" : "HelloWorld",
"Type" : "Ruby",
"Date": "01/07/2018"
},
{
"Name" : "HelloWorld",
"Type" : "C",
"Date": "01/010/2018"
}
];
<!DOCTYPE html>
<html ng-app="ToDo">
<head>
<title>Create Angularjs To Do Application with demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
<div ng-controller="todoController">
<ul>
<li ng-repeat="todo in combinations|getCombinations ">
{{todo.Date}}
</li>
</ul>
{{name}}
</div>
<script>
var app = angular.module('ToDo',[])
app.controller('todoController',['$scope', function($scope){
$scope.combinations = [
{
"Name" : "HelloWorld",
"Type" : "Phyton",
"Date": "01/11/2018"
},
{
"Name" : "HelloWorld",
"Type" : "C#",
"Date": "01/08/2018"
},
{
"Name" : "HelloWorld",
"Type" : "JS",
"Date": "01/04/2018"
},
{
"Name" : "HelloWorld",
"Type" : "Ruby",
"Date": "01/07/2018"
},
{
"Name" : "HelloWorld",
"Type" : "C",
"Date": "01/10/2018"
}
];
}]);
angular.module('ToDo').filter('getCombinations', function($filter) {
return function (combinations) {
var month_filtered_list = [];
for (var i = 0; i < combinations.length; i++) {
var thirty_days_ago = new Date().getTime() - 30*24*60*60*1000;
var com_date = new Date(combinations[i]["Date"]).getTime();
if (com_date >= thirty_days_ago) {
month_filtered_list.push(combinations[i]);
}
}
return month_filtered_list;
}
});
</script>
</body>
</html>
Here you can use filter to show combination:
For monthly combination:
angular.module('ToDo').filter('getCombinations', function($filter) {
return function (combinations) {
var month_filtered_list = [];
for (var i = 0; i < combinations.length; i++) {
var thirty_days_ago = new Date().getTime() - 30*24*60*60*1000;
var com_date = new Date(combinations[i]["Date"]).getTime();
if (com_date >= thirty_days_ago) {
month_filtered_list.push(combinations[i]);
}
}
return month_filtered_list;
}
});
DOM looks like this
<tr ng-repeat="order in combinations|getCombinations">
Similarly you can do for the weak also by just changing the
var thirty_days_ago = new Date().getTime() - 30*24*60*60*1000;
to
var seven_days_ago = new Date().getTime() - 7*24*60*60*1000;
for present day:
.filter('getTodayCombinations', function() {
return function (combinations) {
var today_filtered_list = [];
for (var i = 0; i < combinations.length; i++) {
var com_date = new Date(combinations[i].Date).getTime();
if (com_date == new Date().getTime()) {
today_filtered_list.push(combinations[i]);
}
}
return month_filtered_list;
}
});

AngularJS | how to orderBy with a multidimensional Object?

I want to get my ng-repeat to be ordered by rank, how would I write the expression?
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="(key, value) in cars | orderBy: cars[key].rank ">
{{cars[key].longName}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.cars = {
"BTC" : {
"longName" : "Bitcoin",
"rank" : 1
},
"BCH" : {
"longName" : "Bitcoin Cash",
"rank" : 3
},
"ETH" : {
"longName" : "Ethereum",
"rank" : 2
},
"ETC" : {
"longName" : "Ethereum Classic",
"rank" : 15
},
"IOTA" : {
"longName" : "IOTA",
"rank" : 4
},
"XRP" : {
"longName" : "Ripple",
"rank" : 6
},
"XVG" : {
"longName" : "Verge",
"rank" : 5
}
};
});
</script>
Order by can only be applied to arrays and not objects. You must convert your object to an array or create your own filter : https://justinklemm.com/angularjs-filter-ordering-objects-ngrepeat/

Resources