我有js文件,我用它来学习javascript。但是,当我运行concatAll时,它说:
Uncaught TypeError: movieLists.map(...).concatAll is not a function
Js档案:
console.log(tryFun());
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
results.push(subArray);
});
return results;
};
function tryFun() {
var movieLists = [
{
name: "New Releases",
videos: [
{
"id": 70111470,
"title": "Die Hard",
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,
"title": "Bad Boys",
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id: 432534, time: 65876586 }]
}
]
},
{
name: "Dramas",
videos: [
{
"id": 65432445,
"title": "The Chamber",
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 675465,
"title": "Fracture",
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id: 432534, time: 65876586 }]
}
]
}
];
return movieLists.map(movieList=>
movieList.videos.map(video=>video.id))
.concatAll();
}
看起来我在这里错过了一些技巧。谢谢你看看这个。应该是Javascript没有在它的原型上添加函数,这个函数是在文件开头定义的吗?
答案 0 :(得分:2)
您在添加tryFun
之前调用了concatAll
。 调用tryFun
之前将方法添加到原型:
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
results.push(subArray);
});
return results;
};
console.log(tryFun());
function tryFun() {
var movieLists = [
{
name: "New Releases",
videos: [
{
"id": 70111470,
"title": "Die Hard",
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,
"title": "Bad Boys",
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id: 432534, time: 65876586 }]
}
]
},
{
name: "Dramas",
videos: [
{
"id": 65432445,
"title": "The Chamber",
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 675465,
"title": "Fracture",
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{ id: 432534, time: 65876586 }]
}
]
}
];
return movieLists.map(movieList=>
movieList.videos.map(video=>video.id))
.concatAll();
}

但改变内置对象是非常不好的做法 - 最好调用一个独立的函数,或使用你自己的包装器。
答案 1 :(得分:1)
哦,明白了!它的程序流程,concatAll从未被添加。只需移动呼叫有助于:
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
results.push(subArray);
});
return results;
};
console.log(tryFun());
答案 2 :(得分:1)
通常与您尝试实现的内容相关,您可以在没有.concatAll()
函数的情况下轻松过关,因为.concat()
是可变参数并会自动展平数组。
因此,使用“传播语法”,您可以执行此操作:
return [].concat(...movieLists.map(
movieList => movieList.videos.map(video => video.id))
);
这是一个现场演示:
console.log(tryFun());
function tryFun() {
var movieLists = [{
name: "New Releases",
videos: [{
"id": 70111470,
"title": "Die Hard",
"boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 654356453,
"title": "Bad Boys",
"boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{
id: 432534,
time: 65876586
}]
}
]
},
{
name: "Dramas",
videos: [{
"id": 65432445,
"title": "The Chamber",
"boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 4.0,
"bookmark": []
},
{
"id": 675465,
"title": "Fracture",
"boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
"uri": "http://api.netflix.com/catalog/titles/movies/70111470",
"rating": 5.0,
"bookmark": [{
id: 432534,
time: 65876586
}]
}
]
}
];
return [].concat(...movieLists.map(
movieList => movieList.videos.map(video => video.id))
);
}
或者您可以使用.reduce()
并将.concat()
放入回调中。
return movieLists.reduce(
(res, movieList) => res.concat(...movieList.videos.map(video => video.id))
, []);
答案 3 :(得分:0)
更简单的尝试下面的代码。
Array.prototype.concatAll = function() {
var results = [];
this.forEach(function(subArray) {
results.push.apply(results, subArray);
});
return results;
};
console.log(tryFun());