我希望修改从api获取的数组并添加新密钥:Salary
。
条件是:
年龄小于23岁:薪水为23,000
年龄大于23岁且小于27岁:薪水为27,000
年龄超过27岁:薪水为500,000
这是数组:
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(){
$.each( employee, function( key, value ) {
if(value["Age"] <= 23) {
var obj = {};
obj[Salary] = 23000;
employee.push(obj);
}
else if(value["Age"] > 23 && value["Age"] < 27) {
var obj2 = {};
obj2[Salary] = 27000;
employee.push(obj2);
}
else if(value["Age"] > 27) {
var obj3 = {};
obj3[Salary] = 500000;
employee.push(obj3);
}
});
$.each(employee, function(index, value){
$("#modfy").append(index + ": " + value + '<br>');
});
});
我的HTML:
<div class="modfy"></div>
答案 0 :(得分:2)
只需将Salary属性添加到对象,别忘了使用id
而不是class
。
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() {
$.each(employee, function(key, value) {
if (value["Age"] <= 23) {
value["Salary"] = 23000;
} else if (value["Age"] > 23 && value["Age"] < 27) {
value["Salary"] = 27000;
} else if (value["Age"] > 27) {
value["Salary"] = 500000;
}
});
$.each(employee, function(index, value) {
$("#modfy").append(value["firstName"] + " " + value["lastName"] + " (Age: " + value["Age"] + "): " + value["Salary"] + "<br />");
});
});
console.log(employee);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="modfy"></div>
答案 1 :(得分:1)
您不需要创建新对象,只需在每个循环中为值对象添加属性。 其他问题:
您放置了class="modify"
,但用作ID #modify。
而且每个循环都需要使用值作为对象。
$(".modfy").append(++index + ". " + value.firstName + ": " + value.lastName + ": " + value.Age + ": " + value.Company + ": " + value.Role + ": " + value.Department + ": " + value.Salary + '<br>');
如果要根据需要打印每个对象,则只需要$(".modfy").append(JSON.stringify(value));
或通过其他方式显示为数组$(".modfy").append(JSON.stringify(employee));
$(document).ready(function(){
$.each( employee, function( key, value ) {
if(value["Age"] <= 23) {
//var obj = {};
value["Salary"] = 23000;
//employee.push(obj);
}
else if(value["Age"] > 23 && value["Age"] < 27) {
//var obj2 = {};
value["Salary"] = 27000;
//employee.push(obj2);
}
else if(value["Age"] > 27) {
//var obj3 = {};
value["Salary"] = 500000;
//employee.push(obj3);
}
});
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(){
$.each( employee, function( key, value ) {
if(value["Age"] <= 23) {
//var obj = {};
value["Salary"] = 23000;
//employee.push(obj);
}
else if(value["Age"] > 23 && value["Age"] < 27) {
//var obj2 = {};
value["Salary"] = 27000;
//employee.push(obj2);
}
else if(value["Age"] > 27) {
//var obj3 = {};
value["Salary"] = 500000;
//employee.push(obj3);
}
});
//console.log(employee)
// $.each(employee, function(index, value){
// $(".modfy").append(++index + ". " + value.firstName + ": " + value.lastName + ": " + value.Age + ": " + value.Company + ": " + value.Role + ": " + value.Department + ": " + value.Salary + '<br>');
//$(".modfy").append(JSON.stringify(value));
//});
$(".modfy").append(JSON.stringify(employee));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modfy"></div>