【问题标题】:array_search Recursive in javascriptjavascript中的array_search递归
【发布时间】:2012-09-29 23:04:16
【问题描述】:
function array_searchRecursive( $needle, $haystack, $strict=false, $path=array() )
{
    if( !is_array($haystack) ) {
        return false;
    }

    foreach( $haystack as $key => $val ) {

        if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
            $path = array_merge($path, array($key), $subPath);

            return $path;
        } else if( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {

            $path[] = $key;
            return $path;
        }
    }
    return false;
}

是否有任何机构向我建议相同的功能,可以在 javascript 中实现。 参考http://www.php.net/manual/en/function.array-search.php#68424

【问题讨论】:

标签: javascript jquery logic business-logic


【解决方案1】:

这可能会给你一个开始。未经彻底测试或高度优化,并假设使用 jQuery(用其他实现替换 jQuery 实用程序函数应该不是大问题)。

function searchArrayRecursive(needle, haystack, strict) {

    function constructPath(needle, haystack, path, strict) {
        if (!$.isArray(haystack)) {
            return false;
        }
        var index;
        for (index = 0; index < haystack.length; index++) {
            var value = haystack[index];
            var currentPath = $.merge([], path);
            currentPath.push(index);

            if ((strict && value === needle) || (!strict && value == needle)) {
                return currentPath;
            }
            if ($.isArray(value)) {

                var foundPath = constructPath(needle, value, currentPath, strict);
                if (foundPath) {
                    return foundPath;
                }
            }
        }

        return false;
    }


    return constructPath(needle, haystack, [], strict);
}

http://jsfiddle.net/b8TxJ/2/

【讨论】:

  • 扩展@Amitay 的工作,这里有一个函数可以在haystack 包含数组和/或对象时返回路径:jsfiddle.net/jshado1/rKHXC
【解决方案2】:

确实下划线(或者可能表现更好:lodash)是你的人。 JavaScript 在很大程度上是一种函数式语言,并且在最新的规范中包含下划线提供的大多数功能。对于浏览器兼容,仍建议使用下划线。

在您的情况下最好的下划线功能是:

var haystack = [
  {a: 1}, [{b: 2}, {c: 3}, [{d: 4}, {e: 5}, [{f: 6}, {g: 7}] ] ]
],
needle = 4;

//Search
var result = _(haystack).chain() //chain so we can keep underscoring
  .flatten() //flatten the array
  .find(function(o) { //find the first element that matches our search function
    return _(o).chain() //chain so we can keep underscoring
      .values() //get all object values as an array
      .contains(needle) //see if any of our values contains the needle
      .value(); //get out of the chain
  })
  .value(); //get out of the chain

//In short:
var result = _(haystack).chain().flatten().find(function(o) { return _(o).chain().values().contains(needle).value(); }).value();

当然,您必须对此进行微调并实施您的 $strict 等等。

【讨论】:

  • 它究竟返回了什么?请求的原始数组中的路径,还是简单的值?如果它返回值,这似乎是不必要的,因为我们正在寻找一个特定的值。
  • 我现在看到它将返回第一个具有任何值为“needle”的属性的对象。这是你的意思吗?因为引用的 php 函数返回找到值所在的“遍历路径”。
【解决方案3】:

如果您愿意使用库,我认为 Underscore.js 具有可以为您提供所需内容的功能,可能使用 _.find()、_.pluck() 或 _.pick()。还有很多其他方法可以帮助解决这个问题。

如果你想在核心 JS 中做,请在 Underscore 源代码的封面下偷看,它有 FANTASTIC 注释/文档:

http://underscorejs.org/docs/underscore.html

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2017-05-08
    • 2020-01-05
    • 2014-08-23
    • 2011-06-07
    • 2011-04-07
    相关资源
    最近更新 更多