如何使用键数组在对象的对象中查找值

时间:2018-09-26 11:53:35

标签: javascript ramda.js

例如,我有一个对象,该对象本身具有对象和数组:

<button name="quiz1" value="a" onClick="b1(this);">CLICK ME</button>
<button name="quiz1" value="b" onClick="b1(this);">CLICK ME</button></td>
<br>
<br>

<button name="quiz2" value="a" onClick="b2(this);">CLICK ME</button>
<button name="quiz2" value="b" onClick="b2(this);">CLICK ME</button></td>


<a id='action-link' href='http://example.com?z=0'>Click to continue</a>

和一个以键为值的数组:

const object = 
{
    a: {
        b: [
            0: 'something',
            1: {
                c: 'the thing that I need',
            },
        ],
    },
};

如何使用此数组在对象中导航并给我值? 也许有一种方法可以使用const array = [ 'a', 'b', '1', 'c', ]; 来做到这一点?或者只是为了使它看起来易于阅读。

3 个答案:

答案 0 :(得分:6)

您可以减少定义通过对象的路径的数组。 数组中确实有错误。路径应为:['a','b','1','c'],因为您需要的东西在b数组的第二个元素内,而不是第一个。

with events as (select count(distinct b."SubstituteTeacher"), row_number() over (partition by b."SubstituteTeacher" order by d."StartTime") as seq from dbo."Accounts" a

full outer join dbo."WorkContracts" b on a."ID" = b."SubstituteTeacher" . 

full outer join dbo."SubstituteTeacherPeriods" c . 
on a."ID" = c."SubstituteTeacher" . 

full outer join dbo."OrderPeriods" d . 
on a."ID" = d."CreatedBy"

full outer join dbo."Addresses" e . 
on a."Address" = e."ID"

full outer join dbo."StandardCities" f
on e."City" = f."ID"

full outer join dbo."Orders" g
on d."Order" = g."ID" 

where a."ID" != 16 and a."ID" != 11797 

and b."WorkContractType" != 3 

group by d."StartTime", b."SubstituteTeacher")

select * from events where seq = 1 ;

答案 1 :(得分:1)

此功能在Ramda中称为path

答案 2 :(得分:0)

伊恩·霍夫曼·希克斯(Jan Hoffman-Hicks)精湛的crocks库具有一个功能完全可以实现

import propPathOr from 'crocks/helpers/propPathOr'
const getC = propPathOr(null, ['a', 'b', '0', 'c'])
getC({ a: { b: [{ c: 'gotcha!' }] } }) === 'gotcha!' // true