0

I have a json like below

[
 {"label":"Finder1","count":3},
 {"label":"Finder2","count":1},
 {"label":"Finder1 & Finder2","count":1}
]

I want to show the data in frontend in span tag. for ex:

Finder1 (3)
Finder2 (1)
Finder1 & Finder2 (1)

Please help me the little java script to achieve this. enter image description here

2 Answers 2

2

Try this:Loop through the data and construct the desired string with it.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <script type="text/javascript">
    var counter = 0;
    $(function(){
    	var arr=[
    	 {"label":"Finder1","count":3},
    	 {"label":"Finder2","count":1},
    	 {"label":"Finder1 & Finder2","count":1}
    	];
        var str='';
    	$.each(arr,function(index){
         str=str+arr[index].label+" ("+arr[index].count+")<br>";
    	});

    	$("#data").html(str);
    });
    </script> 

    <body>
    <span id="data"></span>
    </body>

  

Sign up to request clarification or add additional context in comments.

Comments

1

Use a simple map:

var html = '<span>' + arr.map(function (el) {
  return [el.label, ' (', el.count, ')'].join('');
}).join('<br/>') + '</span>';

document.body.insertAdjacentHTML('beforeend', html);

DEMO

1 Comment

Thank you aundy..it helped me to drive in fine tune.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.