在State
块中执行映射后,我要在delete entry.State;
数组中删除Entries
属性(reduce
)。我觉得这段代码是有气味的。
完成映射后,我不再需要State
属性,删除它的正确方法是什么?
您可以看到我正在返回不带状态的状态和数据对象列表(已删除)。
const data = {
FirstName: "Paul",
LastName: "Allen",
Email: "name@domain.com",
Entries: [
{
EntryId: "FF-DD",
Title: "Item 1",
State: [
{ Name: "Lock 1", Time: "1563479611" },
{ Name: "Lock 2", Time: "1563479663" }
]
},
{
EntryId: "GG-RR",
Title: "Item 2",
State: [
{ Name: "Run 1", Time: "116479611" },
{ Name: "Run 2", Time: "126347963" }
]
}
]
};
const newData = createArray(data);
console.log(JSON.stringify(newData, null, 2));
function createArray(data) {
let cloneData = { ...data };
cloneData.EmailAddress = cloneData.Email;
delete cloneData.Email;
const states = cloneData.Entries.reduce((stateBlock, entry) => {
const states = entry.State.map(state => {
const CreatedTime = state.Time;
const newState = {
...state,
CreatedTime: CreatedTime,
EntryId: entry.EntryId
}
delete newState.Time;
return newState;
});
stateBlock.push(...states);
delete entry.State;
return stateBlock;
}, []);
return [cloneData, ...states];
}
输出按预期运行:
[
{
"FirstName": "Paul",
"LastName": "Allen",
"Entries": [
{
"EntryId": "FF-DD",
"Title": "Item 1"
},
{
"EntryId": "GG-RR",
"Title": "Item 2"
}
],
"EmailAddress": "name@domain.com"
},
{
"Name": "Lock 1",
"CreatedTime": "1563479611",
"EntryId": "FF-DD"
},
{
"Name": "Lock 2",
"CreatedTime": "1563479663",
"EntryId": "FF-DD"
},
{
"Name": "Run 1",
"CreatedTime": "116479611",
"EntryId": "GG-RR"
},
{
"Name": "Run 2",
"CreatedTime": "126347963",
"EntryId": "GG-RR"
}
]
时间已重命名为CreatedTime,并且还在每个州都包含EntryId。删除Entries
答案 0 :(得分:2)
您可以使用destructuring assignments with spread operators并嵌套.map()
来获得一些创意,以完成此任务。
代码中的详细信息。
const data = { FirstName: "Paul", LastName: "Allen", Email: "name@domain.com", Entries: [ { EntryId: "FF-DD", Title: "Item 1", State: [ { Name: "Lock 1", Time: "1563479611" }, { Name: "Lock 2", Time: "1563479663" } ] }, { EntryId: "GG-RR", Title: "Item 2", State: [ { Name: "Run 1", Time: "116479611" }, { Name: "Run 2", Time: "126347963" } ] } ] };
const newData = (() => {
let states = [];
//Loop through Entries
let Entries = data.Entries.map(({State, ...others}) => {
if (State) {
//Map each state to new format
let state = State.map(({Time, ...obj}) => ({...obj, CreatedTime: Time, EntryId: others.EntryId}));
states.push(...state);
}
//Return Entitites without State
return others;
});
//Remove LastName and Email from root object
let {LastName, Email, ...dataWithoutName} = data;
//Change Email to EmailAddress, combine all the pieces
return [ {EmailAddress: Email, ...dataWithoutName, Entries}, ...states ];
})();
console.log(newData);
答案 1 :(得分:-1)
如果要就地修改列表中每个项目的内容,请使用map
。如果要将列表“简化”为一个值,请使用reduce
。
function createArray(data) {
let clone = { ...data };
clone.Entries = clone.Entries.map(entry => {
return Object.keys(entry).reduce((a, c) => {
if(c === 'State') return a;
a[c] = entry[c]
return a;
}, {})
});
return clone;
}
在给定数据上运行此操作将返回
{
"FirstName":"Paul",
"LastName":"Allen",
"Email":"name@domain.com",
"Entries":[
{
"EntryId":"FF-DD",
"Title":"Item 1"
},
{
"Id":"GG-RR",
"Title":"Item 2"
}
]
}
编辑:没有意识到您的条目中有两个不同的键,Id
和EntryId
。