How to reduce array's numeric values, skipping strings - arrays

Using Google Sheets' Script Editor, I need to calculate the average of each column of a table such as this one:
+---------+---------+---------+---------+
| | User 1 | User 2 | User 3 |
+---------+---------+---------+---------+
| | 20/1/17 | 21/1/17 | 22/1/17 |
+---------+---------+---------+---------+
| Movie 1 | 6 | 2 | 0 |
+---------+---------+---------+---------+
| Movie 2 | 2 | 1 | 5 |
+---------+---------+---------+---------+
| Movie 3 | 3 | 1 | 2 |
+---------+---------+---------+---------+
I want the result to be:
+---------+---------+---------+
| User 1 | User 2 | User 3 |
+---------+---------+---------+
| 3.6 | 1.3 | 2.3 |
+---------+---------+---------+
I have this code, which works if I use it on a table composed by numbers only. The problem is my table has strings in it, so the .reduce returns a #NUM! error (Result was not a number).
var sum = array.reduce(function(r, e, i) {
e.forEach(function(a, j) {
r[j] = (r[j] || 0) + a
})
if (i == array.length - 1) r = r.map(function(el) {
return el / array.length
});
return r;
}, []);
return sum;
How can I perform the average with the numeric values only? I tried using .filter, but it didn't seem to work on Google Apps Script Editor, or maybe I was just using it wrong (very likely).
This is the condition I was using to filter:
(typeof el !== 'number')

How about following sample? You can use this on Google Apps Script.
var array = [
[,, 'User 1', 'User 2', 'User 3'],
['Movie 1', '20/1/17', 6.0, 2.0, 5.0],
['Movie 2', '21/1/17', 2.0, 0.0, 2.0],
['Movie 3', '22/1/17', 3.0, 1.0, 3.0]
];
var sum = array.reduce(function(r, e, i) {
e.forEach(function(a, j) {
r[j] = (parseInt(r[j]) || 0) + a;
})
if (i == array.length - 1) r = r.map(function(el) {
return isFinite(el) ? el : "";
});
return r;
}, []);
return [array[0].splice(2, 3), [x for each (x in sum)].splice(2, 3)];
>>> [[User 1, User 2, User 3], [11.0, 3.0, 10.0]]

Related

Error: Cannot create styled-component for component: undefined

j
X:/rstfrontend/packages/src/constants.js:4
1 | // #flow
2 |
3 | declare var SC_DISABLE_SPEEDY: ?boolean;
> 4 | declare var __VERSION__: string;
5 |
6 | export const SC_ATTR: string =
7 | (typeof process !== 'undefined' && (process.env.REACT_APP_SC_ATTR || process.env.SC_ATTR)) ||
Another problem is that it refers to a path that doesn't exist.
When clicked on View Compiled it links to main.chunk.
Maybe a bug with webpack?
j
http://localhost:3000/static/js/vendors~main.chunk.js:187248:30
187245 | n[r - 1] = arguments[r];
187246 | }
187247 |
> 187248 | throw false ? undefined : new Error(D.apply(void 0, [R[e]].concat(n)).trim());
| ^ 187249 | }
187250 |
187251 | var T = function () {

Why my laravel passport stopped working when migrated to vagrant from build-in server?

So I had to implement facebook login, which requires https connection, before I was using php artisan serve, but it only allows http, now I decided to migrate whole project to vagrant. Everything works fine but passport.
I checked and found that theese two lines are causing internal error 500
auth()->attempt($loginData, true);
auth()->user()->createToken('authToken')->accessToken;
when removed, I get no error. My whole login function look like this:
public function login(Request $request)
{
$loginData = $request->validate([
'name' => 'required',
'password' => 'required'
]);
$a = auth()->attempt($loginData, true);
if(!$a) {
return response(['message'=>'Invalid credentials', 'auth' => false]);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken, 'auth' => true, "attempt" => $a, "auth()" => auth()]);
}
In addition, I've created users via tinker, tokens and users were stored.
here is my db data
mysql> select * from users;
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| id | name | password | remember_token | created_at | updated_at |
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
| 1 | admin | $2y$10$V8fM6Vvc7xOJxN/LPyImUeQNzG9/k3tSsaF8qf1NsxrzK9B3VPmsa | NULL | 2019-08-26 15:22:02 | 2019-08-26 15:22:02 |
| 2 | demis | $2y$10$oRvcL0ZC2NQuGXE446GhfO1KBcAw3h8d/TGQmODL/AFHnat1I.Yvq | NULL | 2019-08-26 15:45:27 | 2019-08-26 15:45:27 |
+----+-------+--------------------------------------------------------------+----------------+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> select * from oauth_access_tokens;
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
| id | user_id | client_id | name | scopes | revoked | created_at | updated_at | expires_at |
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
| 2e82923d6fdc1cc98580b683952b7b96d555e67d6b7e06b8080f8a55227b6c1d7a755fc21b570941 | NULL | 1 | authToken | [] | 0 | 2019-08-26 15:21:52 | 2019-08-26 15:21:52 | 2020-08-26 15:21:52 |
| 610f8fd1f2212ab257810de88605bff4e419f26477bcf63f8dfe20018d9bcf74797bb9c817d6450a | NULL | 1 | authToken | [] | 0 | 2019-08-26 15:45:19 | 2019-08-26 15:45:19 | 2020-08-26 15:45:19 |
+----------------------------------------------------------------------------------+---------+-----------+-----------+--------+---------+---------------------+---------------------+---------------------+
What's wrong, Why I can't login, maybe there's something wrong with configuration?
Run php artisan passport:install on the new server.

