ramda中是否有任何功能如何通过嵌套键值找到键?我找到了一种如何在数组中找到对象的方法,但这并没有帮助。我需要这样的东西:
const obj = {
addCompany: {
mutationId: '1'
},
addUser: {
mutationId: '2'
},
addCompany: {
mutationId: '3'
}
}
const findByMutationId = R.???
findByMutationId('2', obj) // returns addUser
答案 0 :(得分:0)
对ramda不确定,但如果普通js中的单行有利于你,以下内容将起作用
const obj = {
addCompany: {
mutationId: '1'
},
addUser: {
mutationId: '2'
}
};
let found = Object.keys(obj).find(e => obj[e].mutationId === '2');
console.log(found);
答案 1 :(得分:0)
如果您使用了这个,那么您可能需要填充条目,如果您需要密钥和值,这很好
const obj = {
addCompany: {
mutationId: '1'
},
addUser: {
mutationId: '2'
},
addCompany: {
mutationId: '3'
}
}
let [key, val] = Object.entries(obj).find(obj => obj[1].mutationId == '2')
console.log(key)
console.log(val)

答案 2 :(得分:0)
find
与propEq
和keys
结合使用
const obj = {
addCompany: {
mutationId: '1'
},
addUser: {
mutationId: '2'
},
addCompany2: {
mutationId: '3'
}
}
const findByMutationId = id => obj => R.find(
R.o(R.propEq('mutationId', id), R.flip(R.prop)(obj)),
R.keys(obj)
)
console.log(findByMutationId('2')(obj))

<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>
&#13;
lulz
的无点版本
const obj = {
addUser: {
mutationId: '2'
},
addCompany: {
mutationId: '3'
}
}
const findByMutationId = R.compose(
R.o(R.head),
R.o(R.__, R.toPairs),
R.find,
R.o(R.__, R.nth(1)),
R.propEq('mutationId')
)
console.log(findByMutationId('2')(obj))
const findByMutationId2 = R.compose(
R.ap(R.__, R.keys),
R.o(R.find),
R.o(R.__, R.flip(R.prop)),
R.o,
R.propEq('mutationId')
)
console.log(findByMutationId2('3')(obj))
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>
&#13;