如何将数组值推入输入框

How to push Array values to input box?

本文关键字:输入 数组      更新时间:2023-09-26

嗨,谁能把这里的值推到输入框而不是警报框?请保持列表的唯一性,输出完整的列表

Names:<input id='names'/>
var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var uniqueNames = [];
for(i = 0; i< data.length; i++){    
    if(uniqueNames.indexOf(data[i].website) === -1){
        uniqueNames.push(data[i].website);        
    }        
}
for(i = 0; i< uniqueNames.length; i++){    
   alert(uniqueNames[i]);    
}

请帮忙,因为网上没有人做过!!

这是一个部分的答案,因为它没有附加到文本框,但我设法将它附加到一个列表:虽然我的HTML在这里和JS和JSON是在一个部分(MVC5),不知道这是否是长期的良好做法。但我很高兴我有一半的答案,作为开发者,我们总是会学到越来越多的东西,因为这都是发展!并希望这将帮助某人;-)回答:

HTML: 
Names:<p id="uniqueWebsites"></p>
JS:
var uniqueNamesRes = "";
var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];
var uniqueNames = [];
for(i = 0; i< data.length; i++){    
    if(uniqueNames.indexOf(data[i].website) === -1){
        uniqueNames.push(data[i].website);        
    }        
}
for(i = 0; i< uniqueNames.length; i++){    
  uniqueNamesRes += uniqueNames[i] + "<br>";
}
document.getElementById("uniqueWebsites").innerHTML = uniqueNamesRes;