Javascript 在不可变映射中更改键
在不可变映射中动态更改密钥的最佳方法是什么Javascript 在不可变映射中更改键,javascript,immutable.js,Javascript,Immutable.js,在不可变映射中动态更改密钥的最佳方法是什么 // I have a map like this: const names = Map({ rya: true, mike: false, }); // I receive this as inputs const inputIndex = 0; const inputLetter = n; // What are the operations that i need to do here to get the expected outp
// I have a map like this:
const names = Map({
rya: true,
mike: false,
});
// I receive this as inputs
const inputIndex = 0;
const inputLetter = n;
// What are the operations that i need to do here to get the expected output
// [?CODE?]
// I expect this as output:
const names = Map({
ryan: true,
mike: false,
});
到目前为止,我的解决方案似乎效率低下:
const namesEntrySeq = names.entrySeq();
const objectToModify = namesEntrySeq.get(inputIndex);
objectToModify[0] = objectToModify[0] + inputLetter;
const namesAsArray = namesEntrySeq.toArray();
namesAsArray[inputIndex] = objectToModify;
const names = Immutable.Map(namesAsArray);
必须有一种更优雅的方式吗?首先,您的输入似乎不正确,无法与Map一起使用 Map通常是HashMap。这样,钥匙就不必遵守任何命令。您的输入似乎是这样的:嘿,通过在键的末尾添加一个字母来更改索引“inputIndex”的键 如果无法更改此输入,则可能需要一个列表以按某种顺序保存键
// do not use const here. You will need to update that reference.
let keys = List(['rya', 'mike']); // keep that updated
let names = Map({
'rya': true,
'mike': false
});
const changeKey = function (inputIndex, inputLetter) {
let key = keys.get(inputIndex);
let newKey = key + inputLetter;
names = names.withMutations(map => {
var value = map.get(key);
map.delete(key);
map.set(newKey, value);
});
keys = keys.set(inputIndex, newKey);
};
changeKey(0, 'n');
我认为这应该行得通。首先,您的输入似乎不适合与Map一起使用 Map通常是HashMap。这样,钥匙就不必遵守任何命令。您的输入似乎是这样的:嘿,通过在键的末尾添加一个字母来更改索引“inputIndex”的键 如果无法更改此输入,则可能需要一个列表以按某种顺序保存键
// do not use const here. You will need to update that reference.
let keys = List(['rya', 'mike']); // keep that updated
let names = Map({
'rya': true,
'mike': false
});
const changeKey = function (inputIndex, inputLetter) {
let key = keys.get(inputIndex);
let newKey = key + inputLetter;
names = names.withMutations(map => {
var value = map.get(key);
map.delete(key);
map.set(newKey, value);
});
keys = keys.set(inputIndex, newKey);
};
changeKey(0, 'n');
我认为这应该行得通。如果您依赖于地图中实体的顺序保持不变,那么您应该使用或作为并自己跟踪顺序。 如果使用的是
OrderedMap
,则可以使用更新其中的密钥:
const names=Immutable.Map({
是的,
迈克:错,
});
常量letterToAdd='n';
const indexToUpdate=0;
const updatedNames=names.mapEntries(([key,value],index)=>{
如果(索引===索引输出日期){
返回[键+字母添加,值];
}否则{
返回[键,值];
}
});
console.log(更新的名称)代码>
如果您依赖于地图中实体的顺序保持不变,那么您应该使用或作为,并自己跟踪顺序。
如果使用的是OrderedMap
,则可以使用更新其中的密钥:
const names=Immutable.Map({
是的,
迈克:错,
});
常量letterToAdd='n';
const indexToUpdate=0;
const updatedNames=names.mapEntries(([key,value],index)=>{
如果(索引===索引输出日期){
返回[键+字母添加,值];
}否则{
返回[键,值];
}
});
console.log(更新的名称)代码>
您可以使用mapKeys
和OrderedMap
执行此操作,因为其他人已经注意到,接受输入索引假定您的地图具有特定的顺序
const { Map, OrderedMap, List } = require('immutable')
const names = Map({
rya: true,
mike: false,
});
const orderedNames = OrderedMap(names)
const inputKey = List(orderedNames.toSeq()).get(inputIndex)
orderedNames.mapKeys((k,v) => (k === inputKey) ? k + inputLetter : k)
瞧
Map({
ryan: true,
mike: false,
});
您可以使用mapKeys
和OrderedMap
来实现这一点,因为其他人已经注意到,接受输入索引假定您的地图具有特定的顺序
const { Map, OrderedMap, List } = require('immutable')
const names = Map({
rya: true,
mike: false,
});
const orderedNames = OrderedMap(names)
const inputKey = List(orderedNames.toSeq()).get(inputIndex)
orderedNames.mapKeys((k,v) => (k === inputKey) ? k + inputLetter : k)
瞧
Map({
ryan: true,
mike: false,
});