I am working on an array of array, and I want to test each first value but all what I get is Uncaught TypeError: Cannot read property '0' of undefined.
Help please!
function getManualDesactivation(data){
var tab=[];
var l=data.length ;
var listeService= getCategorie(data);
var resultat=[];
for(var i=0; i<l;i++){
if (data[i][1] == "DESACTIVATION") {
var subtab=[];
subtab.push(data[i][0]);
subtab.push(data[i][2]);
tab.push(subtab);
}
}
if (tab.length > 1) {
var j = 0;
for (var i = 0; i < listeService.length; i++) {
if (listeService[i] == tab[j][0]) {<---- here is the exception
resultat.push(tab[j][1]);
j++;
} else {
resultat.push(0);
j++;
}
}
}
That error is caused by trying to access the 0 index of variable that does not exist.
To ensure that you are checking the index of a defined value, you can write:
if (tab[j] && listeService[i] == tab[j][0]) {
resultat.push(tab[j][1]);
j++;
} else {
resultat.push(0);
j++;
}
Related
function piliang() {
var ids = "";
var num = document.getElementsByName("check");
for (var i = 0; 1 < num.length; i++) {
if (num.item(i).checked) {
ids = ids + num.item(i).value + ",";
}
alert(ids);
}
run successfully
function piliang() {
var ids = "";
var num = document.getElementsByName("check");
for (var i = 0; 1 < num.length; i++) {
if (num.item(i).checked) {
ids = ids + num.item(i).value + ",";
}
}
This gives out the following error:
"Cannot read property 'checked' of null"
The first code also has this error, but it can run successfully
My English is not good, please try to explain it in code, thank you very much
Fixed the for loop
function piliang() {
var ids = "";
var num = document.getElementsByName("check");
for (var i = 0; 1 < num.length; i++) { //use i instead of 1
if (num.item(i).checked) {
ids = ids + num.item(i).value + ",";
}
}
Why isn't this working?
var i = 0;
for (i < 1) {
if ($(".button[name=commit]").val() == "remove"){
i = 1;
}
}
I get this error message saying: unexpcted token ) at line 2.
Here you go with a solution using while loop
var i = 0;
while (i < 1) {
if ($(".button[name=commit]").val() == "remove"){
i = 1;
}
}
Here you go with a solution using for loop
for (var i=0; i<1;) {
if ($(".button[name=commit]").val() == "remove"){
i = 1;
}
}
Hope this will help you.
while(!$(".button[name=commit]").val() == "remove");
it was not working since for() needs 3 commands: Initialization, guard and last action: for(init;guard;action)
I have a loop with inner if statements as follows
var html = "";
var i;
for (i = 0; i < products.length; i++)
{
if(products[i].attrs.product_type == type)
{
html += '<p>hello world</p>';
}
}
I'd really like to be able to say if no results are returned from for loop, say "Sorry, no results were found" etc… I've tried the following…
for (i = 0; i < products.length; i++)
{
if(products[i].attrs.product_type == type)
{
html += '<p>hello world</p>' +i;
}
}
But that just puts the object number next to the returned result…
Any help would be great as I'm sure this is very easy
Thanks
At the end check whether the html variable is actually filled, if not we didn't find any items and we can use the sorry message:
var html = '';
var i;
for (i = 0; i < products.length; i++)
{
if(products[i].attrs.product_type === type)
{
html += '<p>hello world</p>';
}
}
if (html === '') { // or: "if (!html)" if you like that sort of thing
html = 'Sorry, no results were found'
}
Also notice that I changed the comparison from == to ===. That's because == tries to convert the type. While === does not. Use === to prevent strange errors, usually that's the one you want. For more info on it: Which equals operator (== vs ===) should be used in JavaScript comparisons?
Updated because of comment by #ASDFGerte
Similar to shotor's answer, but a slightly different approach would be as follows:
var html = "";
var i;
var found = false;
for (i = 0; i < products.length; i++)
{
if(products[i].attrs.product_type === type)
{
html += '<p>hello world</p>' +i;
found = true;
}
}
if (found === false)
{
//do stuff here.
}
var html = "";
var i;
var hasResult = false;
for ( i = 0; i < products.length; i++ )
{
if( products[i].attrs.product_type == type )
{
html += '<p>hello world</p>';
hasResult = true;
}
}
if( !hasResult ){
html = "No match";
}
Here is the code I am trying to remove the redundant code and move the code to separate function.
//Adding Infotypes to filter and checks whether any infotype option is selected
if(this.$infoOptions.val() != null){
var infotypelength = this.$infoOptions.val().length;
var l=0,j;
//Condition to check empty input type
if( infotypelength > 0){
var infotypeList = this.$infoOptions.val();
for(j = 0; j < infotypelength; j++) {
//It checks whether already option is selected and prevents adding to filter if its duplicate.
if(($.inArray( $('#infoOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#infoOptions').select2('data')[j].text);
if(infotypeList[j].contains('_subgroup')){
var res = infotypeList[j].split("_");
this.aSubinfotype[l]=res[0];
l=l+1;
}
else
this.aInfotypes.push(infotypeList[j]);
}
}
}
}
//Adding Countries to filter
if(this.$countryOptions.val() != null){
var geoLength = this.$countryOptions.val().length;
//Condition to check empty input type
if( geoLength > 0){
var geoList = this.$countryOptions.val();
var l=0;
for(var j = 0; j < geoLength; j++) {
if(($.inArray( $('#countryOptions').select2('data')[j].text, filterList)) == -1 ){
this.filter.push($('#countryOptions').select2('data')[j].text);
if(geoList[j].contains('_subgroup')){
var res = geoList[j].split("_");
this.aSubgeotype[l]=res[0];
l=l+1;
}
else
this.aGeography.push(geoList[j]);
}
}
}
}
But I am facing problem in passing the variable and cached selectors in to other function. Can anyone help me with this?
I don't know how is done your implementation but I really think that you can improve it, by the way, you can reduce your code in two way bit different :
var myFunction = function(option, filter, array, selector, subType) {
if(option && option.val()){
var optList = option.val();
var optLength = optList.length;
//Condition to check empty input type
if( optLength > 0) {
var l = 0;
for(var j = 0; j < optLength; j++) {
if( ($.inArray( selector.select2('data')[j].text, filterList)) == -1 ){
filter.push(selector.select2('data')[j].text);
if(optList[j].contains('_subgroup')){
var res = optList[j].split("_");
subType[l]=res[0];
l=l+1;
} else {
array.push(optList[j]);
}
}
}
}
}
}
call : myFunction(this.$countryOptions, this.filter, this.aGeography, $('#countryOptions'), this.aSubgeotype)
// data = {option, filter, array, selector, subType}
var myFunction = function(data) {
if(data.option && data.option.val()){
var optList = data.option.val();
var optLength = optList.length;
//Condition to check empty input type
if( optLength > 0) {
var l = 0;
for(var j = 0; j < optLength; j++) {
if( ($.inArray( data.selector.select2('data')[j].text, filterList)) == -1 ){
data.filter.push(data.selector.select2('data')[j].text);
if(optList[j].contains('_subgroup')){
var res = optList[j].split("_");
data.subType[l]=res[0];
l=l+1;
} else {
data.array.push(optList[j]);
}
}
}
}
}
}
call :
myFunction({
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
});
or
var data = {
option: this.$countryOptions,
filter: this.filter,
array: this.aGeography,
selector: $('#countryOptions'),
subType: this.aSubgeotype
}
myFunction(data);
The first way is to pass your data one by one, the second you pass your data into an json object.
I am having troubles trying to check if the date exists in the array.
for(var i = 0; i< crisislist.length; i++){
hazecounter = 1;
if(crisislist[i].category == 1){
if(crisislist[i].date != crisislist[i+1].date) {
hazelabel.push(crisislist[i].date);
}else{
hazecounter++;
}
hazedata.push(hazecounter);
}
}
The sample data for the date is:
["01-02-2017", "22-03-2017", "22-03-2017", "07-08-2017"]
And the expected output for hazelabel, hazedata should be:
hazelabel: ["01-02-2017", "22-03-2017", "07-08-2017"]
hazedata: [1,2,1]
With the code above, when I check until the last element in the array and trying to make a comparison, it throw me an error message:
Uncaught TypeError: Cannot read property 'date' of undefined
I think this is because when I reach the last element of array, and I try to find crisislist[I+1].date, it could not found and thus the error message.
Any idea how to fix this? Thanks in advance!
You must access crisislist[i+1].date only when i doesn't point to the last element.
Also notice that to get the desired result, you need to move the hazedata.push inside the if block and put the initialisation of hazecounter in front of the loop.
var hazecounter = 1;
for (var i = 0; i< crisislist.length; i++) {
if (crisislist[i].category == 1) {
if (i == crisislist.length-1 || crisislist[i].date != crisislist[i+1].date) {
hazelabel.push(crisislist[i].date);
hazedata.push(hazecounter);
hazeCounter = 1;
} else {
hazecounter++;
}
}
}
Your if statement is going to be a problem.
if(crisislist[i].date != crisislist[i+1].date) {
You are accessing crisislist[i+1] in a loop that goes to < crisislist.length. That means that if you have an array of size 4, your loop will go until i = 3, but you are accessing i+1 from the array (crisislist[4]), which will be undefined.
Try changing your for loop to go to crisis.length-1
You just need to check till second last :
for(var i = 0; i< (crisislist.length-1); i++){
hazecounter = 1;
if(crisislist[i].category == 1){
if(crisislist[i].date != crisislist[i+1].date) {
hazelabel.push(crisislist[i].date);
if (crisislist.length-2 == i)
{
hazelabel.push(crisislist[i+1].date);
}
}else{
hazecounter++;
}
hazedata.push(hazecounter);
}
}
Check that code. If you have any questions, add a comment :) In my solution dates dont have to be sorted.
</head>
<BODY>
<script>
function Something(date)
{
this.date = date;
this.category = 1;
}
var crisislist = [];
var hazelabel = [];
var hazedata = [];
crisislist[0] = new Something("01-02-2017");
crisislist[1] = new Something("22-03-2017");
crisislist[2] = new Something("22-03-2017");
crisislist[3] = new Something("07-08-2017");
for(var i = 0; i< crisislist.length; i++){
if(crisislist[i].category == 1)
{
if(!hazelabel[crisislist[i].date])
{
hazelabel[crisislist[i].date] = crisislist[i].date;
hazedata[crisislist[i].date] = 1;
}
else
{
hazedata[crisislist[i].date]++;
}
}
}
for(var key in hazelabel)
{
console.log(hazelabel[key]);
console.log(hazedata[key]);
}
</script>
</BODY>
</HTML>