I have a simple Json String
[
{
"assetName":"LCT",
"assetValue":"",
"typeValueInput":"select",
"valueInputSelect":null,
"required":true,
"valueInput":"{\"data\":[{\"name\":\"name1\",\"id\":\"12\"},{\"name\":\"name2\",\"id\":\"13\"},{\"name\":\"name3\",\"id\":\"14\"}]}"
}
]
I want get field Name in Data in ValueInput by Javascript.
Please help me!
You need to loop through the array and then parse the stringified JSON so that you can access the data
array. Then simply loop that data
array to get the value of each name
property.
var arr = [{
"assetName": "LCT",
"assetValue": "",
"typeValueInput": "select",
"valueInputSelect": null,
"required": true,
"valueInput": "{\"data\":[{\"name\":\"name1\",\"id\":\"12\"},{\"name\":\"name2\",\"id\":\"13\"},{\"name\":\"name3\",\"id\":\"14\"}]}"
}];
arr.forEach((arrObj) => {
var jsonData = JSON.parse(arrObj.valueInput);
jsonData.data.forEach(({name}) => console.log(name));
});
You could use JSON.parse
var jsonArray = [
{
assetName: 'LCT',
assetValue: '',
typeValueInput: 'select',
valueInputSelect: null,
required: true,
valueInput:
'{"data":[{"name":"name1","id":"12"},{"name":"name2","id":"13"},{"name":"name3","id":"14"}]}'
}
];
let name = jsonArray[0].valueInput;
name = JSON.parse(name);
name.data.forEach(value => {
console.log(value.name, value.id);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With