so like imagine I have an array like this
names = ["Alex", "David", "Hanna"]
so I want to get the length of the characters like Alex is 4 characters long and If I get an odd length I want to show that as an output. I can't figure out how to do it.
CodePudding user response:
You can use Array.filter
method to filter the results
const names = ["Alex", "David", "Hanna"]
names.filter(name => name.length % 2 !== 0) // ['David', 'Hanna']
CodePudding user response:
Welcome Santonu to SO, you can do the following:
const names = ["Alex", "David", "Hanna"];
for (const name of names) {
console.log(name.length);
}
This will iterate every name of the array and log you the length.
CodePudding user response:
const array = ["Alex", "David", "Hanna"]
array.forEach(item => {if(item.length%2 == 1) console.log(item)});
See this for loop in javascript.