0

I have a react js/javascript object with a bunch of entries and the general format looks like this:

export const myJsonArray = [
{
    headerName: 'Id',
    headerTooltip: 'Id',
    field: 'id',
    width: myColWidth,
    sortable: true,
    filter: true,
},
{
    headerName: 'Bank Code',
    headerTooltip: 'Bank Code',
    field: 'bankCode',
    width: myColWidth,
    sortable: true,
    filter: true,
    autoHeight: true,
    editable: true,
 },
]

I want to extract only the field values and build a new object that looks like:

 {
    id: '',
    bankCode: '',
    ...
    fieldn: ''
 }

With the field name as the keys and an empty string as the value. Trying to start with something like this to extract just the field names.

  let arr = [];
  myJsonArray.map((column) => {
    arr.push(column.field);
  });

In Java there is a JSONObject and you can add key/value pairs to an object like this:

 JSONObject jo = new JSONObject();
 jo.put("firstName", "John");

There must be a simple way to dynamically build an object in javascript/react js but surprisingly not finding what I need. I'm thinking this can be done in two or three lines of code. What is the recommended way to handle this transformation in javascript or react js?

mba12
  • 2,702
  • 6
  • 37
  • 56
  • 1
    When I see consecutively numbered properties like this, my first thought is that an array would be more suitable. Why do you need/want an object like that? Creating an object property is easy: `obj.prop = value;` or `obj['prop'] = value;`. – Felix Kling Oct 07 '20 at 18:07
  • I want to add a new empty row to an ag-grid table. So I need to pass an object to ag-grid with the column names and empty values. A user will then manually fill in the values in the table once an empty row has been added. – mba12 Oct 07 '20 at 18:08
  • 1
    If you want to get the `field` properties of each object as key, then initialize it to an object literal instead of an array: `let o = {}` and then `myJsonArray.foreach((column) => o[column.field] = '' )` – adiga Oct 07 '20 at 18:08
  • Technical note: what you're showing is not JSON, it's a normal JS array of plain JS objects. That may seem nitpicking, but it's not: JSON is string data. It is not "code" that can do anything, it is plain (if structured) text and needs to be parsed into JS before you can do anything, at which point things stop having anything to do with JSON. – Mike 'Pomax' Kamermans Oct 07 '20 at 18:10
  • 1
    You could also use: `const o = Object.fromEntries( myJsonArray.map(o => [o.field, '']) )` – adiga Oct 07 '20 at 18:27

1 Answers1

2

const myJsonArray=[{headerName:"Id",headerTooltip:"Id",field:"id",width:20,sortable:!0,filter:!0},{headerName:"Source Bank Code",headerTooltip:"Source Bank Code",field:"field1",width:10,sortable:!0,filter:!0,autoHeight:!0,editable:!0}];

const result = {}
myJsonArray.forEach((obj, idx) => {
  if(obj.field){
    result[obj.field] = ''
  }
})
 
 console.log(result)
adiga
  • 34,372
  • 9
  • 61
  • 83
symlink
  • 11,984
  • 7
  • 29
  • 50