我有这个json文件,我试图过滤数据,只保留用户:“11111”,但它不工作。
var data= [
{
id: {
server: 'xxxxx',
user: '123456',
_serialized: '64566'
},
name: 'Doe',
isCalled: false,
isReceived: false,
unreadCount: 1,
},
{
id: {
server: 'xxxxx',
user: '123456',
_serialized: '64566'
},
name: 'Doe',
isCalled: false,
isReceived: false,
unreadCount: 1,
},
{
id: {
server: 'xxxxx',
user: '123456',
_serialized: '64566'
},
name: 'Doe',
isCalled: false,
isReceived: false,
unreadCount: 1,
}
]
我试过了
const seun = sex.map(({ id, ...rest }) => rest);
但没有奏效
预期结果:
var data= [
{
user: '123456'
},
{
user: '123456',
},
{
user: '123456',
},
}
]
3条答案
按热度按时间xxhby3vn1#
array.map( x => f(x) )
这将遍历数组的元素,并返回一个新数组,其元素为f(x)
bxgwgixi2#
您可以将Array.prototype.map()与destructuring assignment结合使用
t1rydlwq3#
Array#map
(参见@YosvelQuintero的答案)是最好的方法。在这里我将说明您也可以使用Array#reduce
方法: