如何从数组中提取值并将它们存储在另一个数组中?

开发组合

我有从服务中获得的这个数组:

dataFromServer = [
 {
  created_date: "02/10/2019"
  date_of_birth: "01/01/2000"
  email: "[email protected]"
  first_name: "test"
  last_name: "test"
  mobile_phone: "999-999-9999"
  registration_id: "3344"
 },
 {
  created_date: "02/10/2015"
  date_of_birth: "01/01/1980"
  email: "[email protected]"
  first_name: "test2"
  last_name: "test2"
  mobile_phone: "111-222-333"
  registration_id: "123"
 }
]

我必须把它放在另一个数组中以摆脱每个属性之间“_”所以这就是我正在做的:

      const newArray = []
      dataFromServer.foreach(obj => {
      newArray.push(
        {
          lastName: obj.last_name,
          firstName: obj.first_name,
          dateOfBirth: obj.date_of_birth,
          registrationId: obj.registration_id,
          createdDate: obj.created_date,
          email: obj.email,
          mobile_phone: obj.mobile_phone
        });
    });

在纯 javascript(可能使用解构)或使用 Lodash 中是否有更好/清晰的方法?非常感谢!

TJ克劳德

是的,您可以使用map, 或许(使用 ES2015+)和箭头函数:

const newArray = dataFromServer.map(obj => ({
    lastName: obj.last_name,
    firstName: obj.first_name,
    dateOfBirth: obj.date_of_birth,
    registrationId: obj.registration_id,
    createdDate: obj.created_date,
    email: obj.email,
    mobile_phone: obj.mobile_phone
}));

现场示例:

const dataFromServer = [
 {
  created_date: "02/10/2019",
  date_of_birth: "01/01/2000",
  email: "[email protected]",
  first_name: "test",
  last_name: "test",
  mobile_phone: "999-999-9999",
  registration_id: "3344"
 },
 {
  created_date: "02/10/2015",
  date_of_birth: "01/01/1980",
  email: "[email protected]",
  first_name: "test2",
  last_name: "test2",
  mobile_phone: "111-222-333",
  registration_id: "123"
 }
];

const newArray = dataFromServer.map(obj => ({
    lastName: obj.last_name,
    firstName: obj.first_name,
    dateOfBirth: obj.date_of_birth,
    registrationId: obj.registration_id,
    createdDate: obj.created_date,
    email: obj.email,
    mobile_phone: obj.mobile_phone
}));
console.log(newArray);
.as-console-wrapper {
  max-height: 100% !important;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Java 中从一个数组中提取正数并将它们移动到另一个数组?

如何从一个数组中提取数组并将它们转换为对象并将它们放入一个新数组中?

如何找到满足条件的矩阵元素-并将它们存储在另一个数组中

如何从另一个数组中的 JSON 对象中提取值?

PHP从数组中提取值并将其还原到另一个数组中

如何在 JS 中破坏数组并将它们分组为另一个数组中的对象?

如何选择另一个数组中的元素并将它们放在 Python 中的数组外部?

如何在另一个数组中显示数组而不将它们相乘?

Vuejs 从数组中获取数据并将它们放在另一个数组中

JQuery - 我可以用 jquery 移动数组元素吗?并将它们存储在另一个数组中?

即使我将 1 个数组的值存储到另一个数组中并将它们作为一个整体打印,但数组仍然打印最后一个值

如何从数组中拆分多个项目并将它们添加到python中的另一个数组中?

如何从文件中提取特定值并将它们打印到另一个文件中

在gnuplot中声明两个数组,并将它们相对于另一个绘制

如何基于另一个数组中的条件从关联数组中提取?

如何从给定数组中选择奇数和偶数,然后将它们存储在另一个数组中?

函数根据另一个数组的值从一个数组中提取值

找到相同的 id 并将它们存储到一个数组中

如何从一个数组中提取零并将非零部分保存到另一个数组?

使用 for 循环从一个数组中获取随机对象并将它们添加到另一个

从多次点击中获取坐标并将它们返回到一个数组中以在另一个函数中使用

Python:如何从数组中提取值的第一个实例并将其存储在列表中?

如何使用另一个数组按键/值从数组中提取对象

从另一个数组中定义的(图像)数组中提取多个窗口/补丁

如何使用nodejs在mongoose中的另一个数组中提取或删除数组的元素?

循环遍历数组并将值存储在另一个数组中

检查数组中的某些变量是否相等,然后将它们分配给另一个数组

如何使用 stdClass 对象从另一个数组中的数组中获取值?

如何使用数组从另一个数组中获取值?