简体   繁体   English

结合名字和姓氏并返回 object

[英]combine first name and last name and return object

I was trying to combine first name last name in an object and return same object again我试图在 object 中组合名字姓氏并再次返回相同的 object

I have tried multiple ways with map and foreach but doesn't seem to work我用 map 和 foreach 尝试了多种方法,但似乎不起作用

[ { "id": 1321, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "g", "lastName": "prashanth", "zipCode": "39493", "NPI": "990343" }, { "id": 1322, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "prasilla", "lastName": "shaik", "zipCode": "39493", "NPI": "990343" } ] [ {“id”:1321,“createdAt”:“2022-08-03T12:36:20.096Z”,“UpdatedAt”:“2022-08-03T12:36:20.096Z”,“firstName”:“g”, “姓氏”:“prashanth”,“邮政编码”:“39493”,“NPI”:“990343”},{“id”:1322,“createdAt”:“2022-08-03T12:36:20.096Z”,“ UpdatedAt”:“2022-08-03T12:36:20.096Z”,“firstName”:“prasilla”,“lastName”:“shaik”,“zipCode”:“39493”,“NPI”:“990343”}]

what i have tried:我试过的:

let final_array: any=[]
data.forEach(function (value: any, a:any) {
let obj: any={}
    console.log(a)
     final_array.push( obj[keys_array[a]] = data[a]["firstName"]+" " +data[a]["lastName"], obj[keys_array[a]] =data[a]["zipCode"], obj[keys_array[a]]=data[a]["NPI"])
})

});

required format:所需格式:

[ { "name": "g prashanth", "zipCode": "39493", "NPI": "990343" }, { "Name": "prasilla shaik", "zipCode": "39493", "NPI": "990343" } ] [ {“名称”:“g prashanth”,“邮政编码”:“39493”,“NPI”:“990343”},{“名称”:“prasilla shaik”,“邮政编码”:“39493”,“NPI”: “990343”}]

    var data = [ { "id": 1321, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "g", "lastName": "prashanth", "zipCode": "39493", "NPI": "990343" }, { "id": 1322, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "prasilla", "lastName": "shaik", "zipCode": "39493", "NPI": "990343" } ]

    // console.log( final_array)


let data_1= data.map((object,i) => {
  return {...object, fullName: data[i]["firstName"]+' '+data[i]["lastName"]} ;

});

You need to pass index value i to map, and probably also include the fullname construction inside the map.您需要将索引值 i 传递给 map,并且可能还包括 map 内的全名结构。 Also the outer loop is not required也不需要外循环

const newArrayOfObj = array.map((obj) => {
 let temp = { ...obj, name: `${obj.firstName} ${obj.lastName}` };
 let res = {};

 for (const [key, value] of Object.entries(temp)) {
  if (key === "firstName" || key === "lastName") continue;
  else res[key] = value;
 }

 return res;
});

if you want full result with added property full name you try @Anuja script.如果您想要添加属性全名的完整结果,请尝试 @Anuja 脚本。

this script will work as the ouput you mentioned.该脚本将作为您提到的输出工作。

 const dataAsJson = `[ { "id": 1321, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "g", "lastName": "prashanth", "zipCode": "39493", "NPI": "990343" }, { "id": 1322, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt": "2022-08-03T12:36:20.096Z", "firstName": "prasilla", "lastName": "shaik", "zipCode": "39493", "NPI": "990343" } ]`; const data = JSON.parse(dataAsJson); const changedData = data.map(item => ({ name: `${item.firstName} ${item.lastName}`, zipCode: item.zipCode, NPI: item.NPI })); console.log(changedData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM