Complex .reduce to filter an array - reduce

so I'm trying to write a fancy reduce function (i could loop, but ...). Would love help on this -
The initial items array:
const items = [
{'labels': ['a'], 'team': "infra"},
{'labels': [], 'team': "infra"},
{'labels': [], 'team': "InfraNew"},
{'labels': ['new'], 'team': "infrastructure org"},
{'labels': ['aaa'],'team': "infra"},
{'labels': [], 'team': "Infra 1"},
{'labels': ['b'],'team': "infra"},
{'labels': ['a'],'team': "DT"},
{'labels': ['c'], 'team': "DT"},
{'labels': ['c', 'b'], 'team': null}
]
and the filters object.
const filters = {
team: ['infra', 'DT'],
labels: ['a', 'b']
}
The goal is to get results array of items, with labels within the filtered teams. (So if teams don't have matching labels, they should be excluded).
const results = [
{'labels': ['a'],'team': "infra"},
{'labels': ['b'],'team': "infra"},
{'labels': ['a'],'team': "DT"},
]
This is what I have, but can't seem to get the conditioning correctly.
function filtering(res, el) {
for (let i = 0; i < Object.keys(filters).length; i++) {
const key = Object.keys(filters)[i];
let filterArray = filters[key];
const elementValue = el[key];
if (!elementValue) return res;
if (key === 'labels') {
if (elementValue.length === 0) return res;
elementValue.map(elementValueItem => {
if (filterArray.indexOf(elementValueItem) < 0) {
return res
}
})
} else if (filterArray.indexOf(elementValue) < 0) {
return res;
}
}
res.push(el);
return res;
}
var results = items.reduce(filtering, [])
console.log(results)
Here is the jsfiddle

It would greatly simplify your conditions if you thought about this problem in terms of Array.prototype.filter. I've done that for you below
const items = [
{'labels': ['a'], 'team': "infra"},
{'labels': [], 'team': "infra"},
{'labels': [], 'team': "InfraNew"},
{'labels': ['new'], 'team': "infrastructure org"},
{'labels': ['aaa'],'team': "infra"},
{'labels': [], 'team': "Infra 1"},
{'labels': ['b'],'team': "infra"},
{'labels': ['a'],'team': "DT"},
{'labels': ['c'], 'team': "DT"},
{'labels': ['c', 'b'], 'team': null},
]
const filters = {
team: ['infra', 'DT'],
labels: ['a', 'b'],
}
const shouldKeep = item => {
if (!filters.team.includes(item.team)) {
return false
}
if (item.labels.length === 0) {
return false
}
for (const label of item.labels) {
if (!filters.labels.includes(label)) {
return false
}
}
return true
}
console.log(
items.filter(shouldKeep)
)

Related

Adding values to 2D array with forEach

I am trying to add values to a 2D array in Google Apps Script. The following code runs, but is not the desired output for the array.
function fruitList() {
var fruitArray = [['apple'], ['banana'], ['cherry']];
var newArray = [];
fruitArray.forEach(function(value) {
newArray.push([value, "name"]);
});
}
My code yields the following output:
[ [ [ 'apple' ], 'name' ], [ [ 'banana' ], 'name' ], [ [ 'cherry' ], 'name' ] ]
My desired output is:
[ [ 'apple', 'name' ], [ 'banana', 'name' ], [ 'cherry', 'name' ] ]
value is a array. Not a string/primitive value. You can use destructuring assignment to get the actual value:
fruitArray.forEach(function([/*destructure 1D array*/value]) {
newArray.push([value, "name"]);
});
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
function fruitList() {
var fruitArray = [['apple'], ['banana'], ['cherry']];
var newArray = [];
fruitArray.forEach(function([value]) {
newArray.push([value, "name"]);
});
console.info(newArray)
}
fruitList()
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Add a value:
function fruitList() {
var fruitArray = [['apple'], ['banana'], ['cherry']];
var newArray = [];
fruitArray.forEach(function(value) {
newArray.push([value[0], "name"]);
});
Logger.log(JSON.stringify(newArray))
}
Execution log
3:27:33 PM Notice Execution started
3:27:34 PM Info [["apple","name"],["banana","name"],["cherry","name"]]
3:27:34 PM Notice Execution completed

Jest coverage in redux reducer - object destruction not covered

I have the following issue with Jest:
I have this reducer:
[REMOVE_FILTER]: (state: FiltersState, action: Action<string>): FiltersState => {
const { [action.payload!]: deleted, ...activeFilters } = state.activeFilters;
return { ...state, activeFilters, createFilterSelection: undefined, filterCreateOpen: false };
}
When I am trying to test it, it says that I do not have coverage for
...activeFilters } = state.activeFilters;
Here is my test:
test(REMOVE_FILTER, () => {
const action: IAction<string> = {
type: REMOVE_FILTER,
payload: "subprovider"
};
expect(
testReducer({ reducer, state, action })
).toEqual({
...state,
activeFilters: { name: null, branded: null },
createFilterSelection: undefined,
filterCreateOpen: false
});
});
Can someone suggest what I am doing wrong?
I am using:
Jest 23.6.0
Typescript 3.4.0
Redux 4.0.0
React-Redux: 6.0.0
Redux Actions: 2.6.1
Thank you!
P.S: Here is the Jest config:
{
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"globals": {
"window": true,
"document": true
},
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__test__/.*)\\.test\\.(ts|tsx)$",
"notify": true,
"collectCoverageFrom": [
"**/*.{ts,tsx}"
],
"coveragePathIgnorePatterns": [
"(/__e2e__/.*)",
"(/__specs__/.*)",
"(/__test__/.*)",
"(/interfaces/.*)",
"(index.ts)",
"(src/server/app.ts)",
"(src/server/config.ts)",
"(/mock/.*)",
"(data/mock.ts)",
"(automapperConfiguration.ts)",
"(src/app/store/store.ts)",
"(src/app/containers/brand-configuration/.*)"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"setupTestFrameworkScriptFile": "<rootDir>/jestSetup.js",
"testURL": "http://localhost/"
}
The above TS code gets transpilled to:
[REMOVE_FILTER]: (state, action) => {
const _a = state.activeFilters, _b = action.payload, deleted = _a[_b], activeFilters = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
return Object.assign({}, state, { activeFilters, createFilterSelection: undefined, filterCreateOpen: false });
}

