I have an array that looks like this -
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
}
]
and I need to duplicate / copy / convert / ...whatever... that array to something like this -
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",
}
so that I can reference it by
console.log(myNewArray.field_1.title)
or something more usable.
I have attempted to use the filter method to no avail. Everything I've attempted just returns undefined. I'm just super confused. Is there a better way to reference the elements in the sub array directly without converting?
This was sorta working... the console.log would output what I wanted but the returned value would output as undefined, which is confusing me.
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; } });
You could map the items in new objects with the wanted properties as objects.
It works with
Array#map
, field
with another object with the renamed properties, Object.assign
and spread syntax ...
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; }
I am guessing all of the processing should be in the $.get function :
$.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);
};
You can write a simple reduce
call to get the desired output:
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);
The answer is very simple:
const objs = new Map();
for (const obj of myOldArray) {
objs.set(obj.form_field_name, obj);
}
And now you can access your objects by the field name:
const myObj = objs.get("field_1");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.