嵌套对象中的搜索关键字和返回值 [英] Search key and return value in nested object

查看:85
本文介绍了嵌套对象中的搜索关键字和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我的对象如下所示.它混合了不同的数组和父对象,没有层次结构的顺序.例如

Lets say my object looks like the below. Its a mix of different arrays and parents, no hierarchical order. e.g.

"person": {
"id": 12345,
"name": "John Doe",
"emergencyContacts": [
  {
    "name": "Jane Doe",
    "phone": "888-555-1212",
    "relationship": "spouse",
    "moreDetails": {
      "id": 12345,
      "phones": {},
      "home": "800-123-4567",
      "mobile": "877-123-1234"
    }
  },
  {
    "name": "Justin Doe",
    "phone": "877-123-1212",
    "relationship": "parent",
    "mobile": "877-123-1234"
  }
],
"workContacts": [
  {
    "name": "Jane Doe",
    "phone": "888-555-1212",
    "relationship": "spouse",
    "moreworkDetails": {
      "id": 12345,
      "phones": {},
      "home": "800-123-4567",
      "mobile": "877-123-1234"
    }
  },
  {
    "name": "Justin Doe",
    "phone": "877-123-1212",
    "relationship": "parent",
    "mobile": "877-123-1234"
  }
]
}

我希望能够搜索person的整个对象并带回键mobile的值.我猜想该对象需要展平,并使用唯一"移动电话号码列表创建一个新数组,例如

I want to be able to search the entire object of person and bring back the value of the key mobile. I am guessing that the object needs to be flattened and a new array created with the list of "unique" mobile numbers found e.g.

var mobile = { 
0: 888-555-1212,
1: 800-123-4567,
2: 877-123-1234,
3: 083-111-3346
}

我已经在纯js和lodash中搜索了解决方案,但是发现的每个对象都知道父级的名称和数组的深度-假设父级和深度可能是无限的(这将其设置为问题在嵌套对象的深处按键查找).任何帮助将不胜感激.谢谢

I have searched for solutions in pure js and lodash, however each one found is knowing what the parent is called and depth of the array - lets assume the parent and depth could be limitless(this sets it aside from question Find by key deep in a nested object). Any help would be muchly appreciated. Thanks

推荐答案

您将需要一个递归函数.有关说明,请查看评论

You will need a recursive function. For explanation please check the comments

let person = {
  "id": 12345,
  "name": "John Doe",
  "emergencyContacts": [{
      "name": "Jane Doe",
      "phone": "888-555-1212",
      "relationship": "spouse",
      "moreDetails": {
        "id": 12345,
        "phones": {},
        "home": "800-123-4567",
        "mobile": "877-123-1234"
      }
    },
    {
      "name": "Justin Doe",
      "phone": "877-123-1212",
      "relationship": "parent",
      "mobile": "877-123-1234"
    }
  ],
  "workContacts": [{
      "name": "Jane Doe",
      "phone": "888-555-1212",
      "relationship": "spouse",
      "moreworkDetails": {
        "id": 12345,
        "phones": {},
        "home": "800-123-4567",
        "mobile": "877-123-1236"
      }
    },
    {
      "name": "Justin Doe",
      "phone": "877-123-1212",
      "relationship": "parent",
      "mobile": "877-123-1235"
    }
  ]
}
let arrys = [];

//iterate the object 
function recursive(obj, key) {
  //iterate the object
  for (let keys in obj) {
    // check if the key name is same as the desired one
    if (keys === key) {
      // then push the value to an array
      arrys.push(obj[keys])

    } else {
      // if the value of a key is an array & if it is not empty
      if (Array.isArray(obj[keys]) && obj[keys].length > 0) {
        // iterate the array. in each iteration you will get the object
        // example emergencyContacts. In each iteration 
        // call the same function with new data 
        obj[keys].forEach(function(item) {
          // pass that object to the same function
          recursive(item, key)
        })

      }
      // if the value is an object then again call the same function 
      else if (typeof obj[keys] === 'object') {
        recursive(obj[keys], key)
      }
    }
  }
}

recursive(person, 'mobile');
//create object from the array
let newObj = {}
for (let m = 0; m < arrys.length; m++) {
  newObj[m] = arrys[m]

}
console.log(newObj)

这篇关于嵌套对象中的搜索关键字和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