我有一个对象,我必须分成两个数组才能正确处理.它看起来像这样:
{
city:"stuttgart",
street:"randomstreet",
...
}
Run Code Online (Sandbox Code Playgroud)
由于它需要适合某个指令,我必须将其转换为:
[
{key:"city", value:"stuttgart"}
{key:"street", value:"randomstreet"},
...
]
Run Code Online (Sandbox Code Playgroud)
为此,我首先使用
var mapFromObjectWithIndex = function (array) {
return $.map(array, function(value, index) {
return [value];
});
};
var mapFromObjectWithValue = function (array) {
return $.map(array, function(value, index) {
return [index];
});
});
Run Code Online (Sandbox Code Playgroud)
创建两个数组,一个包含旧键,另一个包含旧值.然后我创建了另一个二维数组,将它们映射到一个单独的数组中
var mapToArray = function (arrayValue, arrayIndex) {
var tableData = [];
for (var i = 0; i<arrayIndex.length; i++){
tableData[i] = {key:arrayIndex[i] , value:arrayValue[i]};
}
return tableData;
};
Run Code Online (Sandbox Code Playgroud)
(也许我已经搞砸了,这可以更轻松吗?)
现在,我使用数组(tableData)来显示表单中的数据.可以编辑值字段.最后,我想将数组(tableData)转换为原始数组.(见第一个对象)
请注意,原始对象不仅包含字符串作为值,还可以包含对象.
我有一些接口,如下所示:
export enum SortValueType {
String = 'string',
Number = 'number',
Date = 'date',
}
export interface SortConfig {
key: string;
direction: SortDirection;
type: SortValueType;
options?: {
sortBy?: 'year' | 'day';
};
}
Run Code Online (Sandbox Code Playgroud)
我希望能够扩展它,以便可能的类型options.sortBy应该依赖于type. 我正在创建迭代器,type因此它不应该创建一个对象,其中typeisstring且options.sortBy值为year。
这是getIteratee函数:
private getIteratee(config: SortConfig) {
if (config.type === SortValueType.String) {
return item => _lowerCase(item[config.key]);
}
if (config.type === SortValueType.Date) {
const sortBy = _get(config, 'options.sortBy') as 'year' | 'day'; …Run Code Online (Sandbox Code Playgroud)