JS 数组与字典(JSON)通过字符串快捷取值

时间:2025-04-06 20:31:18
<script> // -------------------------- Json 测试 // 数据 const json = { a: 'a', b: { c: 'c', d: [{ e: 'e' }], f: [[{ g: 'g' }]] } } // 调用 console.log(GetValue(json, '[0]')) // {e: 'e'} console.log(GetValue(json, '[0][0].g')) // g console.log(GetValue(json, '[0[')) // g console.log(GetValue(json, ']0]')) // g console.log(GetValue(json, '.0.')) // g // -------------------------- Array 测试 // 数据 const arr = [ { a: 'a', b: [{ c: 'c', d: { e: 'e' } }] } ] // 调用 console.log(GetValue(arr, '[0].b[0].')) // e console.log(GetValue(arr, '.')) // e </script>