Untested but you ought to be able to do something like this - the data being saved as worker.json
located in the same directory as operating script.
[
{
"name": "sam",
"age": "26",
"salery": "20000",
"portfolio": "https://www.example.com/"
},
{
"name": "tony",
"age": "30",
"salery": "30000",
"portfolio": "https://www.example.com/"
},
{
"name": "sam",
"age": "24",
"salery": "15000",
"portfolio": "https://www.example.com/"
}
]
And processed by:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>fetch json & filter</title>
</head>
<body>
<p id="demo"></p>
<script>
fetch("worker.json")
.then( r=>r.json() )
.then( json=>{
Object.keys( json ).forEach(k=>{
let obj=json[k];
if( obj.name=='sam' ){
document.getElementById("demo").innerHTML+=[
'Name:'+obj.name,
'Age:'+obj.age,
'Salary:'+obj.salery,
'Portfolio:<a href="'+obj.portfolio+'" target="_blank">'+obj.portfolio+'</a>',
'<br />'
].join('<br />')
}
})
})
</script>
</body>
</html>
As there are two sams
in the supplied data and no indication which to filter the use of +=
when calling document.getElementById("demo").innerHTML
will add to any existing content - thus both entries should appear in the displayed output.