JavaScript Display for loop values in a div

I have one array called data I need to display each values of data array in a div.This is my function

function myFunction(data)
{
    var result = "";
    for (var i = 0; i < data.length; i++) 
    {
        result +='NAME: ' + data[i].name + ', AGE: ' + data[i].age;
        console.log(result)
    }
    $('#info').text(result)
}

html:

<div id="info" style="width:750px;height:150px;border:1px solid"></div>

But its displaying only final name and age from an array.

console.log(result) will return

NAME: xxx, AGE: 20
NAME: yyy, AGE: 30

I tried like this also

result +='NAME: ' + data[i].name + ', AGE: ' + data[i].age;

But I couldn't get all datas.

Answer:1

Here you go with a solution https://jsfiddle.net/hc3L98wd/

var data = [
  {name:'xxx',age:20},
  {name:'yyy',age:30}
];

var content = "";
$.each(data, function(i){
  content += `Name : ${data[i].name}  Age : ${data[i].age}<br/>`;
});

$('#info').html(content);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="info" style="width:750px;height:150px;border:1px solid"></div>
Answer:2

You have to define var result=""; outside the for loop. And it is always better to use .html() as it allows you to render the HTML tags like <br/>. Check the code snippet:

var data = [{name:'jack',age:10},{name:'Tom',age:12},{name:'Mira',age:14}]
function myFunction(data)
{
    var result="";
    for (var i = 0; i < data.length; i++) 
    {
        result += 'NAME: ' + data[i].name + ', AGE: ' + data[i].age +"<br/>";
        $('#info').html(result);
    }

}

myFunction(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="info" style="width:750px;height:150px;border:1px solid"></div>
Answer:3

I want to merge this array using or || operator [[true,false,false],[false,false,false],[false,false,true]] so that the output is [true,false,true] is this possible with map or reduce or similar? ...

I want to merge this array using or || operator [[true,false,false],[false,false,false],[false,false,true]] so that the output is [true,false,true] is this possible with map or reduce or similar? ...

  1. javascript merge multiple arrays
  2. javascript merge multiple objects
  3. javascript merge multiple arrays of objects
  4. javascript merge multiple arrays without duplicates
  5. javascript merge multiple documents
  6. merge multiple javascript into one
  7. merge multiple javascript files into one
  8. merge multiple javascript
  9. javascript merge multiple files

I understand the word self has no special meaning in javascript. Below code, while if one writes as var Life = function( ....), it's clear but author decide to do var _ = self.Life = function(...). ...

I understand the word self has no special meaning in javascript. Below code, while if one writes as var Life = function( ....), it's clear but author decide to do var _ = self.Life = function(...). ...

I have this JSON file on my server: {"1504929411112":{"name":"user1","score":"10"},"1504929416574":{"name":"2nduser","score":"14"},"1504929754610":{"name":"usr3","score":"99"},"1504929762722":{"name":...

I have this JSON file on my server: {"1504929411112":{"name":"user1","score":"10"},"1504929416574":{"name":"2nduser","score":"14"},"1504929754610":{"name":"usr3","score":"99"},"1504929762722":{"name":...

  1. node sort json array
  2. node sort json
  3. node json sort keys
  4. node js sort json array
  5. node js sort json by value
  6. node js sort json object
  7. node js sort json
  8. node js sort json by date
  9. node js sort json alphabetically
  10. node.js sort json array

Angular cancels a $http call when the timeout promise gets resolved or when the timeout if passed as integer exceeds the specified time. I created an implementation in my projects that cancels ...

Angular cancels a $http call when the timeout promise gets resolved or when the timeout if passed as integer exceeds the specified time. I created an implementation in my projects that cancels ...

  1. what happens when session timeout in asp.net
  2. what happens when session timeout in java
  3. what happens after session timeout
  4. what happens in a timeout