我有三个对象数组:
extension ViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return string.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
}
}
我需要没有以下内容的新名称数组:
在这种情况下,我的预期输出应该是
const names = [
{ value: "Time" },
{ value: "Angle" },
{ value: "Speed" },
{ value: "Mass" }
];
const params = [
{ name: "Time", value: "s" },
{ name: "Time", value: "h" },
{ name: "Time", value: "m" },
{ name: "Angle", value: "rad" },
{ name: "Angle", value: "grade" }
];
const selected = [
{ id: 1, name: "Time", param: "s" },
{ id: 2, name: "Time", param: "h" },
{ id: 3, name: "Time", param: "m" },
{ id: 4, name: "Angle", param: "grade" },
{ id: 5, name: "Speed", param: null }
];
答案 0 :(得分:1)
您可以按
进行过滤null
和
const
names = [{ value: "Time" }, { value: "Angle" }, { value: "Speed" }, { value: "Mass" }],
params = [{ name: "Time", value: "s" }, { name: "Time", value: "h" }, { name: "Time", value: "m" }, { name: "Angle", value: "rad" }, { name: "Angle", value: "grade" }],
selected = [ { id: 1, name: "Time", param: "s" }, { id: 2, name: "Time", param: "h" }, { id: 3, name: "Time", param: "m" }, { id: 4, name: "Angle", param: "grade" }, { id: 5, name: "Speed", param: null }],
result = names.filter(({ value }) => {
const check = ({ name }) => name === value,
pl = params.filter(check).length,
sl = selected.filter(check).length;
return !selected.some(o => o.name === value && o.param === null)
&& !(pl === sl && pl);
});
console.log(result);