gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:03:46 26 4
gpt4 key购买 nike

我有一个嵌套的 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),它应该给我 [play, sing]。我怎样才能得到这个?使用下划线或 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>

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

}

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com