[英]How to convert a React array to a JSON object with same key and value?
我的 React 應用程序[profile1, profile2, profile3]
有一個從數據庫讀取的值數組,我需要將這些值轉換為可在第三方組件中使用的 json 對象。 生成的 json 需要是:
{profile1: "profile1", profile2: "profile2", profile3: "profile2"}
我不熟悉 JavaScript 和 React。
const object = {}; const array = ["one", "two", "three"]; array.forEach(v => object[v] = v); console.log(object);
我建議做這樣的事情
const profiles = ['profile1', 'profile2', 'profile3']; let obj = {}; profiles.forEach(profile => obj[profile] = profile); console.log(obj);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.