Error in reactjs +materail-ui

I am getting this error while building a simple app which has two routes one to '/' and other to '/search'. I have used various components of material-ui.
The problem is when I route to '/search' the following error is thrown:
I have tried all necessary things but unable to figure out what's wrong?
I am just posting half of code here:
function capitalizeFirstLetter(string) {
28 | process.env.NODE_ENV !== "production" ? (0, _warning2.default)(typeof string === 'string', 'Material-UI: capitalizeFirstLetter(string) expects a string argument.') : void 0;
29 |
> 30 | return string.charAt(0).toUpperCase() + string.slice(1);
31 | } // weak
32 |
33 | function contains(obj, pred) {
View compiled
Typography
node_modules/material-ui/Typography/Typography.js:114
111 | other = (0, _objectWithoutProperties3.default)(props, ['align', 'classes', 'className', 'component', 'color', 'gutterBottom', 'headlineMapping', 'noWrap', 'paragraph', 'type']);
112 |
113 |
> 114 | var className = (0, _classnames2.default)(classes.root, classes[type], (_classNames = {}, (0, _defineProperty3.default)(_classNames, classes['color' + (0, _helpers.capitalizeFirstLetter)(color)], color !== 'default'), (0, _defineProperty3.default)(_classNames, classes.noWrap, noWrap), (0, _defineProperty3.default)(_classNames, classes.gutterBottom, gutterBottom), (0, _defineProperty3.default)(_classNames, classes.paragraph, paragraph), (0, _defineProperty3.default)(_classNames, classes['align' + (0, _helpers.capitalizeFirstLetter)(align)], align !== 'inherit'), _classNames), classNameProp);
115 |
116 | var Component = componentProp || (paragraph ? 'p' : headlineMapping[type]) || 'span';
117 |
View compiled
mountIndeterminateComponent
node_modules/react-dom/cjs/react-dom.development.js:8032
8029 | warning(false, "The <%s /> component appears to have a render method, but doesn't extend React.Component. " + 'This is likely to cause errors. Change %s to extend React.Component instead.', componentName, componentName);
8030 | }
8031 | ReactCurrentOwner.current = workInProgress;
> 8032 | value = fn(props, context);
8033 | }
8034 | // React DevTools reads this flag.
8035 | workInProgress.effectTag |= PerformedWork;
View compiled
beginWork
node_modules/react-dom/cjs/react-dom.development.js:8221
8218 |
8219 | switch (workInProgress.tag) {
8220 | case IndeterminateComponent:
> 8221 | return mountIndeterminateComponent(current, workInProgress, renderExpirationTime);
8222 | case FunctionalComponent:
8223 | return updateFunctionalComponent(current, workInProgress);
I see in Typography.js that it use capitalizeFirstLetter for color and align.
Check if you inserted there something that is not a string.

CakePHP 3 - Query builder properly not working for HasMany associations

Table : Services
+-----+--------------+
| id | title |
+-----+--------------+
| 1 | Service 1 |
+-----+--------------+
| 2 | Service 2 |
+-----+--------------+
Table : Workshops { HasMany WorkshopServices }
+-----+---------------+
| id | title |
+-----+---------------+
| 1 | Workshop 1 |
+-----+---------------+
| 2 | Workshop 2 |
+-----+---------------+
Table : WorkshopServices
+-----+--------------+-------------+
| id | workshop_id | service_id |
+-----+--------------+-------------+
| 1 | 1 | 1 |
+-----+--------------+-------------+
| 2 | 1 | 2 |
+-----+--------------+-------------+
I want to find Workshops by service_id
My Query
$this->Workshops->find()
->contain([
'WorkshopServices'
])
->where([
'WorkshopServices.service_id IN'=>[1,2,3]
]);
Query Result
Unknown column `WorkshopServices.service_id` in 'where clause'
Actually Workshops table is not generating any JOIN with WorkshopServices table.
How can I write the query to get proper result from Query Builder?
Use matching:
$array = [1,2,3];
$this->Workshops->find()
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array])
});
I updated #GabrielFerreira's query and Grouping the rows by WorkshopServices.workshop_id, This solution meet my Problem
$array = [1,2,3];
$this->Workshops->find()
->select([
'title'=>'Workshops.title',
'id'=>'Workshops.id',
])
->matching('WorkshopServices', function ($q) use($array) {
return $q->where(['WorkshopServices.service_id IN' => $array]);
})
->group(['WorkshopServices.workshop_id']);

