function parseJson(jsonObj, key, value) {
// 循环所有键
let array = []
for (let v in jsonObj) {
let element = jsonObj[v]
// 1.判断是对象或者数组
if (typeof (element) == ‘object’) {
let result = parseJson(element, key, value)
if(result.length) {
array = array.concat(result)
}
} else {
if (v == key) {
if (element == value){
array.push(jsonObj)
}
}
}
}
return array
}
var array = [
{
code: 1,
value: 'b'
},
{
code: 2,
value: 'a'
},
{
code: 3,
value: 'c'
},
{
code: 4,
value: 'd'
},
{
code: 5,
value: 'e'
},
{
code: 5,
value: 'f'
}
]
console.log(parseJson(array, 'code', 5)) //