Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从一个数组中提取值并将其存储在另一个数组中?_Javascript_Lodash - Fatal编程技术网

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

Javascript 如何从一个数组中提取值并将其存储在另一个数组中?,javascript,lodash,Javascript,Lodash,我从发球中得到了这个阵型: dataFromServer = [ { created_date: "02/10/2019" date_of_birth: "01/01/2000" email: "test@test.com" first_name: "test" last_name: "test" mobile_phone: "999-999-9999" registration_id: "3344" }, { created_date: "02/10/20

我从发球中得到了这个阵型:

dataFromServer = [
 {
  created_date: "02/10/2019"
  date_of_birth: "01/01/2000"
  email: "test@test.com"
  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: "test2@test2.com"
  first_name: "test2"
  last_name: "test2"
  mobile_phone: "111-222-333"
  registration_id: "123"
 }
]
我必须把它放在另一个数组中,以去掉每个属性之间的“\ustrong>”。这就是我正在做的:

      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有更好/更清晰的方法吗?非常感谢

是的,您可以使用
映射
,也许(使用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=[
{
创建日期:“2019年10月2日”,
出生日期:“2000年1月1日”,
电子邮件:“test@test.com",
名字:“测试”,
姓氏:“测试”,
手机:“999-999-9999”,
注册号:“3344”
},
{
创建日期:“2015年10月2日”,
出生日期:“1980年1月1日”,
电子邮件:“test2@test2.com",
名字:“test2”,
姓氏:“test2”,
手机:“111-222-333”,
注册号:“123”
}
];
const newArray=dataFromServer.map(obj=>({
姓氏:obj.last_name,
名字:obj.first_name,
出生日期:出生日期,
注册id:obj.registration\u id,
createdDate:obj.created\u日期,
电子邮件:obj.email,
移动电话:obj.移动电话
}));
log(newArray)
。作为控制台包装器{
最大高度:100%!重要;

}
查看。很抱歉,我无法回答错误重复状态的原因。您可以尝试这个const newArray=dataFromServer.map(obj=>{var conv={};Object.keys(obj).forEach(k=>{var newKey=k.split('''''').map((e,i)=>{if(i==0)返回e;返回e.charAt(0).toUpperCase()+e.slice(1)})。join('';conv[newKey]=obj[e];});返回conv;})@法维兰-这不是假的。请看链接问题的答案。我已经读过了,但它不起作用,因为它是一个类似的问题,而不是重复的问题,比如“==”并不意味着“==”@Farvillain-它是这样的。如果另一个问题的答案回答了这个问题(他们确实回答了),那么“重复”的定义也是如此
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
    }));