javascript 工具类Lodash集合查询,修改,删除使用例子

本文通过一个具体的示例展示了如何使用Lodash库进行数组数据的查找、修改与删除操作。示例中使用了一个包含用户信息的数组,通过Lodash提供的方法实现了对特定用户的查找,并进一步演示了如何修改找到的用户数据以及从数组中移除该用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Lodash集合使用例子</title>
    <meta name="generator" content="editplus" />
    <meta name="author" content="" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <script type="text/javascript" src="lodash.core.min.js"></script>
    <script>
        var users = [
            { 'id': '6000', 'user': 'barney', 'age': 36, 'money': 1000 },
            { 'id': '6001', 'user': 'fred', 'age': 40, 'money': 2000 },
            { 'id': '6002', 'user': 'pebbles', 'age': 18, 'money': 3000 }
        ];
        //查找数据
        var user = _.find(users, function (o) { return o.id == 6002; });
        if (typeof (user) == 'undefined') {
            console.log('没有相关数据');
        } else {
            //修改数据
            console.log('修改前数据==》');
            console.log(JSON.stringify(user));
            user.age = 28;
            user.money = 6000; //修改金钱为6000
            console.log('修改后数据==》');
            console.log(JSON.stringify(user));
            console.log('修改后集合数据==》');
            console.log(JSON.stringify(users));
            //删除数据
            var leave_users = _.remove(users, function (o) { return o.id == 6002; });
            console.log('删除后数据==》');
            console.log(JSON.stringify(users));
        }

    </script>
</head>

<body>

</body>
</html>
Collection Functions (Arrays or Objects) _.each(list, iterator, [context]) Alias: forEach Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is bound to the _.map(list, iterator, [context]) Alias: collect Produces a new array of values by mapping each value in list through a transformation function ( _.reduce(list, iterator, memo, [context]) Aliases: inject, foldl Also known as inject and foldl, reduce boils down a list of values into a single value. _.reduceRight(list, iterator, memo, [context]) Alias: foldr The right-associative version of reduce. Delegates to the JavaScript 1.8 version of _.find(list, iterator, [context]) Alias: detect Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as _.filter(list, iterator, [context]) Alias: select Looks through each value in the list, returning an array of all the values that pass a truth test ( _.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in _.findWhere(list, properties) Looks through the list and returns the first value that matches all of the key-value pairs listed in properties. _.reject(list, iterator, [context]) Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter. _.every(list, iterator, [context]) Alias: all Returns true if all of the values in the list pass the iterator truth test. Delegates to the native method _.some(list, [iterator], [context]) Alias: any Returns true if any of the values in the list pass the iterator truth test. Short-circuits and stops traversing the list _.contains(list, value) Alias: include Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. _.invoke(list, methodName, [*arguments]) Calls the method named by methodName on each value in the list. Any extra arguments passed to _.pluck(list, propertyName) A convenient version of what is perhaps the most common use-case for map: extracting a list of property values. _.max(list, [iterator], [context]) Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.min(list, [iterator], [context]) Returns the minimum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator _.groupBy(list, iterator, [context]) Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of _.countBy(list, iterator, [context]) Sorts a list into groups and returns a count for the number of objects in each group. Similar to groupBy _.shuffle(list) Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle. _.toArray(list) Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object. _.size(list) Return the number of values in the list.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smartsmile2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值