我想比较关联数组中的值,并想打印通过HTML选择的人的头部
我的数组看起来像
var employee=[{"firstName":"Zahir","lastName":"Alam","Age":25,"Company":"Switchme","Role":"Developer","Department":"Tech","Head":{"Id":3,"Name":"Sourasis Roy"}},{"firstName":"Amith","lastName":"Manniken","Age":25,"Company":"Switchme","Role":"Developer","Department":"Tech","Head":{"Id":3,"Name":"Sourasis Roy"}},{"firstName":"Sourasis","lastName":"Roy","Age":28,"Company":"Switchme","Role":"CTO"},{"firstName":"Aditya","lastName":"Mishra","Age":29,"Company":"Switchme","Department":"Tech","Role":"CEO"},{"firstName":"Priti","lastName":"Lata","Age":24,"Company":"Switchme","Role":"HR"},{"firstName":"Sumita","lastName":"Nath","Age":24,"Company":"Switchme","Role":"HLA Head","Department":"Crm"},{"firstName":"Tarini","lastName":"Khanna","Age":22,"Company":"Switchme","Role":"Content Writer"},{"firstName":"Abhisek","lastName":"Soni","Age":23,"Company":"Switchme","Role":"HLA","Department":"Crm","Head":{"Id":5,"Name":"Sumita Nath"}},{"firstName":"Ankit","lastName":"Pump","Age":23,"Company":"Switchme","Role":"HLA","Department":"Crm","Head":{"Id":5,"Name":"Sumita Nath"}},{"firstName":"Pogo","lastName":"Laal","Age":23,"Company":"Switchme","Role":"Designer"},{"firstName":"Sabina","lastName":"Sekh","Age":28,"Company":"Switchme","Role":"HLA Head","Department":"Crm"},{"firstName":"Sanjay","lastName":"Poudal","Age":24,"Company":"Switchme","Role":"HLA Head","Department":"Crm","Head":{"Id":10,"Name":"Sabina Sekh"}}];
$(document).ready(function(){
$("select.employee").change(function(){
var selectedEmployee = $(this).children("option:selected").val();
$.each( employee, function( key, value ) {
if(employee["firstName"]==selectedEmployee) {
$("div.print").html("The head of"+selectedEmployee+"is"+employee["Head"]["Name"]);
}
});
});
});
<h3>1. Who is Head of </h3>
<select class="employee">
<option value="0">Select</option>
<option name="Amith" id="Amith" value="Amith" >Amith</option>
<option name="Sourasis" id="Sourasis" value="Sourasis">Sourasis</option>
<option name="Abhisek" id="Abhishek" value="Abhisek">Abhisek</option>
<option name="Sanjay" id="Sanjay" value="Sanjay">Sanjay</option>
</select>
答案 0 :(得分:0)
这是您要的吗?
var employee = [{
"firstName": "Zahir",
"lastName": "Alam",
"Age": 25,
"Company": "Switchme",
"Role": "Developer",
"Department": "Tech",
"Head": {
"Id": 3,
"Name": "Sourasis Roy"
}
}, {
"firstName": "Amith",
"lastName": "Manniken",
"Age": 25,
"Company": "Switchme",
"Role": "Developer",
"Department": "Tech",
"Head": {
"Id": 3,
"Name": "Sourasis Roy"
}
}, {
"firstName": "Sourasis",
"lastName": "Roy",
"Age": 28,
"Company": "Switchme",
"Role": "CTO"
}, {
"firstName": "Aditya",
"lastName": "Mishra",
"Age": 29,
"Company": "Switchme",
"Department": "Tech",
"Role": "CEO"
}, {
"firstName": "Priti",
"lastName": "Lata",
"Age": 24,
"Company": "Switchme",
"Role": "HR"
}, {
"firstName": "Sumita",
"lastName": "Nath",
"Age": 24,
"Company": "Switchme",
"Role": "HLA Head",
"Department": "Crm"
}, {
"firstName": "Tarini",
"lastName": "Khanna",
"Age": 22,
"Company": "Switchme",
"Role": "Content Writer"
}, {
"firstName": "Abhisek",
"lastName": "Soni",
"Age": 23,
"Company": "Switchme",
"Role": "HLA",
"Department": "Crm",
"Head": {
"Id": 5,
"Name": "Sumita Nath"
}
}, {
"firstName": "Ankit",
"lastName": "Pump",
"Age": 23,
"Company": "Switchme",
"Role": "HLA",
"Department": "Crm",
"Head": {
"Id": 5,
"Name": "Sumita Nath"
}
}, {
"firstName": "Pogo",
"lastName": "Laal",
"Age": 23,
"Company": "Switchme",
"Role": "Designer"
}, {
"firstName": "Sabina",
"lastName": "Sekh",
"Age": 28,
"Company": "Switchme",
"Role": "HLA Head",
"Department": "Crm"
}, {
"firstName": "Sanjay",
"lastName": "Poudal",
"Age": 24,
"Company": "Switchme",
"Role": "HLA Head",
"Department": "Crm",
"Head": {
"Id": 10,
"Name": "Sabina Sekh"
}
}];
$(document).ready(function() {
$("select.employee").change(function() {
var selectedEmployee = this.value
$.each(employee, function(i, emp) {
if (emp.firstName == selectedEmployee) {
if (emp.Head) {
$("div.print").html("The head of " + selectedEmployee + " is " + emp.Head.Name);
} else {
$("div.print").html(selectedEmployee + " has no head! ");
}
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>1. Who is Head of </h3>
<select class="employee">
<option value="0">Select</option>
<option name="Amith" id="Amith" value="Amith">Amith</option>
<option name="Sourasis" id="Sourasis" value="Sourasis">Sourasis</option>
<option name="Abhisek" id="Abhishek" value="Abhisek">Abhisek</option>
<option name="Sanjay" id="Sanjay" value="Sanjay">Sanjay</option>
</select>
<hr>
<div class="print"></div>
答案 1 :(得分:0)
一些问题:
value
而不是employee
的属性return false
退出循环。children("option:selected")
。您可以仅在.val()
jQuery元素上进行select
您应该这样做:
var employee=[{"firstName":"Zahir","lastName":"Alam","Age":25,"Company":"Switchme","Role":"Developer","Department":"Tech","Head":{"Id":3,"Name":"Sourasis Roy"}},{"firstName":"Amith","lastName":"Manniken","Age":25,"Company":"Switchme","Role":"Developer","Department":"Tech","Head":{"Id":3,"Name":"Sourasis Roy"}},{"firstName":"Sourasis","lastName":"Roy","Age":28,"Company":"Switchme","Role":"CTO"},{"firstName":"Aditya","lastName":"Mishra","Age":29,"Company":"Switchme","Department":"Tech","Role":"CEO"},{"firstName":"Priti","lastName":"Lata","Age":24,"Company":"Switchme","Role":"HR"},{"firstName":"Sumita","lastName":"Nath","Age":24,"Company":"Switchme","Role":"HLA Head","Department":"Crm"},{"firstName":"Tarini","lastName":"Khanna","Age":22,"Company":"Switchme","Role":"Content Writer"},{"firstName":"Abhisek","lastName":"Soni","Age":23,"Company":"Switchme","Role":"HLA","Department":"Crm","Head":{"Id":5,"Name":"Sumita Nath"}},{"firstName":"Ankit","lastName":"Pump","Age":23,"Company":"Switchme","Role":"HLA","Department":"Crm","Head":{"Id":5,"Name":"Sumita Nath"}},{"firstName":"Pogo","lastName":"Laal","Age":23,"Company":"Switchme","Role":"Designer"},{"firstName":"Sabina","lastName":"Sekh","Age":28,"Company":"Switchme","Role":"HLA Head","Department":"Crm"},{"firstName":"Sanjay","lastName":"Poudal","Age":24,"Company":"Switchme","Role":"HLA Head","Department":"Crm","Head":{"Id":10,"Name":"Sabina Sekh"}}];
$(document).ready(function(){
$("select.employee").change(function(){
var selectedEmployee = $(this).val();
$.each( employee, function( key, value ) {
if(value["firstName"] == selectedEmployee) {
$("div.print").text("The head of " +selectedEmployee+" is "+ (value["Head"] ? value["Head"]["Name"] : "unknown"));
return false; // exit;
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>1. Who is Head of </h3>
<select class="employee">
<option value="0">Select</option>
<option name="Amith" id="Amith" value="Amith" >Amith</option>
<option name="Sourasis" id="Sourasis" value="Sourasis">Sourasis</option>
<option name="Abhisek" id="Abhishek" value="Abhisek">Abhisek</option>
<option name="Sanjay" id="Sanjay" value="Sanjay">Sanjay</option>
</select>
<br>
<div class="print"></div>