鍍金池/ 問答/HTML/ 在 react中 json數(shù)據(jù)請求回來,是怎么對應數(shù)據(jù)模型的?

在 react中 json數(shù)據(jù)請求回來,是怎么對應數(shù)據(jù)模型的?

之前數(shù)據(jù)請求回來了,簡單使用map方法來渲染的,但現(xiàn)在有了對應的模型,一共請求兩個包括 模型:數(shù)據(jù),這個問題我覺得這個很好復雜,半天搞定不了。

參考數(shù)據(jù)

{
fadddcb83: "單行文本默認值",
f3ddd19d7: "多行文本默認值",
faddd307d: ""
},

參考模型

{
id: 1016,
table: "161",
name: "fadddcb83",
displayName: "單行文本",
type: "text",
},

首先請求url數(shù)據(jù)回來了,再次從模型里面查詢到對應的name,然后拿到了displayName值,這就比如單行文本:單行文本默認值,即是"key":"value"。

這個怎么用法?

回答
編輯回答
爆扎

是這個意思嗎

        let model = [{
            id: 1016,
            table: "161",
            name: "fadddcb83",
            displayName: "單行文本",
            type: "text"
        }, {
            id: 1016,
            table: "161",
            name: "f3ddd19d2",
            displayName: "多行文本",
            type: "text"
        }];

        let dataArray = [
            {fadddcb83: "單行文本默認值1",f3ddd19d7: "多行文本默認值1",faddd307d: ""},
            {fadddcb81: "單行文本默認值2",f3ddd19d2: "多行文本默認值2",faddd3073: ""},
            { fadddcb84: "單行文本默認值3",f3ddd19d5: "多行文本默認值3",faddd3076: ""}
        ];
        
        let resArr = [];

        dataArray.forEach(dataItem => {
            for(let key in dataItem) {
                model.forEach(item => {
                    if (key === item.name) {
                        resArr.push({
                            key: item.displayName,
                            value: dataItem[key]
                        });
                    }
                })
            }
        })
2017年2月15日 10:51