数字列表到数字列表
List of numbers to list of numbers
我有一个数字列表,格式如下:
[[1,2],[3,4],[5,1]]
我期待得到下一个输出:
[1,2,3,4,5]
这是我当前方法的一个示例:
<!DOCTYPE html>
<html>
<head>
<title>Expected - [1,2,3,4,5,6,7,8,9,10]</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
// Expected - [1,2,3,4,5,6,7,8,9,10];
var array_of_arrays = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
console.log(array_of_arrays);
// Section 1 - code does what I expect. I would like something like Section 2.
var array_of_values = [];
array_of_arrays.map((x) => {
x.map((y) => {
if (array_of_values.indexOf(y) == -1)
array_of_values.push(y)
})
});
console.log(array_of_values);
// Section 2 - This code does not do what I expect.
var resp = array_of_arrays.map(x => x.map(y => y));
console.log(resp);
});
</script>
</body>
</html>
您可以使用 flatMap
来做到这一点。它根据提供的回调函数展平(取消嵌套)数组。使用过滤器过滤掉重复项并使用 sort()
对它们进行排序
var a=[[1,2],[3,4],[5,1]].flatMap((x)=>x);
var arr=[];
console.log(a.filter((e)=>arr.indexOf(e)==-1?arr.push(e):false).sort());
您可以使用 Array.prototype.reduce() and using Set
let arr = [[1,2],[3,4],[5,1]]
//flatting array
arr = arr.reduce((ac,item) => ([...ac,...item]),[])
//remove dupliactes
arr = [...new Set(arr)];
console.log(arr);
一种方法可以使用 reduce() with a set 作为 accumulator
:
let input = [[1,2],[3,4],[5,1]];
let res = input.reduce((acc, a) =>
{
a.forEach(x => acc.add(x));
return acc;
}, new Set());
console.log(Array.from(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
另一种方法是使用新的 flat() 方法:
let input = [[1,2],[3,4],[5,1]];
let res = new Set(input.flat());
console.log(Array.from(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
您可以创建一个 distinct
过滤器并使用 flatMap
const distinct = (value, index, self) => {
return self.indexOf(value) == index;
}
console.log(
[[1,2],[3,4],[5,6],[7,8],[9,10],[1,2],[3,4],[5,6],[7,8],[9,10]]
.flatMap(x => x)
.filter(distinct)
);
这是你想要的吗
阅读评论
var array_of_arrays = [[1, 2],[3, 4], [5, 6], [7, 8], [9, 10], [1, 2],[3, 4], [5, 6], [7, 8], [9, 10]];
// flat the array and then select distinct after that sort the array out
var arr = array_of_arrays.flatMap((x)=>x).filter((x, i, a) => a.indexOf(x) == i).sort((a,b)=> a-b)
console.log(arr)
你可以使用 reduce 并传递 Set 对象作为它的初始值,Set 为唯一值。
var list = [[1,2],[3,4],[5,1]];
var arr = list.reduce((acc, c)=>{ c.map((a)=>{ acc.add(a) }); return acc; }, new Set());
我有一个数字列表,格式如下:
[[1,2],[3,4],[5,1]]
我期待得到下一个输出:
[1,2,3,4,5]
这是我当前方法的一个示例:
<!DOCTYPE html>
<html>
<head>
<title>Expected - [1,2,3,4,5,6,7,8,9,10]</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
// Expected - [1,2,3,4,5,6,7,8,9,10];
var array_of_arrays = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]];
console.log(array_of_arrays);
// Section 1 - code does what I expect. I would like something like Section 2.
var array_of_values = [];
array_of_arrays.map((x) => {
x.map((y) => {
if (array_of_values.indexOf(y) == -1)
array_of_values.push(y)
})
});
console.log(array_of_values);
// Section 2 - This code does not do what I expect.
var resp = array_of_arrays.map(x => x.map(y => y));
console.log(resp);
});
</script>
</body>
</html>
您可以使用 flatMap
来做到这一点。它根据提供的回调函数展平(取消嵌套)数组。使用过滤器过滤掉重复项并使用 sort()
var a=[[1,2],[3,4],[5,1]].flatMap((x)=>x);
var arr=[];
console.log(a.filter((e)=>arr.indexOf(e)==-1?arr.push(e):false).sort());
您可以使用 Array.prototype.reduce() and using Set
let arr = [[1,2],[3,4],[5,1]]
//flatting array
arr = arr.reduce((ac,item) => ([...ac,...item]),[])
//remove dupliactes
arr = [...new Set(arr)];
console.log(arr);
一种方法可以使用 reduce() with a set 作为 accumulator
:
let input = [[1,2],[3,4],[5,1]];
let res = input.reduce((acc, a) =>
{
a.forEach(x => acc.add(x));
return acc;
}, new Set());
console.log(Array.from(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
另一种方法是使用新的 flat() 方法:
let input = [[1,2],[3,4],[5,1]];
let res = new Set(input.flat());
console.log(Array.from(res));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
您可以创建一个 distinct
过滤器并使用 flatMap
const distinct = (value, index, self) => {
return self.indexOf(value) == index;
}
console.log(
[[1,2],[3,4],[5,6],[7,8],[9,10],[1,2],[3,4],[5,6],[7,8],[9,10]]
.flatMap(x => x)
.filter(distinct)
);
这是你想要的吗
阅读评论
var array_of_arrays = [[1, 2],[3, 4], [5, 6], [7, 8], [9, 10], [1, 2],[3, 4], [5, 6], [7, 8], [9, 10]];
// flat the array and then select distinct after that sort the array out
var arr = array_of_arrays.flatMap((x)=>x).filter((x, i, a) => a.indexOf(x) == i).sort((a,b)=> a-b)
console.log(arr)
你可以使用 reduce 并传递 Set 对象作为它的初始值,Set 为唯一值。
var list = [[1,2],[3,4],[5,1]];
var arr = list.reduce((acc, c)=>{ c.map((a)=>{ acc.add(a) }); return acc; }, new Set());