Javascript 在对象中的多个数组上使用贴图和过滤器
使用Wikipedia的API,我得到了一个如下所示的对象:Javascript 在对象中的多个数组上使用贴图和过滤器,javascript,dictionary,functional-programming,Javascript,Dictionary,Functional Programming,使用Wikipedia的API,我得到了一个如下所示的对象: obj = {["string", Array[10], Array[10], Array[10]]} 第一个数组包含标题,第二个是描述,第三个是链接 有些描述可能是空的 示例- 我希望将所有三个阵列合并为一个阵列,如下所示: [{title: obj[1][0], desc: obj[2][0], link: obj[3][0]}, ... , {title: array[1][9], desc: obj[2][0], link:
obj = {["string", Array[10], Array[10], Array[10]]}
第一个数组包含标题,第二个是描述,第三个是链接
有些描述可能是空的
示例-
我希望将所有三个阵列合并为一个阵列,如下所示:
[{title: obj[1][0], desc: obj[2][0], link: obj[3][0]}, ... , {title: array[1][9], desc: obj[2][0], link: obj[3][0]}]
但仅当描述不为空时才合并-obj[2][i]!==
我成功地使用for循环完成了该操作,如果:
var arr = [];
for (var i = 0; i < data[1].length; i++) {
if (data[2][i] !== '') {
arr.push({title: data[1][i], desc:data[2][i], link: data[3][i]});
}
}
看起来你的生活中需要一些细节/下划线 他们就是这样做的图书馆。取物体/阵列,切片并切成小方块,直到它们成为你想要的。非常有用 或者你可以用简单的语言来表达
var crazyArray = ['title here', ['one', 'two', 'three'], ['four', 'five'], '', ['six']]
var reduced = crazyArray.reduce(function( last, cur ){
if(cur) return last.concat(cur);
return last;
}, []);
console.log(reduced) // [ 'title here', 'one', 'two', 'three', 'four', 'five', 'six' ]
看起来你的生活中需要一些细节/下划线 他们就是这样做的图书馆。取物体/阵列,切片并切成小方块,直到它们成为你想要的。非常有用 或者你可以用简单的语言来表达
var crazyArray = ['title here', ['one', 'two', 'three'], ['four', 'five'], '', ['six']]
var reduced = crazyArray.reduce(function( last, cur ){
if(cur) return last.concat(cur);
return last;
}, []);
console.log(reduced) // [ 'title here', 'one', 'two', 'three', 'four', 'five', 'six' ]
如果你不想的话,没有必要让另一个图书馆参与进来。映射和过滤器在这里工作,但我认为它们增加了很多长度。我喜欢forEach 请记住,最多会向回调函数传递三个参数。在这种情况下,你只需要两个就可以了
data[2].forEach(function(elem, index) {
elem && arr.push({title: data[1][index], desc: elem, link: data[3][index]});
});
使用map和filter的解决方案可能如下所示
data[1].map(function(elem, index) {
return ({
title: data[1][index],
description: data[2][index],
link: data[3][index]
});
}).filter(function(elem) {
return elem.description;
});
如果你不想的话,没有必要让另一个图书馆参与进来。映射和过滤器在这里工作,但我认为它们增加了很多长度。我喜欢forEach 请记住,最多会向回调函数传递三个参数。在这种情况下,你只需要两个就可以了
data[2].forEach(function(elem, index) {
elem && arr.push({title: data[1][index], desc: elem, link: data[3][index]});
});
使用map和filter的解决方案可能如下所示
data[1].map(function(elem, index) {
return ({
title: data[1][index],
description: data[2][index],
link: data[3][index]
});
}).filter(function(elem) {
return elem.description;
});
您将要学习一个很棒的新高阶函数,zip!老实说,我很少遇到它有用的情况,但你的例子正好适合它
/*
Example in ES6. In order to run, first install node and then go
npm install -g babel
npm install underscore
babel thisfile.js | node
*/
let _ = require('underscore')
let resp = [
"Football",
['Football', 'Soccer'],
['FootballDescription', 'SoccerDescription'],
['http://football.com', 'http://soccer.com']
]
// 1. Throw away the first string, e.g. "Football"
resp.shift()
// 2. Use zip to make an array of intersected arrays, i.e
// [
// ['Football', 'FootballDescription', 'http://football.com'],
// ['Soccer', 'SoccerDescription', 'http://soccer.com']
// ]
// and then use map to make each array into a nicely named object
let objects = _.zip(
resp[0],
resp[1],
resp[2]
).map(x => ({
name: x[0],
desc: x[1],
link: x[2]
}))
console.log(objects)
/*
Output:
[ { name: 'Football',
description: 'FootballDescription',
link: 'http://football.com' },
{ name: 'Soccer',
description: 'SoccerDescription',
link: 'http://soccer.com' } ]
*/
您将要学习一个很棒的新高阶函数,zip!老实说,我很少遇到它有用的情况,但你的例子正好适合它
/*
Example in ES6. In order to run, first install node and then go
npm install -g babel
npm install underscore
babel thisfile.js | node
*/
let _ = require('underscore')
let resp = [
"Football",
['Football', 'Soccer'],
['FootballDescription', 'SoccerDescription'],
['http://football.com', 'http://soccer.com']
]
// 1. Throw away the first string, e.g. "Football"
resp.shift()
// 2. Use zip to make an array of intersected arrays, i.e
// [
// ['Football', 'FootballDescription', 'http://football.com'],
// ['Soccer', 'SoccerDescription', 'http://soccer.com']
// ]
// and then use map to make each array into a nicely named object
let objects = _.zip(
resp[0],
resp[1],
resp[2]
).map(x => ({
name: x[0],
desc: x[1],
link: x[2]
}))
console.log(objects)
/*
Output:
[ { name: 'Football',
description: 'FootballDescription',
link: 'http://football.com' },
{ name: 'Soccer',
description: 'SoccerDescription',
link: 'http://soccer.com' } ]
*/
看来我会坚持用forEach。在这种情况下,映射和过滤器只会增加额外的复杂性。谢谢。看来我还是要用forEach。在这种情况下,映射和过滤器只会增加额外的复杂性。谢谢。我来看看下划线。js。我来看看下划线。js。太棒了!我把你的答案和-,的帮助结合起来,得到了一个我认为很好看的解决方案:看看我编辑的问题!我把你的答案和-,的帮助结合起来,得到了一个我认为很好看的解决方案:看我编辑的问题