ng-options get referenced document and display its field as value for dropdown population

I'm working with meteor+angular. The following loops through all the documents in the items collection. Within the loop, once a meal == true is hit, it loops the related combos to make dropdowns that use as options, documents from the comboitems collection.
// controller.ng.js
angular.module("myApp").controller("menuCtrl",
function($scope, $stateParams, $meteor){
$scope.items = $meteor.collection(Items);
$scope.combos = $meteor.collection(Combos);
$scope.comboitems = $meteor.collection(Comboitems);
}
);
// view.ng.html
<div ng-repeat="item in items">
<div ng-if="true === item.meal">
<ul>
<li ng-repeat="combo in combos | filter: {itemId: item._id}">
<select ng-model="comboitems" ng-options="comboitem.itemId for comboitem in (comboitems | filter: {comboId: combo._id})"></select>
</li>
</ul>
</div>
</div>
Using the data provided below, once I hit Pizza Meal I will have two dropdowns: Choose your pizza and Choose your drink, where in the former I will be able to choose between pizza 1 and pizza 2, while in the latter between drink 1 and drink 2.
This is the result
My issue is that I can't find a way to render the item.name, knowing its id in the comboitems collection once the <select> is being rendered.
Here is a sample data showing how the relations work:
items collection:
_id | name | price | meal
---------------------------------
a | pizza 1 | 5.00 | false
b | pizza 2 | 6.00 | false
c | drink 1 | 1.00 | false
d | drink 2 | 1.00 | false
e | Pizza meal | 5.00 | true
combos collection:
_id | name | itemId
-----------------------------------
x | Choose your pizza | e
y | Choose your drink | e
comboitems collection:
_id | itemId | comboId
-----------------------
1 | a | x
2 | b | x
3 | c | y
3 | d | y
Use select as in your ng-options directive:
<select ng-model="selectesdItem"
ng-options="comboitem.itemId as getName(comboitem.itemId) for comboitem in (comboitems | filter: {comboId: combo._id})">
</select>
and bind the result to a function that will return the name:
$scope.getName = function(id) {
return $filter('filter')($scope.items, { _id: id }, true)[0].name;
}
http://plnkr.co/edit/efym7LpeSjIqkDKbdLR0?p=preview

Resources