亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從單獨的 json 對象中的值在 React.js 中創建一個新對象

從單獨的 json 對象中的值在 React.js 中創建一個新對象

慕田峪4524236 2022-06-09 17:10:52
我有一個 json 對象,我想從中獲取一些值來在 React.js 中創建一個新對象。但是,無論我嘗試什么,我都會收到與未定義的值或鍵相關的錯誤。json{    "meat":{"drink":"Bovril", "courses":{ "main":"chicken", "pudding":"jelly" },    "vegetarian":{"drink":"milkshake", "courses":{"main":"cheese","pudding":"ice cream"},    "vegan":{"drink":"spinach juice", "courses":{"main":"lettuce","pudding":"apple"}}期望的結果如果我要對其進行硬編碼,我想動態創建一個名為defaultValues的對象,該對象與以下內容匹配。如您所見,這是根據上述 json 文件中的值創建的:const defaultValues: {    meat: "chicken",    vegetarian: "cheese",    vegan: "lettuce"  }我根據類似問題的答案嘗試了以下方法,但它不起作用:我的嘗試const json = Json; //this contains the contents of my json file aboveconst defaultValues = {};Object.keys(json).forEach(function(key) {  defaultValues[key.meat].push([key.courses.main]);});錯誤我不斷收到以下錯誤:×TypeError: Cannot read property 'push' of undefined誰能建議如何做到這一點?
查看完整描述

3 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

首先,我糾正了你的json。之后,我迭代 json 對象的鍵以創建 defaultValues 對象。在您的嘗試中,您一直將對象視為數組。只有數組有 push 方法。


const json = {

    "meat": {

        "drink":"Bovril", 

        "courses":{ 

            "main":"chicken", 

            "pudding":"jelly" 

        }

    },

    "vegetarian":{

        "drink":"milkshake", 

        "courses":{

            "main":"cheese",

            "pudding":"ice cream"

        }

    },

    "vegan":{

        "drink":"spinach juice", 

        "courses":{

            "main":"lettuce",

            "pudding":"apple"

        }

    }

};


const defaultValues = {};


Object.keys(json).forEach(e => {

  defaultValues[e] = json[e]["courses"].main;

});


console.log(defaultValues);


查看完整回答
反對 回復 2022-06-09
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

不完全確定我是否正確閱讀了您的問題,但是如果您嘗試生成


const defaultValues: {

    meat: "chicken",

    vegetarian: "cheese",

    vegan: "lettuce"

}

從這個 json 對象 - (通過驗證器將其卡住,結果并不滿意,這是更正的格式)


const Json= {

    "meat": {"drink": "Bovril","courses": {"main": "chicken","pudding": "jelly"}},

    "vegetarian": {"drink": "milkshake","courses": {"main": "cheese","pudding": "ice cream"}},

    "vegan": {"drink": "spinach juice","courses": {"main": "lettuce","pudding": "apple"}}

}

然后下面應該工作


const json = Json; //this contains the contents of my json file above


const defaultValues = {};

Object.keys(json).forEach(function(key) {

  defaultValues[key] = json[key].courses.main;

});


查看完整回答
反對 回復 2022-06-09
?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

您收到錯誤的原因是因為您在對象上使用了數組方法。您的 defaultValues 是一個對象 {}


另一個問題是 json 是一個字符串,您需要先將字符串解析為一個對象。您還缺少一些 json 上的端花括號。


這是我對您的代碼的看法:


const json = `{

  "meat": {

      "drink": "Bovril", "courses": { "main": "chicken", "pudding": "jelly" }},

  "vegetarian": {

      "drink": "milkshake", "courses": { "main": "cheese", "pudding": "ice cream" }},  

  "vegan": {

      "drink": "spinach juice", "courses": { "main": "lettuce", "pudding": "apple" }}

}`;


const menu = JSON.parse(json)


const defaultValues = {};

Object.entries(menu).forEach(function (entry) {

  defaultValues[entry[0]] = entry[1].courses.main;

});

console.log(defaultValues) // -> Object {meat: "chicken", vegetarian: "cheese", vegan: "lettuce"}

Object.entries 返回一個包含對象鍵長度的數組以及一個包含鍵和值的數組。因此,只需使用 entry[0] 作為鍵和 entry[1] 作為值將它們映射到 defaultValues。


查看完整回答
反對 回復 2022-06-09
  • 3 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號