gpt4 book ai didi

javascript - 将对象数组中的特定值转换为小写

转载 作者:行者123 更新时间:2023-12-01 02:33:39 33 4
gpt4 key购买 nike

我试图弄清楚如何将对象数组中特定键的每个值转换为小写。例如,我想使用键“Domain”将每个值小写:

var array = [
{ "Name" : "Bill":,
"Domain" : "TEST.com",
},
{ "Name" : "John":,
"Domain" : "JohnTest.com",
},
{ "Name" : "Fred":,
"Domain" : "fredtest.com",
}
]

将转换为:

var newArray = [
{ "Name" : "Bill":,
"Domain" : "test.com",
},
{ "Name" : "John":,
"Domain" : "johntest.com",
},
{ "Name" : "Fred":,
"Domain" : "fredtest.com",
}
]

我知道如何将 toLowerCase() 与字符串和数组一起使用,但我似乎不知道如何映射对象数组来执行此操作。我正在尝试类似的事情:

var newArray = array.map(function(i) {
return i.Domain.toLowerCase;

})

最佳答案

map返回另一个数组,并且由于数组的每个项目都是一个对象,因此您还需要在每次迭代时返回一个对象:

const array = [
{ "Name" : "Bill:",
"Domain" : "TEST.com",
},
{ "Name" : "John:",
"Domain" : "JohnTest.com",
},
{ "Name" : "Fred:",
"Domain" : "fredtest.com",
}
]

const result = array.map(item => ({
...item,
Domain: item.Domain.toLowerCase()
}))

console.log(result)

关于javascript - 将对象数组中的特定值转换为小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48116502/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com