簡體   English   中英

轉換Javascript多維數組格式

[英]Converting Javascript Multidimensional Array format

我有一個看起來像這樣的數組-

var myOldArray = [{
        "id": 1,
        "form_id": 4,
        "form_field_name": "field_1",
        "helperTitle": "This is Box 1's TItle",
        "helperText": "This is Box 1 data",
        "created_at": null,
        "updated_at": null
    },
    {
        "id": 2,
        "form_id": 4,
        "form_field_name": "field_2",
        "helperTitle": "Box 2 Title",
        "helperText": "Box 2 TExt",
        "created_at": null,
        "updated_at": null
    }
]

而且我需要復制/復制/轉換/ ...任何...該數組為這樣的東西-

myNewArray = {
  field_1['title'] = "This is Box 1's Title",
  field_1['text'] = "This is Box 1 data",
  field_2['title'] = "Box 2 Title",
  field_2['text'] = "Box 2 Text",
}

以便我可以參考

  console.log(myNewArray.field_1.title) 

或更有用的東西。

我試圖使用過濾器方法無濟於事。 我嘗試過的一切都返回未定義。 我只是超級困惑。 有沒有更好的方法直接引用子數組中的元素而不進行轉換?

這有點工作... console.log將輸出我想要的內容,但是返回值將輸出為undefined,這使我感到困惑。

 myOldArray = [{ "id": 1, "form_id": 4, "form_field_name": "field_1", "helperTitle": "This is Box 1's TItle", "helperText": "This is Box 1 data", "created_at": null, "updated_at": null }, { "id": 2, "form_id": 4, "form_field_name": "field_2", "helperTitle": "Box 2 Title", "helperText": "Box 2 TExt", "created_at": null, "updated_at": null } ] var AR = myOldArray; var newArr = AR.filter(function(item) { if (item.form_field_name == fieldName) { console.log('txt - ' + item + '\\n\\n'); return item; } }); 

您可以將所需屬性作為對象映射到新對象中。

它與

 var data = [{ id: 1, form_id: 4, form_field_name: "field_1", helperTitle: "This is Box 1's TItle", helperText: "This is Box 1 data", created_at: null, updated_at: null }, { id: 2, form_id: 4, form_field_name: "field_2", helperTitle: "Box 2 Title", helperText: "Box 2 TExt", created_at: null, updated_at: null }], result = Object.assign( ...data.map(({ id, helperTitle: title, helperText: text }) => ({ ['field_' + id]: { title, text } })) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

我猜所有的處理都應該在$ .get函數中:

$.getJSON('/api/system/formHelperText/4', function (data) { 
    var myNewArray = data.map(function(item) {
        var obj = {};
        obj['field_' + item.id] = { Title: item.helperTitle, Text: item.helperText };
        return obj;
    });
    console.log(myNewArray);
};

您可以編寫一個簡單的reduce調用以獲得所需的輸出:

 const array=[{id:1,form_id:4,form_field_name:"field_1",helperTitle:"This is Box 1's TItle",helperText:"This is Box 1 data",created_at:null,updated_at:null},{id:2,form_id:4,form_field_name:"field_2",helperTitle:"Box 2 Title",helperText:"Box 2 TExt",created_at:null,updated_at:null}] const result= array.reduce((acc, a) => (acc[a.form_field_name] = {title: a.helperTitle,text: a.helperText}, acc), {}); console.log(result); 

答案很簡單:

const objs = new Map();

for (const obj of myOldArray) {
    objs.set(obj.form_field_name, obj);
}

現在,您可以通過字段名稱訪問對象:

const myObj = objs.get("field_1");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM