如何使用Javascript迭代json对象并形成具有连接值的新对象?

时间:2017-11-17 12:10:05

标签: javascript json angular api

我一直在研究api,它在JSON对象中提供数据,而我根本无法控制它。我需要在选择框中提取数据。我已设法填充它,但它只允许单值绑定。即firstName或lastName或middleName,而不是两者。这是我当前的JSON对象:

.employeesList = [
  {
    id: 1,
    firstName: 'firstName',
    middleName: 'middleName',
    lastName: 'lastName',
  },
  {
    id: 2,
    firstName: 'firstName2',
    middleName: 'middleName2',
    lastName: 'lastName2',
  },
  {
    id: 3,
    firstName: 'firstName3',
    middleName: 'middleName3',
    lastName: 'lastName3',
  },
  {
    id: 4,
    firstName: 'firstName4',
    middleName: 'middleName4',
    lastName: 'lastName4',
  },
  {
    id: 5,
    firstName: 'firstName5',
    middleName: 'middleName5',
    lastName: 'lastName5',
  }
]

我想在每个数据中合并firstName,middleName和lastName,并形成一个只有id和fullNames的新JSON对象,如下所示:

.employeesList = [
  {
    id: 1,
    fullName: 'firstName MiddleName LastName'
  },
  {
    id: 2,
    fullName: 'firstName2 MiddleName2 LastName2'
  },
  {
    id: 3,
    fullName: 'firstName3 MiddleName3 LastName3'
  },
  {
    id: 4,
    fullName: 'firstName4 MiddleName4 LastName4'
  },
  {
    id: 5,
    fullName: 'firstName5 MiddleName5 LastName5'
  }
]

如何使用Javascript实现这一目标?

4 个答案:

答案 0 :(得分:6)

使用 array.prototype.map

var employeesList = [
  {
    id: 1,
    firstName: 'firstName',
    middleName: 'middleName',
    lastName: 'lastName',
  },
  {
    id: 2,
    firstName: 'firstName2',
    middleName: 'middleName2',
    lastName: 'lastName2',
  },
  {
    id: 3,
    firstName: 'firstName3',
    middleName: 'middleName3',
    lastName: 'lastName3',
  },
  {
    id: 4,
    firstName: 'firstName4',
    middleName: 'middleName4',
    lastName: 'lastName4',
  },
  {
    id: 5,
    firstName: 'firstName5',
    middleName: 'middleName5',
    lastName: 'lastName5',
  }
];

var newEmployeesList = employeesList.map(e => ({id: e.id, fullName: `${e.firstName} ${e.middleName} ${e.lastName}`}));
console.log(newEmployeesList);

答案 1 :(得分:0)

var employeesList = [ {
    id: 1,
    firstName: 'firstName',
    middleName: 'middleName',
    lastName: 'lastName',
  },
  {
    id: 2,
    firstName: 'firstName2',
    middleName: 'middleName2',
    lastName: 'lastName2',
  },
  {
    id: 3,
    firstName: 'firstName3',
    middleName: 'middleName3',
    lastName: 'lastName3',
  },
  {
    id: 4,
    firstName: 'firstName4',
    middleName: 'middleName4',
    lastName: 'lastName4',
  },
  {
    id: 5,
    firstName: 'firstName5',
    middleName: 'middleName5',
    lastName: 'lastName5',
  }
]


employeesList.map((obj) => {  
  obj["fullName"] = obj["firstName"] + " " + obj["middleName"] + " "  +obj["lastName"];
  delete obj["firstName"];
  delete obj["middleName"];
  delete obj["lastName"];
  console.log(obj)
});

答案 2 :(得分:0)

newList = employeesList.map((empl) => {
    return {
        id: empl.id, fullName: empl.firstName + ' ' + empl.middleName + ' ' + empl.lastName}
    }
)

答案 3 :(得分:0)

我使用此代码并为我工作正常。 (需要JQuery)。

var newEmployeesList = [];
$.each(employeesList, function(key, item) {

    var f_name = item.firstName;

    var m_name = item.middleName;

    var l_name = item.lastName;

    var obj = new Object();
    obj.id = key;
    obj.fullName = f_name + " " + m_name + " " + l_name;

    newEmployeesList.push(obj);

});