假设您有一个 json 对象作为响应。例如
var items = [{
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/',
description: 'lorem ipsum'
}, {
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/',
description: 'lorem ipsum'
}]
如何映射对象以便只有一个结果对象,如下所示:
[{
image: 'http://www.lorempixel.com/700/600/',
}, {
image: 'http://www.lorempixel.com/900/1200/',
}]
有没有一种使用 rxjs 运算符的干净方法?或者只是 js 原生?
我尝试使用:
const source = from(items);
const result = source.pipe(
pluck('image')
);
result.subscribe(val => console.log('new values ', val));
但是它不起作用。有没有更干净的方法?
最佳答案
仅使用普通 JS map
覆盖项目并返回仅具有该属性的对象:
var items = [{
title: 'sample 1',
image: 'http://www.lorempixel.com/700/600/',
description: 'lorem ipsum'
},{
title: 'sample 2',
image: 'http://www.lorempixel.com/900/1200/',
description: 'lorem ipsum'
}];
const out = items.map(({image}) => ({ image }));
console.log(out);
关于javascript - 使用 rxjs 或 native javascript 从 json 中提取特定键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53244389/