Iterate through JSON Object to get all objects with property

I am trying to iterate through the following JSON document in order to get the names of skating rinks:
I can get one name; however, what I am trying to do is loop through all of the entries (there are 253) and return a list of all the names.
Here is my React component:
class Patinoire extends Component {
constructor(props) {
super(props);
this.state = { patinoires: [] };
}
componentDidMount() {
var url = 'http://localhost:3000/patinoires'
fetch(url).then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return response.json();
})
.then(data => this.setState ({ patinoires: data.patinoires }));
}
render() {
var patinoires = this.state.patinoires;
var pjs2 = Object.values(patinoires);
var pjs3 = pjs2.map(x => x["2"].nom);
return <div>{pjs3}</div>
}
}
Right now, when using {pjs3}, I get the name of 3rd skating rink of the JSON document. How can I loop through all the entries and return the name property of all the entries?
EDIT: here is a sample of the data
{
"patinoires": {
"patinoire": [
{
"nom": [
"Aire de patinage libre, De la Savane (PPL)"
],
"arrondissement": [
{
"nom_arr": [
"Côte-des-Neiges - Notre-Dame-de-Grâce"
],
"cle": [
"cdn"
],
"date_maj": [
"2018-01-12 09:08:25"
]
}
],
"ouvert": [
""
],
"deblaye": [
""
],
"arrose": [
""
],
"resurface": [
""
],
"condition": [
"Mauvaise"
]
},
{
"nom": [
"Aire de patinage libre, Georges-Saint-Pierre (PPL)"
],
"arrondissement": [
{
"nom_arr": [
"Côte-des-Neiges - Notre-Dame-de-Grâce"
],
"cle": [
"cdn"
],
"date_maj": [
"2018-01-12 09:08:25"
]
}
],
"ouvert": [
""
],
"deblaye": [
""
],
"arrose": [
""
],
"resurface": [
""
],
"condition": [
"Mauvaise"
]
}
]
}
}
You can use Array.prototype.reduce() to flatten the result data with combination of Array.prototype.map() or Array.prototype.forEach().
Here is a running example:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
patinoires: {
patinoire: [
{
nom: ["Aire de patinage libre, De la Savane (PPL)"],
arrondissement: [
{
nom_arr: ["Côte-des-Neiges - Notre-Dame-de-Grâce"],
cle: ["cdn"],
date_maj: ["2018-01-12 09:08:25"]
}
],
ouvert: [""],
deblaye: [""],
arrose: [""],
resurface: [""],
condition: ["Mauvaise"]
},
{
nom: ["Aire de patinage libre, Georges-Saint-Pierre (PPL)"],
arrondissement: [
{
nom_arr: ["Côte-des-Neiges - Notre-Dame-de-Grâce"],
cle: ["cdn"],
date_maj: ["2018-01-12 09:08:25"]
}
],
ouvert: [""],
deblaye: [""],
arrose: [""],
resurface: [""],
condition: ["Mauvaise"]
}
]
}
};
}
renderData = () => {
const { patinoires } = this.state;
const markup = patinoires.patinoire.reduce((result, current) => {
current.nom.map(n => {
return result.push(<div>{n}</div>);
});
return result;
}, []);
return markup;
}
render() {
return <div>{this.renderData()}</div>;
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Try this:
var pjs2 = Object.values(patinoires);
var names = pjs2.reduce((accumulator, elem) => {
if (elem.patinoire) {
elem.patinoire.forEach(part => {
if(part.nom) {
part.nom.forEach((name) => accumulator.push(name));
}
});
}
return accumulator;
}, []);

ReactJS filter complex Json

[enter image description here][1]Please help me as I am struggling to get something to work. I have a JSON that represents a tree view in my app.
{
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
What I want to do is traverse through my JSON and only return the entries which have the chosen property as true.
I tried many things without success so far, would you guys be able to help me?
https://i.stack.imgur.com/r61MO.jpg
onPush(e){
var chosennodes = filters.filterTreeChosen(this.state.data);
this.setState({selectednodes: chosennodes});
}
Then the filter itself:
[Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter( key => predicate(obj\[key\]) )
.reduce( (res, key) => (res\[key\] = obj\[key\], res), {} );
export const isChosen = (chosen) =>{
return chosen == true;
}
export const filterTreeChosen = (nodes) => {
var filtered = Object.filter(nodes, node => node.chosen == true);
console.log(filtered);
};][1]
https://i.stack.imgur.com/IAXZ7.jpg
Check this out. I think it should apply to your task.
const tree = {
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
function getChosenNodes (nodes) {
let result = [];
nodes.forEach(node => {
if (node.chosen) {
result = result.concat([node.nodeid]);
}
if (node.children) {
result = result.concat(getChosenNodes(node.children));
}
})
return result;
}
console.log(getChosenNodes([tree]))
I've returned only nodeid but you can change it as you need.

$scope issue with gridOptions, angular-ui-grid and REST call from service

I seem to be having an issue getting my ng-grid directive to populate from a returned REST api json obj.
I have verfied that a valid json obj is returned and i have retrieved a nested obj of the data I need. It seems that it is not making it into the gridOptions function. Where myData is the correct valid json.
Any help will be greatly appreciated. I am pulling my hair out at this point.
Here is my service:
grid-service.js
'use strict';
app.factory('GridService', ['$http', '$q', function($http, $q) {
var apiUrl = "http://xx.xx.xx.xx/coName/public/index.php/";
// configure the send request
function sendRequest(config){
var deferred = $q.defer();
config.then(function(response){
deferred.resolve(response);
}, function(error){
deferred.reject(error);
});
return deferred.promise;
}
// retrieve all
function getRoles() {
var request = $http({
method: 'GET',
url: apiUrl + 'roles'
});
return sendRequest(request);
}
return {
getRoles: getRoles
};
}]);
I inject it into my ctrl here, and my init function and gridOption functions:
app.controller('ModuleCtrl', [ '$scope', '$http', '$modal', '$filter', 'GridService', function($scope, $http, $modal, $filter, gridService) {
var initializeGrid = function(){
getRoles();
};
var getRoles = function(){
gridService.getRoles().then(function(myRoles){
var myRolesData = myRoles.data._embedded.roles;
$scope.myData = myRoles.data._embedded.roles;
console.log($scope.myData);
});
};
$scope.gridOptions = {
data: 'myData',
enableRowSelection: true,
enableCellEditOnFocus: true,
showSelectionCheckbox: true,
selectedItems: $scope.selectedRows,
columnDefs: [{
field: 'ID',
displayName: 'Id',
enableCellEdit: false
}, {
field: 'APP_ID',
displayName: 'Module ID',
enableCellEdit: false
}, {
field: 'RLDESC',
displayName: 'Role Description',
enableCellEdit: true
}, {
field: 'APDESC',
displayName: 'Module Description',
enableCellEdit: true
}, {
field: 'ZEND_DB_ROWNUM',
displayName: 'Record number',
enableCellEdit: false
}]
};
// fire it up
initializeGrid();
}
My complete json:
{
"_links": {
"self": {
"href": "http://xx.xx.xx.xx/coName/public/index.php/roles?page=1"
},
"describedBy": {
"href": "Some Fun Stuff"
},
"first": {
"href": "http://xx.xx.xx.xx/coName/public/index.php/roles"
},
"last": {
"href": "http://xx.xx.xx.xx/coName/public/index.php/roles?page=1"
}
},
"_embedded": {
"roles": [
{
"ID": 1,
"APP_ID": 1,
"RLDESC": "Admin",
"APDESC": "authLive",
"ZEND_DB_ROWNUM": "1"
},
{
"ID": 2,
"APP_ID": 1,
"RLDESC": "User",
"APDESC": "authLive",
"ZEND_DB_ROWNUM": "2"
},
{
"ID": 4,
"APP_ID": 1,
"RLDESC": "SuperUser",
"APDESC": "authLive",
"ZEND_DB_ROWNUM": "3"
}
]
},
"page_count": 1,
"page_size": 25,
"total_items": 3
}
Remove the following line from the gridOptions
data: 'myData'
Then in getRoles() use
$scope.gridOptions.data = myRolesData;
instead of
$scope.myData = myRoles.data._embedded.roles;
(Maybe you need $scope.myData for some other reason than the grid, but if not I think the above is all you need. I have not tested this live, but it should work.)