Javascript 删除对象JS中的节点
我有以下资料:Javascript 删除对象JS中的节点,javascript,arrays,object,attributes,Javascript,Arrays,Object,Attributes,我有以下资料: [{ 'another@exemple.org': { 'age': 42, 'prefered_color': 'blue', 'hate': 'flowers' } }, { 'another@exemple.org': { 'age': 45, 'prefered_color': 'red', 'hate': 'burgers' } }] [{ 'email': 'another@exemple.or
[{
'another@exemple.org': {
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
}
}, {
'another@exemple.org': {
'age': 45,
'prefered_color': 'red',
'hate': 'burgers'
}
}]
[{
'email': 'another@exemple.org',
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
},
{
'email': 'another@exemple.org',
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
}
]
我想将关键变量(电子邮件地址)置于同一级别,以获得以下内容:
[{
'another@exemple.org': {
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
}
}, {
'another@exemple.org': {
'age': 45,
'prefered_color': 'red',
'hate': 'burgers'
}
}]
[{
'email': 'another@exemple.org',
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
},
{
'email': 'another@exemple.org',
'age': 42,
'prefered_color': 'blue',
'hate': 'flowers'
}
]
我尝试了不同的方法(地图、支出),但我实际上想知道什么是获得上述结果的最有效的方法
感谢您的讨论。使用创建一个新数组,并通过获取对象的键来检索电子邮件值:
var newArray = array.map(function(obj) {
var key = Object.keys(obj);
obj[key].email = key[0];
return obj[key];
});
例子:
var数组=[{
'another@exemple.org': {
年龄:42岁,,
“首选颜色”:“蓝色”,
“恨”:“花”
}
}, {
'another@exemple.org': {
年龄:45岁,
“首选颜色”:“红色”,
“恨”:“汉堡”
}
}];
var newArray=array.map(函数(obj){
var key=Object.key(obj);
obj[key]。email=key[0];
返回obj[键];
});
document.querySelector('pre').textContent=JSON.stringify(newArray,null,4)代码>
使用地图
。代码:arr.map(函数(e){var email=Object.keys(e)[0];e[email].email=email;返回e[email];})代码>