Я хочу изменить массив, используя условие в соответствии с зарплатой - PullRequest
0 голосов
/ 21 апреля 2019

Я хочу изменить массив, полученный из 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>

Ответы [ 2 ]

2 голосов
/ 21 апреля 2019

Просто добавьте атрибут 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 голос
/ 21 апреля 2019

Вам не нужно создавать новый объект, только добавьте свойство для объекта значения в каждом цикле.Другая проблема:

Вы ставите class="modify", но используете в качестве идентификатора #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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...