I am having trouble figuring out a way to loop the object in the objectMap to give me the Highest value of the fruits it gathered.My question is, how do I get loop to get the highest value using a for in loop. I have attached my code below.And where i have stopped,by the for in loop
var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
var fruitList = fruitString.split(',')
var fruitMap = {};
function soldfruits(){
for (var i = 0; i < fruitList.length; i++) {
var currentFruit = fruitList[i]
if (fruitMap[currentFruit] === undefined) {// cehck if fruit is not available
fruitMap[currentFruit]= 0;
}
fruitMap[currentFruit] = fruitMap[currentFruit] +1
}
console.log(fruitMap);
}
soldfruits(fruitMap); // calling function for check.
for (var fruits in fruitMap) {
if ( ) {
}
}
Try this:
var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
var fruitList = fruitString.split(',')
var fruitMap = {};
function soldfruits(){
for (var i = 0; i < fruitList.length; i++) {
var currentFruit = fruitList[i]
if (fruitMap[currentFruit] === undefined) {
fruitMap[currentFruit]= 0;
}
fruitMap[currentFruit] = fruitMap[currentFruit] +1
}
console.log(fruitMap);
return fruitMap
}
var fmap = soldfruits(fruitMap);
var high = 0
var high_name = ''
for(var x in fmap)
{
if(fmap[x] > high)
{
high = fmap[x]
high_name = x
}
}
console.log(high + ' ' + high_name)
This code will return the highest number of fruit sold and also the names of those fruit.
var mostSold = 0;
var mostFruits = [];
for (var fruits in fruitMap) {
if (fruitMap[fruits] > mostSold) {
// store the number of fruit sold
mostSold = fruitMap[fruits];
// initialise the list with the fruit name
mostFruits = [fruits];
}
else if (fruitMap[fruits] == mostSold) {
// add the fruit name to the list
mostFruits.push(fruits);
}
}
console.log( mostSold + ' : ' + mostFruits );
Like this:
var highest = {fruit:"", num:0}
for (var fruit in fruitMap) {
var num = fruitMap[fruit];
if (num > highest.num) { // if the current number of fruits is greater than the saved
highest.fruit=fruit; // save the fruit
highest.num=num; // save the number
}
}
console.log(highest);
Alternatively sort the array using object sort and pop the last
var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana'
var fruitList = fruitString.split(',')
var fruitMap = {};
function soldfruits() {
for (var i = 0; i < fruitList.length; i++) {
var currentFruit = fruitList[i]
if (fruitMap[currentFruit] === undefined) {
fruitMap[currentFruit] = 0;
}
fruitMap[currentFruit] = fruitMap[currentFruit] + 1
}
console.log(fruitMap);
}
soldfruits(fruitMap);
var highest = {fruit:"", num:0}
for (var fruit in fruitMap) {
var num = fruitMap[fruit];
if (num > highest.num) {
highest.fruit=fruit;
highest.num=num;
}
}
console.log(highest);
You could loop with Array#forEach and check, if fruitList has a value of the propery fruit. If not take zero otherwise the value of the propery and add one. Then assign to the property.
var fruitString = 'Banana,Banana,Pear,Orange,Apple,Melon,Grape,Apple,Banana,Grape,Melon,Grape,Melon,Apple,Grape,Banana,Orange,Melon,Orange,Banana,Banana,Orange,Pear,Grape,Orange,Orange,Apple,Apple,Banana',
fruitList = fruitString.split(','),
fruitMap = {},
highest = 0;
fruitList.forEach(function (fruit) {
fruitMap[fruit] = (fruitMap[fruit] || 0) + 1;
if (fruitMap[fruit] > highest) {
highest = fruitMap[fruit];
}
});
console.log(fruitMap);
console.log(highest);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I am trying to push elements to an array in a nested loop, but only the last item is getting repeated in the final array, where am I going wrong, very new to javascript asynchronous concept.Below is the function in which I push the items to an array.
$scope.showBeList = function(feed) {
if (feed[srcServ.KEY_CONTENT_TEXT]) {
var content = JSON.parse(feed[srcServ.KEY_CONTENT_TEXT])
if (content) {
$scope.beList = {};
for (var key in content) {
var decorationVal;
//reading value from a lokijs collection
var decoration = dataServ[srcServ.CONST_COLLECTION_DECORATION].find({
'name': key
});
if (decoration && decoration.length) {
decorationVal = decoration[0];
if (decorationVal != null) {
var tempObj = JSON.parse(decorationVal.value);
if (tempObj) {
var header = tempObj[key][key + '_HEADER'];
if (header) {
var counter = content[key].length;
var tempItems = [];
for (var j = 0; j < content[key].length; j++) {
(function(j) {
var obj = {};
obj[srcServ.KEY_MAIN_HEADER] = tempObj[key][srcServ.KEY_DESC];
obj[srcServ.KEY_SUB_HEADER] = header[srcServ.KEY_DESC];
obj.id = j;
var itemVal = content[key][j][key + '_HEADER'];
var details = [];
var notes = [];
for (var item in itemVal) {
var val = null;
var found = false;
for (var i = 0; i < header.field.length; i++) {
if (header.field[i].name == item) {
val = header.field[i];
found = true;
break;
}
}
if (found && val != null) {
val[srcServ.KEY_DESC_VALUE] = itemVal[item];
details.push(val);
}
}
obj.details = details;
counter--;
if (counter == 0) {
$scope.showMoreDetails = true;
$scope.beList.beItems = tempItems;
console.log(JSON.stringify($scope.beList));
}
tempItems.push(obj)
})(j);
// $scope.beList.beItems.push(obj);
}
}
}
}
}
}
}
}
}
I have a function which gets values from elements:
function getTransactionValues() {
var o = {};
o.reservations = [];
$('#custom-headers option:selected').each(function (i, selected) {
o.reservations[i] = $(selected).val();
});
o.amount = $('input[name=amount-price]').val();
o.currency_value = $('input[name=currency-value]').val();
o.currency_name = $('.currency_appendto option:selected').html();
o.actual_amount = $('input[name=actual-amount]').val();
o.actual_remaining = $('input[name=actual-remaining]').val();
o.funds_arrival_date = $('input[name=funds-arrival]').val();
o.paid_to = $('.paidto option:selected').html();
o.checkbox = $('.multi-transaction:checked').map(function () {
return this.value
}).get();
return o;
}
Now i want to check whether amount, actual_amount and funds_arrival_date are filled. if they are i will release the disabled class from a button. i've tried
var check_review = function () {
var a = getTransactionValues();
var options = [a.amount, a.actual_amount, a.funds_arrival_date];
for(i = 0; i < options.length; i++) {
if(options[i].length > 0) {
$('a[name=review_button]').removeClass('disabled');
}
else{
//this array is empty
alert('There is a problem!');
}
}
}
$('.test').click(function() {
check_review();
});
But it doesn't seems to be working..
Remove disabled attribute using JQuery?
Can you please look at above link, I think we should use $('.inputDisabled').prop("disabled", false);
Even if a single array element will be non empty then your code will remove the class disabled from the a. To make sure that all the elements of array are non empty and then only you want to remove the class then the way is:
for(i = 0; i < options.length; i++) {
if(options[i].length > 0) {
$('a[name=review_button]').removeClass('disabled');
}
else{
$('a[name=review_button]').addClass('disabled');
}
}
Or the other way is
var check = true;
for(i = 0; i < options.length; i++) {
if(options[i].length == 0) {
check = false;
}
}
if(check ) $('a[name=review_button]').removeClass('disabled');
Try using Array.prototype.every()
if (options.every(Boolean)) {
$("a[name=review_button]").removeClass("disabled");
} else {
// do other stuff
}
So, I'm trying to delete from an array if it's element value isn't equal to a value that I specified:
Code: http://pastebin.com/hUc3mVLv
$scope.enablePVR = function()
{
for (i = 0; i < $scope.new_epg.length; i++) {
start_time = convert_time($scope.new_epg[i].start);
$scope.new_epg[i].title = $scope.new_epg[i].title.replace(/<(?:.|\n)*?>/gm, '');
$scope.new_epg[i].description = "";
$scope.new_epg[i].time = start_time;
}
archiveEPG = [];
for(var i=0; i<archiveEPG.length; i++) {
var e = document.getElementById("dateSelect");
if($scope.new_epg[i].start.split(" ")[0] == e[e.selectedIndex].value) {
archiveEPG[archiveEPG.length+1] = $scope.new_epg[i];
}
}
document.getElementById("dateSelect").remove(0);
$scope.get_epg(null, true, archiveEPG);
}
.remove(0) isn't valid you can add this function to make it valid tho :
Array.prototype.remove = function(index) {
return this.splice(index, 1); // The second parameter is the number of elements to remove.
}
I have an objects array that has 27 length:
[object(array)]
....
....
Each item in this array is empty or consists of an objects of array which has properties as shown below. This could have several loops so it will have many objects.
function MainTests(testName, section, totalExpected, totalPasssed, totalDuration, loopExpected, loopPassed, loopDuration) {
var self = this;
self.sectionName = section;
self.testName = testName;
self.totalPassExpect = totalExpected;
self.totalPassRate = totalPasssed;
self.totalDuration = totalDuration;
self.loopPassExpected = loopExpected;
self.loopPassRate = loopPassed;
self.loopDuration = loopDuration;
}
I want to group by sectionName and testName like this:
{
section: sectionName,
tests: [testName:testName, totalPassExpect: totalExpected........loopDuration:loopDuration..]
}
I tried this but it only groups it by sectionName:
self.GroupMainTest = ko.computed(function () {
var mainTestArray = [];
for (var i = 0; i < allMainTests.length ; i++) {
var group = {};
var result = allMainTests[i];
if (result.length != 0) {
for (var j = 0; j < result.length; j++) {
if (group[result[j].sectionName] == undefined) {
group[result[j].sectionName] = [group[result[j].sectionName]];
}
group[result[j].sectionName].push(result[j].sectionName)
group[result[j].sectionName].push(result[j].totalPassExpect)
group[result[j].sectionName].push(result[j].totalPassRate)
group[result[j].sectionName].push(result[j].totalDuration)
group[result[j].sectionName].push(result[j].loopPassExpected)
group[result[j].sectionName].push(result[j].loopPassRate)
group[result[j].sectionName].push(result[j].loopDuration)
group[result[j].sectionName].push(result[j].testName)
}
mainTestArray.push(group);
}
else {
mainTestArray.push(result);
}
}
return mainTestArray;
});
My goal is to achieve similar to this plnkr.
Can you help?