Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a form that has eight input types

<input type="text" name="name0" class="form-control" id="name0">
<input type="text" name="address0" class="form-control" id="address0">
<hr>
<input type="text" name="name1" class="form-control" id="name1">
<input type="text" name="address1" class="form-control" id="address1">
<hr>
<input type="text" name="name2" class="form-control" id="name2">
<input type="text" name="address2" class="form-control" id="address2">
<hr>
<input type="text" name="name3" class="form-control" id="name3">
<input type="text" name="address3" class="form-control" id="address3">


I am now trying to prefill the values of the form using the array

info = [{name:"Tom", address:"abc street"},{name:"Lois", address:"abc street"},{name:"Cale", address:"abc street"},{name:"Bob", address:"abc street"}];


in javascript. The order that appears on the array is the order the input type get populated.

What I have tried:

I started with a for loop

for( let k of info)

{
	
}


In the past I have been able to filter through the array when all (key:values) were different but I am having trouble distinguishing and filtering it out for this case. Open to any help if possible.
Posted
Updated 21-Dec-17 4:44am

1 solution

First, you should realize that all key values are still different.
So think . . . . how do you access each of the smaller arrays in your larger array?

An index (0, 1, ...) and the key name.
Your input boxes have name and an index on them.
You can loop through the indexes and

Using the DOM, update using the id created by the (name + index) by appending the index to the end of the key.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900