gpt4 book ai didi

javascript - 使用 lodash 将子属性推送到父对象

转载 作者:行者123 更新时间:2023-11-30 09:13:10 26 4
gpt4 key购买 nike

我有如下嵌套对象:

{
Campaign_Type: 1
Campaign_TypeFull: {text: "Test my campaign", value: 1}
End_Date: "07/26/2019"
Live_Date: "07/15/2019"
Name: "Death of An Art Collector - Book Signings"
Status: "ACTIVE"
}

我需要的输出是将 Campaign_TypeFull 子对象转换为父对象属性,因为我的 react Material 表只接受平面数组。

{
Campaign_Type: 1
text: "Test my campaign",
value: 1,
End_Date: "07/26/2019"
Live_Date: "07/15/2019"
Name: "Death of An Art Collector - Book Signings"
Status: "ACTIVE"
}

我尝试使用 flatmap 或 map 但无法正常工作(我正在使用 lodash)。

let griddata = flatMap(tabledata,({ _id,Name, Status,Live_Date, End_Date, Campaign_TypeFull }) => map(Campaign_TypeFull, Campaign_TypeFull => ({...Campaign_TypeFull, _id,Name, Status,Live_Date, End_Date })))

提前致谢。

最佳答案

你可以很容易地通过一些对象解构来做到这一点

const data = { Campaign_Type: 1,Campaign_TypeFull: { text: "Test my campaign",value: 1},End_Date:"07/26/2019",Live_Date: "07/15/2019",Name: "Death of An Art Collector - Book Signings",Status: "ACTIVE"}


const flattenCampaign = ({Campaign_TypeFull, ...rest}) => {
return Object.assign({}, Campaign_TypeFull, rest);
};

console.log(flattenCampaign(data))

关于javascript - 使用 lodash 将子属性推送到父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57095109/

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