Home > Blockchain >  TypeError: Cannot read properties of undefined (reading 'endsWith')
TypeError: Cannot read properties of undefined (reading 'endsWith')

Time:05-02

I am trying to print out all countries which end with 'land' from the countries array below:

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

newArr = [];

for (let i = 0; i <= countries.length; i  ){
  console.log(countries[i])
  if (countries[i].endsWith('land') === true){
    newArr.push(countries[i])
  }
  else{
    continue
  }
}

However, I am having a TypeError: Cannot read properties of undefined (reading 'endsWith)

CodePudding user response:

out of index try this

for (let i = 0; i <= countries.length - 1; i  ) {
    console.log(countries[i]);
    if (countries[i].endsWith("land") === true) {
        newArr.push(countries[i]);
    } else {
        continue;
    }
}

CodePudding user response:

Don't use = because index will be one less than the array length!

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

newArr = [];

for (let i = 0; i < countries.length; i  ){
  console.log(countries[i])
  if (countries[i].endsWith('land') === true){
    newArr.push(countries[i])
  }
  else{
    continue
  }
}

CodePudding user response:

Hope you have already got the answer from comments and other answers, but here is few improvement you can do using array reduce and &&

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

const z = countries.reduce((acc, curr) => {
  curr.endsWith('land') && acc.push(curr);
  return acc;
}, []);
console.log(z)

CodePudding user response:

You can make a few changes to improve your loop

countries = ["finland", "ireland", "australia", "italy"];
newArr = [];
for (let i = 0; i < countries.length; i  ) {
    // < countries.length instead of <= countries.length - 1
    if (countries[i].endsWith("land")) { // Don't need === true
        newArr.push(countries[i]);
    } // don't need "else continue"
}

console.log(newArr);

And you can also use the inbuilt Array.prototype.filter method to do the same thing.

const countries = [
  'Albania',
  'Bolivia',
  'Canada',
  'Denmark',
  'Ethiopia',
  'Finland',
  'Germany',
  'Hungary',
  'Ireland',
  'Japan',
  'Kenya'
]

const newArray = countries.filter(country => country.endsWith('land'));

console.log(newArray);

CodePudding user response:

it is because in the for loop you took i<=countries.length it should be i<countries.length.

const countries = [
'Albania',
'Bolivia',
'Canada',
'Denmark',
'Ethiopia',
'Finland',
'Germany',
'Hungary',
'Ireland',
'Japan',
'Kenya'
]
newArr = [];
for (let i = 0; i <countries.length; i  ){
   if(countries[i].endsWith('land'))
   newArr.push(countries[i]);
}   
for(let i=0; i<newArr.length; i  ){
   console.log(newArr[i]);
}
  • Related