【发布时间】:2020-11-01 09:40:38
【问题描述】:
我有一个具有以下格式的对象数组,
const options = [
{ key: 1, text: "Name", value: "name", icon: "sort" },
{ key: 2, text: "Time", value: "time", icon: "sort" },
{ key: 3, text: "Type", value: "type", icon: "sort" }
];
现在根据输入的格式 {fieldName,order} 我必须修改数组。基本上 order 将采用两个值“asc”或“desc”,而 fieldName 将采用 options 数组的 value 属性中的任何值。
例如:{ fieldName: "name", order : "asc"} 或 { fieldName: "type", order: "desc"}
现在基本上基于这个顺序,我已经为那个字段修改了源数组的icon字段。
如果order 为asc,则将该字段的icon 属性更改为sort up。如果其顺序为desc,则更改@987654327将该字段的@属性设置为sort down
例子
1) sortBy: { fielName: "name", order:"asc"}
//Output
[
{ key: 1, text: "Name", value: "name", icon: "sort up" },
{ key: 2, text: "Time", value: "time", icon: "sort" },
{ key: 3, text: "Type", value: "type", icon: "sort" }
];
2) sortBy: { fielName: "type", order:"desc"}
//Output
[
{ key: 1, text: "Name", value: "name", icon: "sort" },
{ key: 2, text: "Time", value: "time", icon: "sort" },
{ key: 3, text: "Type", value: "type", icon: "sort down"}
];
它应该只更新传递给它的字段icon,其余字段icon应该设置为“排序”
这是我尝试过的
const options = [
{ key: 1, text: "Name", value: "name", icon: "sort" },
{ key: 2, text: "Time", value: "time", icon: "sort" },
{ key: 3, text: "Type", value: "type", icon: "sort" }
];
function updateArray(obj)
{
const newArr = options.map(item => {
if(item.name === obj.fieldName) {
return {...item, icon: obj.order === "desc" ? "sort-desc" :"sort-asc" };
}
return {...item};
});
return newArr;
}
【问题讨论】:
标签: javascript arrays ecmascript-6 ecmascript-5 ecmascript-2016