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

时间:2017-07-20 00:43:44

标签: javascript json underscore.js lodash

您好我有一个嵌套的JSON对象,我想在其中找到特定键的值。让我们说JSON是这样的:

{{1}}

现在如果我想找到" city"的值,那么它应该给我" ca",如果我想找到" lng&#34的值;然后它应该返回132.232。如果我想找到" hobbies"的价值?它应该给我[玩,唱]。我怎么能得到这个?使用下划线或lodash的解决方案将受到关注。

1 个答案:

答案 0 :(得分:1)

您可以通过递归迭代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>
&#13;
&#13;
&#13;

替代方案:这是一个非递归的vanilla 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);
      }
    }
  }

}

&#13;
&#13;
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'));
&#13;
.as-console-wrapper { min-height: 100%; top: 0; }
&#13;
&#13;
&#13;