Sorry for the rookie question but I couldn't find an answer on similar questions.
I have an object made up of hundreds of json strings and I want to iterate through the object and return specific values from each string.
I have this code
const leo =
'{"contractName":"tokens","contractAction":"stake","contractPayload":{"to":"hivebuilder","symbol":"LEO","quantity":"0.161"}}';
const obj = JSON.parse(leo);
console.log(obj.contractPayload.to, obj.contractPayload.quantity);
Which returns:
hivebuilder 0.161
That's exactly what I want, but I want to do it to a nested json object like this:
const leo = [
'{"contractName":"tokens","contractAction":"stake","contractPayload":{"to":"hivebuilder","symbol":"LEO","quantity":"0.161"}}',
'{"contractName":"tokens","contractAction":"stake","contractPayload":{"to":"pele23","symbol":"LEO","quantity":"1.031"}}',
];
My expected result would be:
hivebuilder 0.161
pele23 1.031
I know I have to use a loop to iterate through each string, but I can't wrap my head around how to do it, any help please?
try this
let names = leo.map(function (item) {
const elem = JSON.parse(item);
return elem.contractPayload.to + " " + elem.contractPayload.quantity;
});
console.log(JSON.stringify(names));
result
["hivebuilder 0.161","pele23 1.031"]