gpt4 book ai didi

javascript - FirstLetterCaps 类似于 item.toLowerCase()

转载 作者:行者123 更新时间:2023-12-03 04:41:26 25 4
gpt4 key购买 nike

我有一些详细信息,例如,

DataSet: Group: [Subvalues]
Fruits: [Orange, Apple]
Chocolate: [Dairy Milk, Kit Kat]

这些是静态项目,最初在变量初始化期间推送。现在我想动态地对一些项目进行分类,以便将其插入每个相关组中。

举例来说,如果我给出 FRUITS: Pine Apple,它应该添加为 Fruits 组中的第三个项目,但它会添加到新的组名称 “FRUITS”全大写,

我尝试过将所有这些元素都推小,然后效果很好,

fruits: [orange, apple, pine apple]

但是,实际上,我想让每个单词的第一个字母大写,例如,

Fruits: [Orange, Apple, Pine Apple]

最终的对象映射就像,

var value = ko.observableArray([]);
value(FRUITS: Pine Apple, Fruits: Apple, Fruits: Orange, Chocolate: Dairy Milk, Chocolate: Kit Kat);

var mapUpdated = value().reduce(function (map, item) {
var key = item.split(':')[0];
map[key] = map[key] || [];
map[key].push(item.split(':')[1] || []);
return map;
}, {});

这张更新后的 map 显示了诸如以下的详细信息:

FRUITS: Pine Apple
Fruits: Apple, Orange
Chocolate: Dairy Milk, Kit Kat

我必须将 Pine Apple 推送到 Fruits 组。

对此的任何帮助/建议将不胜感激。

最佳答案

将第一个字母大写,并将其他所有字母转换为小写:

var selectedSkillCluster = ko.observableArray([]);
selectedSkillCluster(FRUITS: Pine Apple, Fruits: Apple, Fruits: Orange, Chocolate: Dairy Milk, Chocolate: Kit Kat);

var mapUpdated = selectedSkillCluster().reduce(function (map, item) {
var key = item.split(':')[0];
key = key.charAt(0).toUpperCase() + key.substring(1).toLowerCase();
map[key] = map[key] || [];
map[key].push(item.split(':')[1] || []);
return map;
}, {});

关于javascript - FirstLetterCaps 类似于 item.toLowerCase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070583/

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