admin管理员组文章数量:1023120
I need to write a removeVowelKeys function that takes an object and removes all keys that begin with a vowel. The register does not matter.
For example, I have an object
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
The result of a function's work should look like this:
vowelsObj === {
chip: 100,
}
This is what I've already written
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
if (key.startsWith('a'||'e'||'i'||'o'||'u'||'y')) {
delete object[key];
}
}
}
return object;
}
but it removes only the first field that starts with a vowel and the others stay. Any ideas how it can be done?
I need to write a removeVowelKeys function that takes an object and removes all keys that begin with a vowel. The register does not matter.
For example, I have an object
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
The result of a function's work should look like this:
vowelsObj === {
chip: 100,
}
This is what I've already written
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
if (key.startsWith('a'||'e'||'i'||'o'||'u'||'y')) {
delete object[key];
}
}
}
return object;
}
but it removes only the first field that starts with a vowel and the others stay. Any ideas how it can be done?
Share Improve this question asked Dec 7, 2020 at 21:13 Anna HarshynaAnna Harshyna 932 silver badges8 bronze badges 3-
2
key.startsWith('a'||'e'||'i'||'o'||'u'||'y'))
That doesn't do what you think it does,..'a'||'e'||'i'||'o'||'u'||'y' = 'a'
, IOW: your code does ->key.startsWith('a')
– Keith Commented Dec 7, 2020 at 21:25 -
Try this
['a','e','i','o','u','y'].includes(key.charAt(0).toLowerCase())
in your if statement. – ptothep Commented Dec 7, 2020 at 21:26 -
Programing language ain't understand
key.startsWith('a' || 'b' || 'c')
like human, you should translate it tokey.startsWith('a') || key.startsWith('b') || key.startsWith('c')
; Or just['a', 'b', 'c'].includes(key[0].toLowerCase())
; Or, if you prefer a more pact way,const removeVowelKeys = object => Object.fromEntries(Object.entries(object).filter(pair => !['a','e','i','o','u','y'].includes(pair[0][0].toLowerCase())))
, this line of function will destruct object as key-value pair, then just filter out key that is vowel, and return a new constructed object – zmou-d Commented Dec 7, 2020 at 23:21
3 Answers
Reset to default 2You could use the regex /^[aeiouy]/
. The ^
in the regex matches the start of the string [aeiouy]
matches one of a
, e
, i
, o
, u
, or y
. To match case in-sensitive (ignore character casing) add the i
flag. eg. /^[aeiouy]/i
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
for (let key in object) {
if (key.match(/^[aeiouy]/)) {
delete object[key];
}
}
return object;
}
removeVowelKeys(vowelsObj);
console.log(vowelsObj);
I've kept removeVowelKeys
similar to what you already had. However delete
mutates the argument. If you don't want this, copy the properties that you do want to a new object instead of deleting from the original.
const copy = {};
for (let key in object) {
if (!key.match(/^[aeiouy]/)) { // <- added ! to invert the if-statement
copy[key] = object[key];
}
}
return copy;
You are simply including an extra for loop outside looping through the keys which are not needed and using includes
will be helpful than repeating the startsWith
for each and every character.
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
const updatedResult = Object.keys(object).reduce((updatedResult, key) => {
const char = (typeof(key) === 'string' && key.trim().length > 0)? key.toLowerCase().charAt(0) : '';
if (!['a', 'e' , 'i' ,'o' , 'u' ,'y'].includes(char)){
updatedResult[key] = object[key];
}
return updatedResult;
}, {})
return updatedResult;
}
console.log(removeVowelKeys(vowelsObj));
You have to use individual .startsWith
calls for each character. Also you need to transform the key to lowercase to remove the uppercase keys.
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
const lowercaseKey = key.toLowerCase()
if (
lowercaseKey.startsWith('a') || lowercaseKey.startsWith('e')
|| lowercaseKey.startsWith('i') || lowercaseKey.startsWith('o')
|| lowercaseKey.startsWith('u') || lowercaseKey.startsWith('y')
) {
delete object[key];
}
}
}
return object;
}
console.log(
removeVowelKeys(vowelsObj)
)
I need to write a removeVowelKeys function that takes an object and removes all keys that begin with a vowel. The register does not matter.
For example, I have an object
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
The result of a function's work should look like this:
vowelsObj === {
chip: 100,
}
This is what I've already written
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
if (key.startsWith('a'||'e'||'i'||'o'||'u'||'y')) {
delete object[key];
}
}
}
return object;
}
but it removes only the first field that starts with a vowel and the others stay. Any ideas how it can be done?
I need to write a removeVowelKeys function that takes an object and removes all keys that begin with a vowel. The register does not matter.
For example, I have an object
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
The result of a function's work should look like this:
vowelsObj === {
chip: 100,
}
This is what I've already written
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
if (key.startsWith('a'||'e'||'i'||'o'||'u'||'y')) {
delete object[key];
}
}
}
return object;
}
but it removes only the first field that starts with a vowel and the others stay. Any ideas how it can be done?
Share Improve this question asked Dec 7, 2020 at 21:13 Anna HarshynaAnna Harshyna 932 silver badges8 bronze badges 3-
2
key.startsWith('a'||'e'||'i'||'o'||'u'||'y'))
That doesn't do what you think it does,..'a'||'e'||'i'||'o'||'u'||'y' = 'a'
, IOW: your code does ->key.startsWith('a')
– Keith Commented Dec 7, 2020 at 21:25 -
Try this
['a','e','i','o','u','y'].includes(key.charAt(0).toLowerCase())
in your if statement. – ptothep Commented Dec 7, 2020 at 21:26 -
Programing language ain't understand
key.startsWith('a' || 'b' || 'c')
like human, you should translate it tokey.startsWith('a') || key.startsWith('b') || key.startsWith('c')
; Or just['a', 'b', 'c'].includes(key[0].toLowerCase())
; Or, if you prefer a more pact way,const removeVowelKeys = object => Object.fromEntries(Object.entries(object).filter(pair => !['a','e','i','o','u','y'].includes(pair[0][0].toLowerCase())))
, this line of function will destruct object as key-value pair, then just filter out key that is vowel, and return a new constructed object – zmou-d Commented Dec 7, 2020 at 23:21
3 Answers
Reset to default 2You could use the regex /^[aeiouy]/
. The ^
in the regex matches the start of the string [aeiouy]
matches one of a
, e
, i
, o
, u
, or y
. To match case in-sensitive (ignore character casing) add the i
flag. eg. /^[aeiouy]/i
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
for (let key in object) {
if (key.match(/^[aeiouy]/)) {
delete object[key];
}
}
return object;
}
removeVowelKeys(vowelsObj);
console.log(vowelsObj);
I've kept removeVowelKeys
similar to what you already had. However delete
mutates the argument. If you don't want this, copy the properties that you do want to a new object instead of deleting from the original.
const copy = {};
for (let key in object) {
if (!key.match(/^[aeiouy]/)) { // <- added ! to invert the if-statement
copy[key] = object[key];
}
}
return copy;
You are simply including an extra for loop outside looping through the keys which are not needed and using includes
will be helpful than repeating the startsWith
for each and every character.
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
const updatedResult = Object.keys(object).reduce((updatedResult, key) => {
const char = (typeof(key) === 'string' && key.trim().length > 0)? key.toLowerCase().charAt(0) : '';
if (!['a', 'e' , 'i' ,'o' , 'u' ,'y'].includes(char)){
updatedResult[key] = object[key];
}
return updatedResult;
}, {})
return updatedResult;
}
console.log(removeVowelKeys(vowelsObj));
You have to use individual .startsWith
calls for each character. Also you need to transform the key to lowercase to remove the uppercase keys.
const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}
function removeVowelKeys(object) {
const num = Object.keys(object);
for( let i = 0; i < num.length; i++) {
for (let key in object) {
const lowercaseKey = key.toLowerCase()
if (
lowercaseKey.startsWith('a') || lowercaseKey.startsWith('e')
|| lowercaseKey.startsWith('i') || lowercaseKey.startsWith('o')
|| lowercaseKey.startsWith('u') || lowercaseKey.startsWith('y')
) {
delete object[key];
}
}
}
return object;
}
console.log(
removeVowelKeys(vowelsObj)
)
本文标签: nodejsDelete a key under a condition in Javascript objectStack Overflow
版权声明:本文标题:node.js - Delete a key under a condition in Javascript object - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745579363a2157216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论