使用lodash检查属性是否有价值

时间:2017-04-29 20:30:44

标签: javascript lodash

如何检查value属性是否有价值

[
  {
    "name": "DESIGEMPRESA",
    "value": ""
  },
  {
    "name": "DSP_DIRECAO",
    "value": ""
  },
  {
    "name": "MES",
    "value": ""
  }
]

5 个答案:

答案 0 :(得分:2)

var arr = [
  {
    "name": "DESIGEMPRESA",
    "value": ""
  },
  {
    "name": "DSP_DIRECAO",
    "value": ""
  },
  {
    "name": "MES",
    "value": "d"
  }
]

var out = arr.filter(function(obj){
  return obj.value
})

console.log(out)

答案 1 :(得分:2)

你不需要lodash:

<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

答案 2 :(得分:2)

您可以使用Array#some检查至少有一个元素是否没有空value值。

var arr = [
  {
    "name": "DESIGEMPRESA",
    "value": ""
  },
  {
    "name": "DSP_DIRECAO",
    "value": ""
  },
  {
    "name": "MES",
    "value": ""
  }
],
res = arr.some(v => v.value != ''); //no object with not empty value

console.log(res);

答案 3 :(得分:1)

lodash中有_.isEmpty()

$parentRec
    const a = [
      {
        "name": "DESIGEMPRESA",
        "value": ""
      },
      {
        "name": "DSP_DIRECAO",
        "value": ""
      },
      {
        "name": "MES",
        "value": ""
      }
    ]
    
    const r = a.map(x => _.isEmpty(x.value))
    
    console.log(r)

答案 4 :(得分:1)

您可以使用 let x = [ { "name": "DESIGEMPRESA", "value": "tr" }, { "name": "DSP_DIRECAO", "value": "" }, { "name": "MES", "value": "" } ] _.find(x, (o) => o.value.length > 0); https://lodash.com/docs/4.17.4#find

{name: "DESIGEMPRESA" , value: "tr"}

返回 int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY); content.measure(measureWidth, measuredHeight); content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

https://runkit.com/csprance/5904f9fcad0c6400123abaa2