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;
}
});