从JavaScript中的API动态渲染对象(object/api/dynamic渲染)

发布于 2025-02-13 22:49:38 字数 188 浏览 1 评论 0原文

我有一个数组和对象,并在API的帮助下带来了。而且,如果我输入对象的名称输入,则必须找到该对象并将其其他属性渲染到 same same 容器中使用我的输入我输入了名称。 实际上,这应该在 javascript 中完成。

I have an array and objects in it, brought with the help of API. And If I enter name of an object to input it have to find that object and render its other properties to specific tags of HTML which are located in the same container with my input I entered name.
Actually this should have done in Javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合约呢 2025-02-20 22:49:38

首先,为了通过其名称找到对象,您可以使用arr.filter():

var desiredName = "Name 1" //The name entered into the input
const objects = [{name: "Name 1", name: "Name 2"}] //Your array of objects

//Use arr.filter() to find the item with the desired name
var selectedObject = objects.filter((item) => {
  return item.name = desiredName
})[0] //Target the first element in the returned array

至于使用所选对象属性填充段落元素,您可以只使用document.getelemntbyid():

document.getElementById("someElement").innerHTML = "Your Text"
<p id="someElement"></p>

First, in order to find an object by its name, you can use arr.filter():

var desiredName = "Name 1" //The name entered into the input
const objects = [{name: "Name 1", name: "Name 2"}] //Your array of objects

//Use arr.filter() to find the item with the desired name
var selectedObject = objects.filter((item) => {
  return item.name = desiredName
})[0] //Target the first element in the returned array

As far as populating paragraph elements with the selected objects properties, you can just use document.getEleemntById():

document.getElementById("someElement").innerHTML = "Your Text"
<p id="someElement"></p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文