所以我有一个数组..有很多对象,..但我想修改每个对象里面的东西..但不知道如何去做。 之前
[{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: 2018-01-30T00:00:00.000Z,
end: 2018-02-01T00:00:00.000Z,
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
之后需要
[
{
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: 2018-01-30T00:00:00.000Z,
end: 2018-02-01T00:00:00.000Z,
address: '101 Great American Pkwy, Santa Clara, CA',
}
]
这个数组只有一个对象用于这个例子,但它将是我将以相同的方式修改的许多对象..(编辑) 我认为.map应该被使用..但这就是我所知道的
答案 0 :(得分:2)
arr.map(function(obj) {
return {
permalink: obj.info.permalink,
title: obj.info.title,
location: obj.info.title,
description: obj.info.descriptioin,
start: obj.info.start,
end: obj.info.end,
address: obj.info.address,
}
})
答案 1 :(得分:0)
var myArray = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: '2018-01-30T00:00:00.000Z',
end: '2018-02-01T00:00:00.000Z',
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
console.log(myArray.map(function(arrayItem) {
delete arrayItem.info.layout;
return arrayItem.info
}));
答案 2 :(得分:0)
const myArray = [{
filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info: {
layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: "2018-01-30T00:00:00.000Z",
end: "2018-02-01T00:00:00.000Z",
address: '101 Great American Pkwy, Santa Clara, CA'
}
}];
// Do it manually:
const mapped = myArray.map(x => ({
permalink: x.info.permalink,
title: x.info.title,
location: x.info.location,
description: x.info.description,
start: x.info.start,
end: x.info.end,
address: x.info.address
}));
// Or define a set of keep keys and discard the rest:
const keepKeys = [
"permalink",
"title",
"location",
"description",
"start",
"end",
"address"
];
const otherMapped = myArray.map(x => Object.keys(x.info).reduce((newObj, key) => {
if (keepKeys.includes(key)) newObj[key] = x.info[key];
return newObj;
}, {}));
console.log(mapped);
console.log(otherMapped);
答案 3 :(得分:0)
您可以使用delete
命令删除info.layout
var a = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: '2018-01-30T00:00:00.000Z',
end: '2018-02-01T00:00:00.000Z',
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
a = a.map(o => {
var o = o.info;
delete o['layout'];
return o;
});
console.log(a);

答案 4 :(得分:0)
这里,它是一种通用的方法,它有一个只包含嵌套对象属性的新数组。使用typeof
,检查属性的值是否为对象,并Object.assign
复制对象属性。
var data = [{ filePath: 'stuff/stuff/someplace/',
path: '/events/places/and/things',
info:
{ layout: 'event-single',
permalink: '/events/enigma-2018/',
title: 'Enigma',
location: 'Santa Clara, CA',
description: 'a discription',
start: "2018-01-30T00:00:00.000Z",
end: "2018-02-01T00:00:00.000Z",
address: '101 Great American Pkwy, Santa Clara, CA',
}
}]
var newArray = [];
data.forEach(element => {
var newObj = {};
Object.keys(element).forEach(key => {
if(typeof element[key] === "object"){
Object.assign(newObj, element[key]);
newArray.push(newObj);
}
})
})
console.log(newArray)
答案 5 :(得分:0)
如果/当您对ES6语法感到满意时,可以非常简洁地执行此操作:
const out = arr.map(obj => {
const { layout, ...rest } = obj.info;
return { ...obj, info: rest }
});
对于对象数组 map
,使用object destructuring获取layout
属性,然后使用...rest
(spread syntax)获取所有其他属性。然后只返回仅将rest
中收集的属性分配给嵌套信息对象的对象。