尝试从一组对象中获取特定数据,这些数据由于多种原因而无法在服务器上托管。数据作为变量存储在文档中。这是我迄今为止一直在努力取得的成就:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var users = [{"username":"nphillips7m","first_name":"Nicole","last_name":"Phillips","email":"nphillips7m@ebay.co.uk","gender":"Female","sexuality":"Networked static concept","language":"Gagauz"},
{"username":"esimpson7n","first_name":"Elizabeth","last_name":"Simpson","gender":"Female","sexuality":"Future-proofed solution-oriented definition","language":"Malay"},
{"username":"llawrence7o","first_name":"Lillian","last_name":"Lawrence","email":"llawrence7o@technorati.com","gender":"Female","sexuality":"Re-contextualized demand-driven middleware","language":"Tetum"}]
var simpson = users.find("last_name" + "Simpson")
document.getElementById("return").innerHTML = function myfunction() {
simpson;
}
</script>
</head>
<body>
<div id="return"></div>
</body>
目前,我一直试图从用户中提取一些/任何数据&#39;数组,但是我希望用户能够搜索一个单词以及整个行&#39; /&#39;行&#39;与用户&#39;中的单词/单词相关的数据显示为结果。我应该用什么方法来实现这个目标?
答案 0 :(得分:2)
1。您需要在'0x7fff'
方法中传递callback
函数。 find
方法搜索数组中的元素,如果找到则返回该元素。否则返回find
。搜索条件由回调函数定义。像
undefined
2。您可能不需要将您的innerHTML作为一个函数,而是指向像UserName Found这样有意义的信息更合适。
var simpson = users.find(currentValue => currentValue.last_name === "Simpson");
请尝试以下代码。
document.getElementById("return").innerHTML = simpson.username;
&#13;
var users = [{"username":"nphillips7m","first_name":"Nicole","last_name":"Phillips","email":"nphillips7m@ebay.co.uk","gender":"Female","sexuality":"Networked static concept","language":"Gagauz"},
{"username":"esimpson7n","first_name":"Elizabeth","last_name":"Simpson","gender":"Female","sexuality":"Future-proofed solution-oriented definition","language":"Malay"},
{"username":"llawrence7o","first_name":"Lillian","last_name":"Lawrence","email":"llawrence7o@technorati.com","gender":"Female","sexuality":"Re-contextualized demand-driven middleware","language":"Tetum"}]
var simpson = users.find(currentValue => currentValue.last_name === "Simpson");
document.getElementById("return").innerHTML = simpson.username;
&#13;
答案 1 :(得分:2)
您的代码中存在一些错误。首先,SELECT *
FROM customers
JOIN orders
USING custID
GROUP BY Name
ORDER BY custID ASC;
函数接受find
函数作为参数。
callback
如果将函数传递给 var simpson = users.find(a=>a.last_name=="Simpson");
,则必须调用它,如下所示:
innerHTML
并且函数必须返回document.getElementById("return").innerHTML = (function myFunction(){
return JSON.stringify(simpson);
})();
才能设置value
元素的HTML内容(内部HTML)。
result
&#13;
var users = [{"username":"nphillips7m","first_name":"Nicole","last_name":"Phillips","email":"nphillips7m@ebay.co.uk","gender":"Female","sexuality":"Networked static concept","language":"Gagauz"},
{"username":"esimpson7n","first_name":"Elizabeth","last_name":"Simpson","gender":"Female","sexuality":"Future-proofed solution-oriented definition","language":"Malay"},
{"username":"llawrence7o","first_name":"Lillian","last_name":"Lawrence","email":"llawrence7o@technorati.com","gender":"Female","sexuality":"Re-contextualized demand-driven middleware","language":"Tetum"}]
var simpson = users.find(callback);
function callback(item){
return item.last_name=="Simpson";
}
document.getElementById("return").innerHTML = (function myFunction(){
return JSON.stringify(simpson);
})();
&#13;