过滤子数组
Filter Sub array
我正在尝试过滤具有子数组的数组。
我想通过子数组过滤我的数组
我的阵列看起来像这样:
var Branches = [
{
Name: 'branch1',
Screens: [
{
Name: 'Screen1',
Player: {
Status: 0
}
},
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
},
{
Name: 'branch2',
Screens: [
{
Name: 'Screen1',
Player: {
Status: 0
}
},
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
}]
我已经尝试使用 linq.js
var param = 1
var result = Enumerable.From(Branches).Where(function (d) {
return (d.Screen.some(x => x.Player.Status === param))
}).ToArray();
但是此代码也从每个分支获得状态 0
我只想从每个分支中获取 status = 1
预计结束:
[
{
Name: 'branch1',
Screens: [
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
},
{
Name: 'branch2',
Screens: [
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
}
]
谢谢 ;)
var Branches = [
{
Name: "branch1",
Screens: [
{
Name: "Screen1",
Player: {
Status: 0
}
},
{
Name: "Screen2",
Player: {
Status: 1
}
}
]
},
{
Name: "branch2",
Screens: [
{
Name: "Screen1",
Player: {
Status: 0
}
},
{
Name: "Screen2",
Player: {
Status: 1
}
}
]
}
];
var param = 1;
console.log(Branches.reduce( (p, c) => ((c.Screens = c.Screens
.filter(s => s.Player.Status == param)).length && p.push(c), p), []
));
您可以映射对象并过滤 Screens
。
var branches = [{ Name: 'branch1', Screens: [{ Name: 'Screen1', Player: { Status: 0 } }, { Name: 'Screen2', Player: { Status: 1 } }] }, { Name: 'branch2', Screens: [{ Name: 'Screen1', Player: { Status: 0 } }, { Name: 'Screen2', Player: { Status: 1 } }] }],
wanted = 1,
result = branches.map(({ Screens, ...o }) => ({ ...o, Screens: Screens.filter(({ Player: { Status } }) => Status === wanted) }));
console.log(result);
使用linq.js,您需要将其视为选择具有一组过滤屏幕的分支。您可能不想修改现有项目,因此您应该创建新项目。
let status = 1;
let result = Enumerable.From(Branches)
.Select(({Screens, ...rest}) => ({
Screens: Enumerable.From(Screens)
.Where(({Player:{Status}}) => Status === status)
.ToArray(),
...rest
})).ToArray();
尽管 Nina 表明,您并不真的需要这个,因为 Array.map
和 Array.filter
可以处理这个。
我正在尝试过滤具有子数组的数组。 我想通过子数组过滤我的数组 我的阵列看起来像这样:
var Branches = [
{
Name: 'branch1',
Screens: [
{
Name: 'Screen1',
Player: {
Status: 0
}
},
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
},
{
Name: 'branch2',
Screens: [
{
Name: 'Screen1',
Player: {
Status: 0
}
},
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
}]
我已经尝试使用 linq.js
var param = 1
var result = Enumerable.From(Branches).Where(function (d) {
return (d.Screen.some(x => x.Player.Status === param))
}).ToArray();
但是此代码也从每个分支获得状态 0
我只想从每个分支中获取 status = 1
预计结束:
[
{
Name: 'branch1',
Screens: [
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
},
{
Name: 'branch2',
Screens: [
{
Name: 'Screen2',
Player: {
Status: 1
}
}
]
}
]
谢谢 ;)
var Branches = [
{
Name: "branch1",
Screens: [
{
Name: "Screen1",
Player: {
Status: 0
}
},
{
Name: "Screen2",
Player: {
Status: 1
}
}
]
},
{
Name: "branch2",
Screens: [
{
Name: "Screen1",
Player: {
Status: 0
}
},
{
Name: "Screen2",
Player: {
Status: 1
}
}
]
}
];
var param = 1;
console.log(Branches.reduce( (p, c) => ((c.Screens = c.Screens
.filter(s => s.Player.Status == param)).length && p.push(c), p), []
));
您可以映射对象并过滤 Screens
。
var branches = [{ Name: 'branch1', Screens: [{ Name: 'Screen1', Player: { Status: 0 } }, { Name: 'Screen2', Player: { Status: 1 } }] }, { Name: 'branch2', Screens: [{ Name: 'Screen1', Player: { Status: 0 } }, { Name: 'Screen2', Player: { Status: 1 } }] }],
wanted = 1,
result = branches.map(({ Screens, ...o }) => ({ ...o, Screens: Screens.filter(({ Player: { Status } }) => Status === wanted) }));
console.log(result);
使用linq.js,您需要将其视为选择具有一组过滤屏幕的分支。您可能不想修改现有项目,因此您应该创建新项目。
let status = 1;
let result = Enumerable.From(Branches)
.Select(({Screens, ...rest}) => ({
Screens: Enumerable.From(Screens)
.Where(({Player:{Status}}) => Status === status)
.ToArray(),
...rest
})).ToArray();
尽管 Nina 表明,您并不真的需要这个,因为 Array.map
和 Array.filter
可以处理这个。