【问题标题】:How do return the value of an attribute in a nested object without using the traditional js object navigation style?如何在不使用传统的 js 对象导航样式的情况下返回嵌套对象中的属性值?
【发布时间】:2021-06-11 00:37:54
【问题描述】:

例如,如何在不执行“tags.value”或“style.value”的情况下自动循环遍历下面对象中的所有“value”属性

[
  {
    "title": "Old Man's War",
    "author": {
      "name": "John Scalzi",
      "tags": [
        {
          "value": "American"
        }
      ]
    }
  },
  {
    "title": "The Lock Artist",
    "author": {
      "name": "Steve Hamilton",
      "tags": [
        {
          "value": "English"
        }
      "style": [
        {
          "value": "Italix"
        }
      ]
    }
  }
]

【问题讨论】:

    标签: javascript node.js json search


    【解决方案1】:

    您是否要提取所有样式和标签的列表?一种直接的方法是通过reduce()map() 组合

    let attributes = data.reduce((b, a) => ({
        tags: [...b.tags, a.author.tags?.map(v => v.value) || []].flat(),
        styles: [...b.styles, a.author.style?.map(v => v.value) || []].flat() }), {tags:[], styles:[]})
    

    输出:

    {
      "tags": [
        "American",
        "English"
      ],
      "styles": [
        "Italix"
      ]
    }
    

    const data = [{
      "title": "Old Man's War",
      "author": {
        "name": "John Scalzi",
        "tags": [{
          "value": "American"
        }]
      }
    }, {
      "title": "The Lock Artist",
      "author": {
        "name": "Steve Hamilton",
        "tags": [{
          "value": "English"
        }],
        "style": [{
          "value": "Italix"
        }]
      }
    }]
    
    // just the tag values
    
    let attributes = data.reduce((b, a) => ({
        tags: [...b.tags, a.author.tags?.map(v => v.value) || []].flat(),
        styles: [...b.styles, a.author.style?.map(v => v.value) || []].flat() }), {tags:[], styles:[]})
    console.log(attributes)

    【讨论】:

    • @user16192387 - 这对您解决问题有帮助吗?如果是,请标记为答案。谢谢!
    【解决方案2】:

    //first,there is an error in your json.I assume it like this
    const data = [
       {
          "title":"Old Man's War",
          "author":{
             "name":"John Scalzi",
             "tags":[
                {
                   "value":"American"
                }
             ]
          }
       },
       {
          "title":"The Lock Artist",
          "author":{
             "name":"Steve Hamilton",
             "tags":[
                {
                   "value":"English"
                }
             ],
             "style":[
                {
                   "value":"Italix"
                }
             ]
          }
       }
    ]
    
    for(const top of data){
      if(typeof top == 'object'){
        for(const second_key in top){
          const second = top[second_key]
          if(typeof second == 'object'){
            for(const third_key in second){
              const third = second[third_key]
              if(Array.isArray(third)){
                for(const fourth of third){
                  document.write(fourth['value']+"<br>")
                }
              }
            }
          }
        }
      }
    }

    //this is another solution
    const data = [
       {
          "title":"Old Man's War",
          "author":{
             "name":"John Scalzi",
             "tags":[
                {
                   "value":"American"
                }
             ]
          }
       },
       {
          "title":"The Lock Artist",
          "author":{
             "name":"Steve Hamilton",
             "tags":[
                {
                   "value":"English"
                }
             ],
             "style":[
                {
                   "value":"Italix"
                }
             ]
          }
       }
    ]
    
    function lookForValue(obj){
      if(obj.hasOwnProperty('value')){
        document.write(obj['value']+"<br>")
        return
      }
      if(typeof obj == 'object'){
        for(const key in obj){
          lookForValue(obj[key])
        }
      }
      if(Array.isArray(obj)){
        for(const item in obj){
          lookForValue(item)
        }
      }
    }
    
    lookForValue(data)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多