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:
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.
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);
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, ...
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 ...
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 ...