Cannot increment the value associative array - javascript-objects

I've tried to increment the value of an associative array key. In the `"else if" at the end of the code. There's no error but it doesn't seem to work when I execute the code.
addItem(index) {
const menu = [...this.props.menu];
const panier = [...this.state.panierClient]
let item = panier.findIndex((item)=> {
return item.article == menu[index].pizza;
})
if (item = -1){
panier.push({ article: menu[index].pizza, prix: menu[index].price, volume: 1 });
}else if (item != -1){
panier[item].volume++
}
console.log("yeah")
console.log(this.state.panierClient)
this.setState({
panierClient: panier
})
}
``

you missed the equal operator
if (item == -1){ // here should be like this

It should be === insead of =
if (item === -1){
panier.push({ article: menu[index].pizza, prix: menu[index].price, volume: 1 });

Related

How to get array after convert by reduce()

How to get an array after converted by reduce()?
Before convert:
After convert:
newProductsDetail = newProductsDetail.reduce((acc, curr) => {
const { categoryName } = curr
if (!acc[categoryName]) {
acc[categoryName] = []
}
acc[categoryName].push(curr)
return acc
}, [])
I would like get newProductsDetail[categoryName]
Have any way to approach this?
Thanks to all of you!
you can filter it like this
productDetail.filter( detail => { if(detail !== undefined && detail[0] !== undefined) return detail[0].categoryName === myCategory; return false; } )
Set initial value as object and not list as follows if you want to get hold of value from newProductsDetail[categoryName]:
newProductsDetail = newProductsDetail.reduce((acc, curr) => {
const { categoryName } = curr
if (!acc[categoryName]) {
acc[categoryName] = []
}
acc[categoryName].push(curr)
return acc
}, {}) // dictionary/object is initial and not list
If you only want array and some manipulation, try to use map to iterate and convert final output to array of something.

equivalent of following code in angular 2

I'm having trouble updating this code from angularjs ad angular 2, thank you for any help
if (settings != null && settings.checkSubscriptionForVip === true) {
return this.user.hasVipAccess().then(function(hasVipAccess) {
hasVipAccess ? deferred.resolve() : deferred.reject("NO_VIP");
return;
});
} else {
deferred.resolve();
return;
}
You neeed to have a boolean variable defined in your component and assign the result in to that,
hasVipAccess : boolean = false;
if (settings != null && settings.checkSubscriptionForVip === true) {
this.user.hasVipAccess().then(function(access) {
this.hasVipAccess = access;
});
} else {
this.hasVipAccess = access;
}
However if you do not need to set boolean variable, just return the result

AngularJS multiselect custom filter not working as intended

In an AngularJS record display (with filters) I have a multiselect array of territories that a user can select from to find out if a certain item is available in a certain territory.
The array returns a list of values such as
['001','010','200']
based on the ID of the territories selected. This is then checked against a JSON list of records which has a JSON value looks like this
territoriesnotavailable: "001, 085, 090"
Each record either has this set to null, or has a list from one to many numbers.
I currently use the following code (customFilter) which works perfectly if you only select ONE value.. it basically makes the item filter out if the territory selected in the multiselect is in the list of territoriesnotavailable
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (!(stringB.indexOf( stringA ) > -1)) {
return data;
}
}
});
return [];
};
}
So if I choose only one filter (resulting in a query of ['010'] for example. and this appears in territoriesnoavailable for the record.. it vanishes as expected.. but if I choose any value that is NOT in territoriesnotavailable the item appears again.. i need the record to vanish if ANY selected territory appears in the list regardless of any that do not
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (!(stringB.indexOf( stringA ) > -1)) {
return false;
}
}
return true;
});
return [];
};
}
This is the code that I settled on. It basically sets a flag as 1 (show) and then checks each number in the filter.. if there is just one occurance of the number in the list, the flag is set to zero. At the end of the check data is shown if the flaG REMAins as 1
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
var flag = 1;
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (stringB.indexOf( stringA ) > -1) {
flag = 0;
}
}
if(flag === 1) {
return data;
}
});
return [];
};
}

How can i prevent duplicate Record in Array

I need to prevent inserting duplicate Rec in $scope.EmployeeList Array For that i wrote if ($scope.EmployeeList.indexOf(EmpDetails) == -1) but its not filtering my rec
$scope.EmployeeList = [];
var amtArray = [];
$scope.G_Total = "Total Amount is";
$scope.SaveDb = function (Isvalid) {
var EmpDetails = {
'EmpName': $scope.EmpName,
'Email': $scope.Email,
'Cost': $scope.cost
}
if ($scope.EmployeeList.indexOf(EmpDetails) == -1) {
$scope.EmployeeList.push(EmpDetails);
console.log($scope.EmployeeList);
}
else
alert('Duplicate Value....');
Don't use indexOf (it checks for strict equality), try findIndex:
if ($scope.EmployeeList.findIndex(function(e) { return e.EmpName === EmpDetails.EmpName; }) === -1) {
$scope.EmployeeList.push(EmpDetails);
console.log($scope.EmployeeList);
}
You can also use Array.prototype.some to find duplicate value.The some() method tests whether at least one element in the array passes the test implemented by the provided function.
var empDetails = {name: 'abc', email: 'a#gmail.com'};
let duplicate = $scope.EmployeeList.some(function (emp) {
return emp.name === empDetails.name && emp.email === empDetails.email;
});
if (!duplicate) {
$scope.EmployeeList.push(EmpDetails);
}
else
alert('Duplicate Value....');

Ag-Grid Filter Comma

im using ag-Grid, but there is a issue when it filters my data, when i filter my data in the price column, it only works with numbers dot and not with commas.
Link: https://plnkr.co/edit/LDdrRbANSalvb4Iwh5mp?p=preview
Practical Example:
In the Price column select box equal and above insert "1.5" and than try inserting "1,5"
This is because this filter is a native one.
If you want to handle custom behaviour, define your own filter.
Documentation : https://www.ag-grid.com/angular-grid-filtering/index.php
A quick and dirty solution would be to monkey patch the NumberFilter like this :
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
valueAsNumber = value;
}
else {
valueAsNumber = parseFloat(value.replace(',','.'));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Then changed line is here :
valueAsNumber = parseFloat(value.replace(',','.'));
So i found the problem, first i had to convert the value has a string than i needed to replace the dot by the comma, the problem with the answer above was first because of the data type and than the order of the properties of the replace function, but the problem now is that is not filtering correctly, if i search using equal option if gives me 2 values, instead a fixed one, code looks something like this:
Code:
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
value = value.toString()
valueAsNumber = parseFloat(value.replace('.',','));
}
else {
valueAsNumber = parseFloat(value.replace('.',','));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Possible Solution:
NumberFilter.prototype.onFilterChanged = function () {
var filterText = utils_1.default.makeNull(this.eFilterTextField.value);
if (filterText && filterText.trim() === '') {
filterText = null;
}
var newFilter;
if (filterText !== null && filterText !== undefined) {
console.log(filterText);
// replace comma by dot
newFilter = parseFloat(filterText.replace(/,/g, '.'));
console.log(newFilter);
}
else {
newFilter = null;
}
if (this.filterNumber !== newFilter) {
this.filterNumber = newFilter;
this.filterChanged();
}
};

Resources