1

How to fetch valid external json file and filter it by string value (eg:sam), finaly place it inside a div.

[{
"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/"}]

<script>fetch("worker.json")
.then((response) => response.json())document.getElementById("demo").innerHTML = .then((json) => json.filter(function(item){
return item.name == "sam"; }));</script>
<p id="demo"></p>

2 Answers2

0

As you are using fetch, so you can do this;

let result;
fetch('pass url and config').then(res => {
    result = res.json() 
})

//result will look like this;
[{
"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/"}]

result.map(obj => {
if(obj.name == 'sam') {
document.getElementById('details').innerHTML += obj.age + ' ' + obj.salery + ' ' + obj.portfolio + '  '
}
})
<div id='details'></div>
Irfan wani
  • 4,084
  • 2
  • 19
  • 34
0

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.

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46