数据为字符串,转为数组并取值

29 阅读1分钟
const Value =   '[
      {Name:"oo",Value:"1"}, 
      {Name:"e",Value:"0"}, 
      {Name:"h",Value:"00000"}, 
      {Name:"p",Value:"1mememe"}
    ]'
    const inValue=JSON.parse(Value)
    此时 inValue为:[
      {Name:"oo",Value:"1"}, 
      {Name:"e",Value:"0"}, 
      {Name:"h",Value:"00000"}, 
      {Name:"p",Value:"1mememe"}
    ]
    数组方法findIndex如果为false则返回-1
    const hasNameH = inValue.findIndex((item) => {
          return item.Name === 'h'
        })
        const getValueByKey = function(key) {
           const index = inValue.findIndex((item) => {
             return item.Name === 'p'
           })
           return inValue[index]
        }
        if (hasNameH > -1) {
          document.write(getValueByKey('p').Value)
        }
        循环
        const formatValue = function(value) {
           const _newObj = {}
           for (let index = 0; index < value.length; index++) {
            放置数据
        _newObj[value[index].Name] = value[index].Value
           }
          return _newObj
        }
        document.write('<br>')
        JSON转化数据
        document.write(JSON.stringify(formatValue(inValue)))