javascript - 使用下划线在具有非结构化键的嵌套 JSON 中查找特定键的 JSON 值

标签 javascript json underscore.js lodash

我有一个嵌套 JSON 对象,我想查找其中特定键的值。假设 JSON 是这样的:

var data={

       name:"dsd",
       work: "abcd",
       address:{
          street:"wewe 32",
          apt: 12,
          city: "ca",
          geo:{
             lat: 23.4332,
             lng: 132.232
          },
          hobbies:["play","sing"]
       }        

 }

现在如果我想找到“city”的值,那么它应该给我“ca”,如果我想找到“lng”的值那么它应该返回132.232。如果我想找到“爱好”的值(value),它应该给我[玩耍、唱歌]。我怎样才能得到这个?使用下划线或 lodash 的解决方案将受到赞赏。

最佳答案

您可以通过递归迭代 lodash#some 来实现此目的。查看下面的评论以获取指导。

function getValueByKey(object, key) {
  // The resulting value of a matched key
  var result;

  // Use _.some as an iterator, to stop the iteration
  // and recursion once a match is found. Also, name 
  // the predicate function for recursion.
  _.some(object, function matchKey(value, $key) {
    if ($key === key) { // is key a match?
      result = value; // set the result
      return true; // this stops the iteration and recursion
    } else if (_.isObject(value)) { // is value an object?
      // recursively match the keys all over again
      return _.some(value, matchKey);
    }
  });

  // return matched result
  return result;
}

var data = {
  name: "dsd",
  work: "abcd",
  address: {
    street: "wewe 32",
    apt: 12,
    city: "ca",
    geo: {
      lat: 23.4332,
      lng: 132.232
    },
    hobbies: ["play", "sing"]
  }
};

function getValueByKey(object, key) {
  // The resulting value of a matched key
  var result;

  // Use _.some as an iterator, to stop the iteration
  // and recursion once a match is found. Also, name 
  // the predicate function for recursion.
  _.some(object, function matchKey(value, $key) {
    if ($key === key) { // is key a match?
      result = value; // set the result
      return true; // this stops the iteration and recursion
    } else if (_.isObject(value)) { // is value an object?
      // recursively match the keys all over again
      return _.some(value, matchKey);
    }
  });

  // return matched result
  return result;
}

console.log('hobbies:', getValueByKey(data, 'hobbies'));
console.log('geo:', getValueByKey(data, 'geo'));
console.log('lng:', getValueByKey(data, 'lng'));
.as-console-wrapper {
  min-height: 100%;
  top: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

替代方案:这是一个非递归普通 JavaScript 解决方案:

function getValueByKey(object, key) {

  // simulate recursion by stacking
  var stack = [object];
  var current, index, value;

  // keep iterating until the stack is empty
  while (stack.length) {
    // take the head of the stack
    current = stack.pop();
    // iterate over the current object
    for (index in current) {
      // get value of the iterated object
      value = current[index];
      // is it a match?
      if (key === index) {
        return value; // return the matched value
      } 
      // value must be an object and not a null value
      // to be subject for the next stack iteration
      else if (value !== null && typeof value === 'object') {
        // add this value in the stack
        stack.unshift(value);
      }
    }
  }

}

var data = {
  name: "dsd",
  work: "abcd",
  address: {
    street: "wewe 32",
    apt: 12,
    city: "ca",
    geo: {
      lat: 23.4332,
      lng: 132.232
    },
    hobbies: ["play", "sing"]
  }
}

function getValueByKey(object, key) {

  // simulate recursion by stacking
  var stack = [object];
  var current, index, value;

  // keep iterating until the stack is empty
  while (stack.length) {
    // take the head of the stack
    current = stack.pop();
    // iterate over the current object
    for (index in current) {
      // get value of the iterated object
      value = current[index];
      // is it a match?
      if (key === index) {
        return value; // return the matched value
      } 
      // value must be an object and not a null value
      // to be subject for the next stack iteration
      else if (value !== null && typeof value === 'object') {
        // add this value in the stack
        stack.unshift(value);
      }
    }
  }

}

console.log('hobbies:', getValueByKey(data, 'hobbies'));
console.log('geo:', getValueByKey(data, 'geo'));
console.log('lng:', getValueByKey(data, 'lng'));
.as-console-wrapper { min-height: 100%; top: 0; }

关于javascript - 使用下划线在具有非结构化键的嵌套 JSON 中查找特定键的 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45203309/

相关文章:

collections - 在主干集合上使用下划线方法 'find'

javascript - 表单验证和 Node JS - 避免厄运金字塔

python - 从两个查询 django 的输出创建嵌套 json

javascript - 使用 "value":""创建对象的任何缺点

javascript - Highcharts 折线图内存不足

javascript - 根据json数据属性动态生成多个div

javascript - React 什么时候重新渲染子组件?

javascript - 如何从 Javascript 创建动态组件

javascript - & 符号模型可以包含 & 符号集合吗?

json - 在 Postgres 中从 JSON 获取嵌套对象值