简体   繁体   English

找到 object 的最佳方法

[英]best way to find object

I need to find object by property children_inherit where value is true and return this object.我需要通过属性 children_inherit 找到 object,其中值为 true 并返回此 object。 what is the best way to do this, maybe it possible to do this with lodash library or better with clean js?最好的方法是什么,也许可以使用 lodash 库或使用干净的 js 更好地做到这一点?

please check object below...请检查下面的 object...

 { "file": [ { "children_inherit": false, "value": "123123" }, { "children_inherit": false, "value": "asdad" } ], "sample": [ { "children_inherit": true, "value": "1231" } ], "treatment": [ { "children_inherit": false, "value": "asdasd" } ] }

const obj = { 
    "file": [ 
        { 
            "children_inherit": false, 
             'value': 123123 
        }, 
        { 
            "children_inherit": false, 
             'value': 'asdad' 
        } 
    ], 
    "sample": [ 
        { 
            "children_inherit": true, 
             'value': '1231' 
        } 
    ], 
    "treatment": [ 
        { 
            "children_inherit": false, 
             "value": 'asdasd' 
        } 
    ] 
}

let value
function getValue() {
  for(const [v] of Object.values(obj)) {  
    if(v.children_inherit) {
      value = v.value
    }
  }
}

getValue()

console.log(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM