在<h1> </h1>中创建名称数组,并在<h2> </h2>中创建年龄

时间:2019-05-06 03:16:08

标签: javascript

我想对public void read3RegularGraphs( String pathFile ) throws IOException { InputStream reader = new FileInputStream(pathFile); byte [] fileBytes = Files.readAllBytes(new File(pathFile).toPath()); char singleChar; for(byte b : fileBytes) { singleChar = (char) b; System.out.print(singleChar); } } 中的名称和<h1></h1>中的年龄进行排列

我可以使用map()方法吗?以及它如何运作? 旁边,有解决办法吗?

<h2></h2>

非常感谢!

4 个答案:

答案 0 :(得分:1)

是的,您可以使用map方法。 Map的工作原理是遍历数组中的每个条目,然后根据每次迭代时return的内容确定特定数组条目将转换为(即映射到)什么。因此,您可以简单地返回包装在<h1></h1><h2></h2>标签中的每个数组条目对象属性,如下所示:

function readyToPutInTheDOM(arr) {
  return arr.map(function(obj) {
    return "<h1>" +obj.name +"</h1>" + "<h2>" + obj.age +"</h2>";
  });
}

var result = readyToPutInTheDOM([{name:"Angelina Jolie",age:80},{name:"Eric Jones",age:2},{name:"Paris Hilton",age:5},{name:"Kayne West",age:16},{name:"Bob Ziroll",age:100}]);
console.log(result);

或者,您可以使用ES6箭头功能,解构和模板文字:

const readyToPutInTheDOM = arr => 
  arr.map(({name, age}) => `<h1>${name}</h1><h2>${age}</h2>`);

const result = readyToPutInTheDOM([{name:"Angelina Jolie",age:80},{name:"Eric Jones",age:2},{name:"Paris Hilton",age:5},{name:"Kayne West",age:16},{name:"Bob Ziroll",age:100}]);
console.log(result);

答案 1 :(得分:0)

您可以像这样使用forEachmap

function readyToPutInTheDOM(arr) {
  arr.map(c=> {
     document.write("<h1>" + c.name + "</h1>");
     document.write("<h2>" + c.age+ "</h2>");
  });

  arr.forEach(c=> {
     document.write("<h1>" + c.name + "</h1>");
     document.write("<h2>" + c.age+ "</h2>");
  });
}

function readyToPutInTheDOM(arr) {
  arr.map(c=> {
     document.write("<h1>" + c.name + "</h1>");
     document.write("<h2>" + c.age+ "</h2>");
  });
  
  arr.forEach(c=> {
     document.write("<h1>" + c.name + "</h1>");
     document.write("<h2>" + c.age+ "</h2>");
  });
}

readyToPutInTheDOM([
  {
    name: "Angelina Jolie",
    age: 80
  },
  {
    name: "Eric Jones",
    age: 2
  },
  {
    name: "Paris Hilton",
    age: 5
  },
  {
    name: "Kayne West",
    age: 16
  },
  {
    name: "Bob Ziroll",
    age: 100
  }
]) 

答案 2 :(得分:0)

使用each圈入JSON数据,并按如下所示绑定到DOM。

var data = [{
    name: "Angelina Jolie",
    age: 80
  },
  {
    name: "Eric Jones",
    age: 2
  },
  {
    name: "Paris Hilton",
    age: 5
  },
  {
    name: "Kayne West",
    age: 16
  },
  {
    name: "Bob Ziroll",
    age: 100
  }
];
var nHTML = '';
$(data).each(function(i, val) {
  nHTML += '<h1>' + val['name'] + '</h1>';
  nHTML += '<h2>' + val['age'] + '</h2>';
});
$('#display').html(nHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id='display'></div>

答案 3 :(得分:0)

您的问题不清楚,因此我不确定您想要的是什么

declare @myvar varchar(100)

set @myvar:= 'wantedtoremovethisurl.combeforethecomma,showthistextthough'

select replace(@myvar,left(@myvar,instr(@myvar,',')),'')