将json对象值变为javascript中的键和值
我有一个json格式,如下所示。我很困惑,我们可以把第一个值作为键,把第二个值作为键的内容吗将json对象值变为javascript中的键和值,javascript,jquery,arrays,json,object,Javascript,Jquery,Arrays,Json,Object,我有一个json格式,如下所示。我很困惑,我们可以把第一个值作为键,把第二个值作为键的内容吗 [ { "configSlug": "receiptStoreName", "configContent": "The Store Name" }, { "configSlug": "receiptStoreAddress",
[
{
"configSlug": "receiptStoreName",
"configContent": "The Store Name"
},
{
"configSlug": "receiptStoreAddress",
"configContent": "Cattle Street"
},
{
"configSlug": "receiptStorePhone",
"configContent": "01 123234"
},
{
"configSlug": "receiptStoreFoot1",
"configContent": "Thanks For Visiting"
}
]
预期结果:
{
"receiptStoreName": "The Store Name",
"receiptStoreAddress": "Cattle Street",
"receiptStorePhone": "01 123234",
"receiptStoreFoot1": "Thanks For Visiting"
}
谢谢您的帮助。您可以使用:
const obj=[
{
“configSlug”:“receiptStoreName”,
“configContent”:“存储名称”
},
{
“configSlug”:“receiptStoreAddress”,
“内容”:“牛街”
},
{
“configSlug”:“receiptStorePhone”,
“配置内容”:“01 123234”
},
{
“configSlug”:“receiptStoreFoot1”,
“配置内容”:“感谢访问”
}
];
const result=Object.fromEntries(obj.map(entry=>[entry.configSlug,entry.configContent]);
控制台日志(结果)代码>您可以这样使用:
const数据=[
{
“configSlug”:“receiptStoreName”,
“configContent”:“存储名称”
},
{
“configSlug”:“receiptStoreAddress”,
“内容”:“牛街”
},
{
“configSlug”:“receiptStorePhone”,
“配置内容”:“01 123234”
},
{
“configSlug”:“receiptStoreFoot1”,
“配置内容”:“感谢访问”
}
];
常数结果=数据减少((r,e)=>{
r[e.configSlug]=e.configContent;
返回r;
}, {});
控制台日志(结果)代码>请发布您自己编写的代码,试图解决此问题,以便我们可以帮助您调试它。请记住,SO在这里帮助您解决代码中的问题,而不是为您编写代码。如果您在处理此问题时需要帮助,请研究方法。寻求帮助。期待使用reduce方法
data.reduce((obj, item) => { obj[item.configSlug] = item.configContent; return obj; }, {});