使用map(),filter()和concatAll()函数提取Javascript数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用map(),filter()和concatAll()函数提取Javascript数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2709字,纯文字阅读大概需要4分钟。
内容图文
输出几乎是正确的,但是在拼合嵌套的boxart数组时遇到了麻烦.
JavaScript数据:
var movieLists = {
name: "Instant Queue",
videos : [
{
"id": 70111470,
"title": "Die Hard",
"boxarts": [
{width: 150, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard150.jpg"},
{width: 200, height:200, url:"http://cdn-0.nflximg.com/images/2891/DieHard200.jpg"}
],
"url": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": [{ id:432534, time:65876586 }]
},
{
"id": 654356453,
"title": ....,
}];
预期输出:(仅使用functions.map()、. filter()、. concatAll(),返回id,标题,boxart:boxart图片尺寸为150×200的电影的url
// [
// {"id": 675465,"title": "Fracture","boxart":"http://cdn-0...." },
// {"id": 65432445,"title": "The Chamber","boxart":"http://cdn-0...." },
// {"id": 654356453,...}
// ];
电流输出:
// [ //boxart value is an array
// {"id": 675465,"title": "Fracture","boxart":["http://cdn-0...."]},
// {"id": 65432445,"title": "The Chamber","boxart":["http://cdn-0...."]},
// {"id": 654356453,...}
// ];
我的解决方案:
return movieLists.map(function (category) {
return category.videos.map(function (video) {
return {
id: video.id,
title: video.title,
boxart: video.boxarts.filter(function (boxartFeature) {
return boxartFeature.width === 150 && boxartFeature.height === 200;
})
.map(function (boxartProp) {
return boxartProp.url;
})
};
});
}).concatAll(); //Flattens nested array by 1 dimension (please see demo)
我知道我需要应用.concatAll()函数来删除嵌套的boxart数组,但是我似乎找不到位置.
解决方法:
你很亲密我认为这是Observable的练习.因为您正在研究RxJS,所以一定不要使用索引-> [0].处理可观察对象时,没有索引的概念.
了解此问题的技巧是仔细查看concatAll()的功能.它需要一个二维数组,并返回一个展平的数组.
[[1,2],[3,4]]-> [1,2,3,4]
使用当前代码,您正在将boxartProp.url映射到嵌套数组,但其余属性则没有.您现在或多或少拥有的是:[1,2,3,[4]].您想要的是一个均匀嵌套的数组:[[1],[2],[3],[4]].之所以重要,是因为concatAll()希望每个成员都是一个子数组.如果您还没有看过,我建议您看Jafar Husain的this video.他很棒,并在视频中从头实现了concatAll.
只需进行一些细微调整,即可实现您的目标.
return movieLists.map(function(category) {
return category.videos.map(function(video) {
return video.boxarts.filter(function(boxart) {
return boxart.width === 150;
}).map(function(boxart) {
return {id: video.id, title: video.title, boxart: boxart.url};
});
}).concatAll();
}).concatAll();
请注意,在最后一个地图中,我们不仅传递了boxart网址,还传递了视频数据.这为我们提供了运行concatAll()所需的均匀嵌套的数组.
我们必须调用concatAll()两次的原因是,您的视频嵌套在演示的类别中.第一个调用使电影变平,第二个调用使类别变平.
Here is the modified jsbin for your review.
这些是需要进行的一些很棒的练习.祝好运!
内容总结
以上是互联网集市为您收集整理的使用map(),filter()和concatAll()函数提取Javascript数据全部内容,希望文章能够帮你解决使用map(),filter()和concatAll()函数提取Javascript数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。