代码如下,有没有方法可以动态改变filter中if中的判断内容,根据filterObj中所包含的元素内容改变而改变?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动态改变if中的判断内容</title>
</head>

<body>
    <script>
        let list = [
            {
                name: '孙悟空',
                phone: '13896965655'
            },
            {
                name: '爱米莉娅',
                phone: '12589633654'
            },
            {
                name: '雷姆',
                phone: '13589633684'
            },
            {
                name: '孙二娘',
                phone: '13564236966'
            }
        ]

        //其中filterObj中的元素会动态改变,有没有一种方法让filter中if判断的内容也动态的改变
    //  1.   let filterObj = {
    //         name: '孙',

    //     }
    //     let newlist = []
    //     list.filter((item) => {
    //         if ((item.name.indexOf(filterObj.name) !== -1)) {
    //             newlist.push(item);
    //         }
    //     });
    //     console.log(newlist)

    //  2.   let filterObj = {
    //        phone:'1'

    //     }
    //     let newlist = []
    //     list.filter((item) => {
    //         if ((item.phone.indexOf(filterObj.phone) !== -1)) {
    //             newlist.push(item);
    //         }
    //     });
    //     console.log(newlist)

//3.
        // let filterObj = {
        //     name: '孙',
        //     phone: '1'
        // }
        // let newlist = []
        // list.filter((item) => {
        //     if ((item.name.indexOf(filterObj.name) !== -1) && (item.phone.indexOf(filterObj.phone) !== -1)) {
        //         newlist.push(item);
        //     }
        // });
        // console.log(newlist)

        //以上合成一块代码如何编写



    </script>

</body>

</html>
阅读 1.7k
5 个回答
✓ 已被采纳
const newlist = list.filter((item) => {
  return Object.keys(filterObj).every(key => item[key].indexOf(filterObj[key]) !== -1)
});

console.log(newlist);

条件都是 indexOf, 那循环 filterObj就好了. 把所有的结果用 && 连接起来.
类似下面的

var rs = true
for ( x in filterObj ) {
    rs = rs && xxx.indexOf(x)
}
return rs
新手上路,请多包涵
let list = [
  {
    name: '孙悟空',
    phone: '13896965655',
  },
  {
    name: '爱米莉娅',
    phone: '12589633654',
  },
  {
    name: '雷姆',
    phone: '13589633684',
  },
  {
    name: '孙二娘',
    phone: '13564236966',
  },
];  
const customFilterList = (list, filterObj) => {
  return list.filter(item => {
    return Object.keys(filterObj).every(
      key => item[key].indexOf(filterObj[key]) !== -1,
    );
  });
};

const filterObjA = { name: '孙' };
const filterObjB = { phone: '1' };
const filterObjC = { name: '孙', phone: '1' };
const listA = customFilterList(list, filterObjA);
const listB = customFilterList(list, filterObjB);
const listC = customFilterList(list, filterObjC);

简单点实现可以把你的filterObj转换成条件Function再组合使用(函数式)

const or = (...fns) => (item) => fns.some(fn => fn(item));
const and = (...fns) => (item) => fns.every(fn => fn(item));

const contains = (member, text) => (item) => item[member] && item[member].indexOf(text) !== -1;

// 可以增加更多条件规则, 如:
// const startsWith = (member, text) => (item) => item[member] && item[member].startsWith(text);

let list = [{
        name: '孙悟空',
        phone: '13896965655'
    }, {
        name: '爱米莉娅',
        phone: '12589633654'
    }, {
        name: '雷姆',
        phone: '13589633684'
    }, {
        name: '孙二娘',
        phone: '13564236966'
    }
];


const f1 = contains('name', '孙');
const f2 = contains('phone', '1');

const items1 = list.filter(f1);
const items2 = list.filter(f2);

const items3 = list.filter(and(f1,f2));
const items4 = list.filter(or(f1,f2));

const f3 = contains('phone', '4');
// 更复杂的条件组合
const items5 = list.filter(and(or(f1,f2), f3));

也可使用一些现成的库
如:https://ramda.cn

let list = [
            {
                name: '孙悟空',
                phone: '13896965655'
            },
            {
                name: '爱米莉娅',
                phone: '12589633654'
            },
            {
                name: '雷姆',
                phone: '13589633684'
            },
            {
                name: '孙二娘',
                phone: '13564236966'
            }
        ]
let filterObj = {
    name: '孙',
    phone: '135'
}

let newlist = list.filter((item) => {
    for (let key in filterObj) {
        if (item[key].indexOf(filterObj[key]) === -1) {
            return false;
        }
    }
    return true;
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