Home > Mobile >  Dynamically rendering objects from API in Javascript(object/API/dynamic rendering)
Dynamically rendering objects from API in Javascript(object/API/dynamic rendering)

Time:07-07

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.

CodePudding user response:

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>

  • Related