在嵌套数组中的深层按键查找

vxbzzdmp  于 2022-09-21  发布在  Node.js
关注(0)|答案(22)|浏览(269)

假设我有一个物体:

[
    {
        'title': "some title"
        'channel_id':'123we'
        'options': [
                    {
                'channel_id':'abc'
                'image':'http://asdasd.com/all-inclusive-block-img.jpg'
                'title':'All-Inclusive'
                'options':[
                    {
                        'channel_id':'dsa2'
                        'title':'Some Recommends'
                        'options':[
                            {
                                'image':'http://www.asdasd.com'                                 'title':'Sandals'
                                'id':'1'
                                'content':{
                                     ...

我想找一个id为1的对象。有这样的函数吗?我可以使用下划线的_.filter方法,但我必须从顶部开始并向下过滤。

tzdcorbm

tzdcorbm1#

@Iulian Pinzaru的答案几乎就是我需要的,但如果您的对象有任何空值,它就不起作用。这个版本解决了这个问题。

function  deepSearch (object, key, predicate) {
  if (object.hasOwnProperty(key) && predicate(key, object[key]) === true) return object

  for (let i = 0; i < Object.keys(object).length; i++) {
    const nextObject = object[Object.keys(object)[i]];
    if (nextObject && typeof nextObject === "object") {
      let o = deepSearch(nextObject, key, predicate)
      if (o != null) return o
    }
  }
  return null
}
ldxq2e6h

ldxq2e6h2#

如果您已经在使用下划线,请使用_.find()

_.find(yourList, function (item) {
    return item.id === 1;
});
esbemjvw

esbemjvw3#

这段代码允许您获取其键为用户定义的JSON中的所有对象。

function main(obj = {}, property){
 const views = [];

 function traverse(o) {
    for (var i in o) {
      if(i === property) views.push(o[i]);
      if (!!o[i] && typeof(o[i])=="object") {
        console.log(i, o[i]);
        traverse(o[i]);
      } else {
        console.log(i, o[i]);
      }
    }
    }

  traverse(obj);
  return views;

}

下面是一个例子:

const obj = {
    id: 'id at level 1',
    level2: {
      id: 'id at level 2',
      level3: {
        id: 'id at level 3',
        level4: {
          level5: {
            id: 'id at level 5'
          }
       }
    }
  },
  text: ''
}

main(obj, 'id');
dohp0rv5

dohp0rv54#

如何搜索测试5或测试3嵌套值,如果它们存在,则返回‘name’值。

{
  name: 'Test1',
  info: {
    Test 1: 'Test 1',
    'Test 3': 'Testing 3',
    'Test 4': 'Testing 4',
    Test 5: 'Testing 5'
  },
  name: 'Test2',
  info: {
    Test 1: 'Test 1',
    'Test 3': 'Testing 3',
    'Test 4': 'Testing 4',
    Test 5: ''
  },
}
33qvvth1

33qvvth15#

function getPath(obj, path, index = 0) {
        const nestedKeys = path.split('.')
        const selectedKey = nestedKeys[index]

        if (index === nestedKeys.length - 1) {
            return obj[selectedKey]
        }

        if (!obj.hasOwnProperty(selectedKey)) {
            return {}
        }

        const nextObj = obj[selectedKey]

        return Utils.hasPath(nextObj, path, index + 1)
    }

欢迎光临:Gorillaz

aamkag61

aamkag616#

找到了我一直在寻找的答案,尤其是阿里·阿尔诺艾米的解决方案。我做了一些小调整,以便也可以搜索值

function deepSearchByKey(object, originalKey, originalValue, matches = []) {
if (object != null) {
  if (Array.isArray(object)) {
    for (let arrayItem of object) {
      deepSearchByKey(arrayItem, originalKey, originalValue, matches);
    }
  } else if (typeof object == 'object') {
    for (let key of Object.keys(object)) {
      if (key == originalKey) {
        if (object[key] == originalValue) {
          matches.push(object);
        }
      } else {
        deepSearchByKey(object[key], originalKey, originalValue, matches);
      }
    }
  }
}

return matches;
}

要使用以下功能,请执行以下操作:

let result = deepSearchByKey(arrayOrObject, 'key', 'value');

这将返回包含匹配键和值的对象。

xcitsw88

xcitsw887#

不久前,我制作了一个小的库find-and,它可以在npm上找到,用于以一种隐藏的方式处理嵌套对象。returnFound函数返回找到的对象,如果找到多个对象,则返回对象数组。

例如,

const findAnd = require('find-and');

const a = [
  {
    'title': "some title",
    'channel_id':'123we',
    'options': [
      {
        'channel_id':'abc',
        'image':'http://asdasd.com/all-inclusive-block-img.jpg',
        'title':'All-Inclusive',
        'options':[
          {
            'channel_id':'dsa2',
            'title':'Some Recommends',
            'options':[
              {
                'image':'http://www.asdasd.com',
                'title':'Sandals',
                'id':'1',
                'content':{},
              },
            ],
          },
        ],
      },
    ],
  },
];

findAnd.returnFound(a, {id: '1'});

退货

{
  'image':'http://www.asdasd.com',
  'title':'Sandals',
  'id':'1',
  'content':{},
}
nimxete2

nimxete28#

我想建议对Zach/RegularMike的回答进行修改(但我没有“声誉”可以发表评论!)我发现这些解决方案是一个非常有用的基础,但在我的应用程序中受到了影响,因为如果数组中有字符串,它将递归地为字符串中的每个字符调用函数(这会导致IE11和Edge浏览器失败,并出现“超出堆栈空间”的错误)。我的简单优化是将“OBJECT”子句递归调用中使用的相同测试添加到“ARRAY”子句中的测试:

if (arrayElem instanceof Object || arrayElem instanceof Array) {

因此,我的完整代码(现在查找特定键的所有示例,因此与原始要求略有不同)是:

// Get all instances of specified property deep within supplied object
function getPropsInObject(theObject, targetProp) {
    var result = [];
    if (theObject instanceof Array) {
        for (var i = 0; i < theObject.length; i++) {
            var arrayElem = theObject[i];
            if (arrayElem instanceof Object || arrayElem instanceof Array) {
                result = result.concat(getPropsInObject(arrayElem, targetProp));
            }
        }
    } else {
        for (var prop in theObject) {
            var objProp = theObject[prop];
            if (prop == targetProp) {
                return theObject[prop];
            }
            if (objProp instanceof Object || objProp instanceof Array) {
                result = result.concat(getPropsInObject(objProp, targetProp));
            }
        }
    }
    return result;
}
42fyovps

42fyovps9#

改进了答案,以考虑对象内的循环引用。它还显示了它到达那里的路径。

在本例中,我正在搜索我知道位于全局对象中某个位置的IFrame:

const objDone = []
var i = 2
function getObject(theObject, k) {
    if (i < 1 || objDone.indexOf(theObject) > -1) return
    objDone.push(theObject)
    var result = null;
    if(theObject instanceof Array) {
        for(var i = 0; i < theObject.length; i++) {
            result = getObject(theObject[i], i);
            if (result) {
                break;
            }   
        }
    }
    else
    {
        for(var prop in theObject) {
            if(prop == 'iframe' && theObject[prop]) {
                i--;
                console.log('iframe', theObject[prop])
                return theObject[prop]
            }
            if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                result = getObject(theObject[prop], prop);
                if (result) {
                    break;
                }
            } 
        }
    }
    if (result) console.info(k)
    return result;
}

运行以下命令:getObject(reader, 'reader')将显示以下输出和最后的iframe元素:

iframe // (The Dom Element)
_views
views
manager
rendition
book
reader

注意:路径的顺序与reader.book.rendition.manager.views._views.iframe相反

zpqajqem

zpqajqem10#

function getPropFromObj(obj, prop) {
            let valueToFindByKey;
            if (!Array.isArray(obj) && obj !== null && typeof obj === "object") {
              if (obj.hasOwnProperty(prop)) {

                 valueToFindByKey = obj[prop];
               console.log(valueToFindByKey);
              } else {

                let i;
                for (i = 0; i < Object.keys(obj).length; i++) {

                    getPropFromObj(obj[Object.keys(obj)[i]], prop);
                }
              }

            }
            return null;

          }

        const objToInvestigate = {
            employeeInformation: {
              employees: {
                name: "surya",
                age: 27,
                job: "Frontend Developer",
              },
            },
          };
          getPropFromObj(objToInvestigate, "name");

1.检测深度嵌套对象中的键。
1.最后返回检测到的key的值。

ldxq2e6h

ldxq2e6h11#

只要使用递归函数即可。
请参见下面的示例:

const data = [
  {
    title: 'some title',
    channel_id: '123we',
    options: [
      {
        channel_id: 'abc',
        image: 'http://asdasd.com/all-inclusive-block-img.jpg',
        title: 'All-Inclusive',
        options: [
          {
            channel_id: 'dsa2',
            title: 'Some Recommends',
            options: [
              {
                image: 'http://www.asdasd.com',
                title: 'Sandals',
                id: '1',
                content: {},
              }
            ]
          }
        ]
      }
    ]
  }
]

function _find(collection, key, value) {
  for (const o of collection) {
    for (const [k, v] of Object.entries(o)) {
      if (k === key && v === value) {
        return o
      }
      if (Array.isArray(v)) {
        const _o = _find(v, key, value)
        if (_o) {
          return _o
        }
      }
    }
  }
}

console.log(_find(data, 'channel_id', 'dsa2'))
eeq64g8w

eeq64g8w12#

递归是你的朋友。我更新了该函数以说明属性数组:

function getObject(theObject) {
    var result = null;
    if(theObject instanceof Array) {
        for(var i = 0; i < theObject.length; i++) {
            result = getObject(theObject[i]);
            if (result) {
                break;
            }   
        }
    }
    else
    {
        for(var prop in theObject) {
            console.log(prop + ': ' + theObject[prop]);
            if(prop == 'id') {
                if(theObject[prop] == 1) {
                    return theObject;
                }
            }
            if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                result = getObject(theObject[prop]);
                if (result) {
                    break;
                }
            } 
        }
    }
    return result;
}

已更新jsFdle:http://jsfiddle.net/FM3qu/7/

px9o7tmv

px9o7tmv13#

您可以在递归函数中使用javascript some函数。有些方法的优点是,一旦孩子被发现,就可以停止循环。不要使用在大数据中速度较慢的Map。

const findChild = (array, id) => {
  let result;
  array.some(
    (child) =>
      (child.id === id && (result = child)) ||
      (result = findChild(child.options || [], id))
  );
  return result;
};

findNode(array, 1)
x6492ojm

x6492ojm14#

我们使用object-scan进行数据处理。它在概念上非常简单,但允许很多很酷的东西。以下是您将如何解决您的特定问题

// const objectScan = require('object-scan');

const find = (id, input) => objectScan(['**'], {
  abort: true,
  rtn: 'value',
  filterFn: ({ value }) => value.id === id
})(input);

const data = [{ title: 'some title', channel_id: '123we', options: [{ channel_id: 'abc', image: 'http://asdasd.com/all-inclusive-block-img.jpg', title: 'All-Inclusive', options: [{ channel_id: 'dsa2', title: 'Some Recommends', options: [{ image: 'http://www.asdasd.com', title: 'Sandals', id: '1', content: {} }] }] }] }];

console.log(find('1', data));
// => { image: 'http://www.asdasd.com', title: 'Sandals', id: '1', content: {} }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>
cbwuti44

cbwuti4415#

如果您对整个ES6感兴趣,您可以使用

const findByKey = (obj, kee) => {
    if (kee in obj) return obj[kee];
    for(n of Object.values(obj).filter(Boolean).filter(v => typeof v === 'object')) {
        let found = findByKey(n, kee)
        if (found) return found
    }
}

const findByProperty = (obj, predicate) => {
    if (predicate(obj)) return obj
    for(n of Object.values(obj).filter(Boolean).filter(v => typeof v === 'object')) {
        let found = findByProperty(n, predicate)
        if (found) return found
    }
}

按值查找将略有不同

let findByValue = (o, val) => {
    if (o === val) return o;
    if (o === NaN || o === Infinity || !o || typeof o !== 'object') return;
    if (Object.values(o).includes(val)) return o;
    for (n of Object.values(o)) {
        const found = findByValue(n, val)
        if (found) return n
    }
}

那么它们就可以像这样使用

const arry = [{ foo: 0 }, null, { bar: [{ baz: { nutherKey: undefined, needle: "gotcha!" } }]}]
const obj = { alice: Infinity, bob: NaN, charlie: "string", david: true, ebert: arry }

findByKey(obj, 'needle')
// 'gotcha!'

findByProperty(obj, val => val.needle === 'gotcha!')
// { nutherKey: undefined, needle: "gotcha!" }

findByValue(obj, 'gotcha!')
// { nutherKey: undefined, needle: "gotcha!" }

相关问题