我有一个这样的对象数组:
const customers = [
{
customer_name: 'Negan Lucille',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: 'negan@sanctuary.com',
customer_city: 'Washington'
},
{
customer_name: 'Daryl Dixon',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: 'daryl.dixon@kickass.com',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick Grimes',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: 'rick@alexandria.com',
customer_city: 'King County'
},
];
,我想将customer_name
分为名字和姓氏,因此名字应保留在customer_name
中,姓氏应放在新创建的属性customer_last_name
中。我也有newKeys
数组,可用来替换customers
对象中的当前键,如下所示:
const newKeys = [
'firstname',
'lastname',
'age',
'weapon',
'email',
'city'
]
let newCustomers = customers.map(obj =>
Object.values(obj).reduce((acc, cur, i) => {
acc[newKeys[i]] = cur;
return acc; }, {}));
新数组应如下所示:
customers = [
{
firstname: 'Negan',
lastname: 'Lucille',
age: 45,
weapon: 'Bat',
email: 'negan@sanctuary.com',
city: 'Washington'
},
{
firstname: 'Daryl',
lastname: 'Dixon',
age: 41,
weapon: 'Crossbow',
email: 'daryl.dixon@kickass.com',
city: 'Atlanta'
},
{
firstname: 'Rick',
lastname: 'Grimes',
age: 45,
weapon: 'Magnum 357',
email: 'rick@alexandria.com',
city: 'King County'
},
]
做到这一点的最佳方法是什么?一个例子将不胜感激!
答案 0 :(得分:1)
假设名字中没有空格,则可以使用string.split
然后您可以使用Array.prototype.shift来获取第一个元素,而其余join一起放回去。
在我的示例中,我将名字更改为具有2个单词的姓氏作为演示
const customers = [
{
customer_name: 'Negan De Lucille',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: 'negan@sanctuary.com',
customer_city: 'Washington'
},
{
customer_name: 'Daryl Dixon',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: 'daryl.dixon@kickass.com',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick Grimes',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: 'rick@alexandria.com',
customer_city: 'King County'
},
];
for (const i in customers) {
const names = customers[i].customer_name.split(" ");
const newCustomer = {
age: customers[i].customer_age
//add in the other properties
}
newCustomer.firstname = names.shift();
newCustomer.lastname = names.join(" ");
customers[i] = newCustomer;
};
console.log(customers);
答案 1 :(得分:1)
使用split()
分解名称,添加新属性,然后删除旧属性。
const customers = [
{
customer_name: 'Negan Lucille',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: 'negan@sanctuary.com',
customer_city: 'Washington'
},
{
customer_name: 'Daryl Dixon',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: 'daryl.dixon@kickass.com',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick Grimes',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: 'rick@alexandria.com',
customer_city: 'King County'
},
];
const refactored = customers.map(customer=>{
var [firstname, lastname] = customer.customer_name.split(/\s/);
return {
firstname, lastname,
age: customer.customer_age,
weapon: customer.customer_weapon,
email: customer.customer_email,
city: customer.customer_city
}
});
console.log(refactored);
答案 2 :(得分:0)
const customers = [
{
customer_name: 'Negan Lucille',
customer_age: 45,
customer_weapon: 'Bat',
customer_email: 'negan@sanctuary.com',
customer_city: 'Washington'
},
{
customer_name: 'Daryl Dixon',
customer_age: 41,
customer_weapon: 'Crossbow',
customer_email: 'daryl.dixon@kickass.com',
customer_city: 'Atlanta'
},
{
customer_name: 'Rick Grimes',
customer_age: 45,
customer_weapon: 'Magnum 357',
customer_email: 'rick@alexandria.com',
customer_city: 'King County'
},
];
for(let i=0;i<customers.length;i++){
let name = customers[i].customer_name.split(' ')
customers[i].customer_name=name[0]
customers[i].customer_last=name[1]
}
console.log(customers)