JavaScript Pick JSON array object by its key

I have JSON array that looks like

var data = {
  "fields": [{
      "firstName": {
        "fieldName": "First Name",
        "required": true,
        "provided": false
      }
    },
    {
      "lastName": {
        "fieldName": "Last Name",
        "required": true,
        "provided": false
      }
    },
    {
      "email": {
        "fieldName": "Email",
        "required": true,
        "provided": false
      }
    }
  ]
}

Further down I am trying to do this:

  1. Access the fieldName of the firstName object. Is there to do that without using the index?

data.fields gives me the array of object. From there onwards, I do not seem to be able to access with the object key.

Also, data.fields[0] gives the whole firstName object but I can't seem to do data.fields[0]["field"] or data.fields[0].field

Thanks.

Answer:1

You can use .find to find a particular element in an array:

var data={"fields":[{"firstName":{"fieldName":"First Name","required":!0,"provided":!1}},{"lastName":{"fieldName":"Last Name","required":!0,"provided":!1}},{"email":{"fieldName":"Email","required":!0,"provided":!1}}]}
const firstNameFieldName = data.fields
  .find(obj => 'firstName' in obj)
  .firstName.fieldName;
console.log(firstNameFieldName);
Answer:2

I have lodash cdn included in this fiddle https://jsfiddle.net/byhh6Lyo/ console.log(_) console.log(_.range(1, 10)) _.map([1,2,3], (o) => console.log(o)) //worked The second line has an error, ...

I have lodash cdn included in this fiddle https://jsfiddle.net/byhh6Lyo/ console.log(_) console.log(_.range(1, 10)) _.map([1,2,3], (o) => console.log(o)) //worked The second line has an error, ...

  1. lodash range function

I have this PHP array ($savedRequestDates) of dates: Array ( [0] => 2018-03-29 10:56:31 [1] => 2018-03-29 10:58:09 [2] => 2018-04-12 11:28:41 [3] => 2018-04-12 13:07:25 [4] => 2018-05-...

I have this PHP array ($savedRequestDates) of dates: Array ( [0] => 2018-03-29 10:56:31 [1] => 2018-03-29 10:58:09 [2] => 2018-04-12 11:28:41 [3] => 2018-04-12 13:07:25 [4] => 2018-05-...

In this code the function sayHi() doesn't write text with style. Why? How do I apply CSS class to a document.write in a function? .class1 { color: red; font-size: 400%; } <script ...

In this code the function sayHi() doesn't write text with style. Why? How do I apply CSS class to a document.write in a function? .class1 { color: red; font-size: 400%; } <script ...

  1. word cannot apply style
  2. cannot apply paragraph style

I am writing tests for a React app using Jest and Enzyme. Enzyme 3.x introduced Adapters to provide compatibility across different versions of React. The installation documentation gives examples how ...

I am writing tests for a React app using Jest and Enzyme. Enzyme 3.x introduced Adapters to provide compatibility across different versions of React. The installation documentation gives examples how ...

  1. configure enzyme adapter
  2. configure enzyme adapter jest
  3. enzyme.configure( adapter new enzyme adapter() )
  4. jest configure enzyme adapter
  5. enzyme.configure( adapter new adapter() )
  6. enzyme-adapter-react-16 configure