gpt4 book ai didi

how to get parent id data from grandchild id data(如何从孙ID数据中获取父ID数据)

转载 作者:bug小助手 更新时间:2023-10-25 18:53:04 26 4
gpt4 key购买 nike



I have the people data below. where in people data there is "dataInduk" and inside "dataInduk" there is "dataAnak". how can I find people id data if I have "idAnak". this is my code

我有下面的数据。在人的数据中有“dataInduk”,在“dataInduk”中有“dataAnak”。如果我有“idAnak”,我怎么能找到人id数据。这是我的代码


const people = [
{
name: "James",
id: 1,
dataInduk: [
{
idInduk: "1a",
dataAnak: [
{
idAnak: "1a1",
},
{
idAnak: "1a2",
},
],
},
{
idInduk: "1b",
dataAnak: [
{
idAnak: "1b1",
},
{
idAnak: "1b2",
},
],
},
],
},
{
name: "John",
id: 2,
dataInduk: [
{
idInduk: "2a",
dataAnak: [
{
idAnak: "2a1",
},
{
idAnak: "2a2",
},
],
},
{
idInduk: "2b",
dataAnak: [
{
idAnak: "2b1",
},
{
idAnak: "2b2",
},
],
},
],
},
];

for example:
when i find from idAnak = '2b2' then show id "2" from people

例如:当我找到from idAnak=‘2b2’时,则显示来自People的id“2


更多回答

the data is just 3 layer deep right? Can different idInduk or people have the same idAnak?

数据只有3层深,对吗?不同的idInduk或人可以拥有相同的idAnak吗?

@kennarddh Yes, all the id are different

@kennarddh是的,所有ID都不同

@Quinn: Did any answer work for you? If yes, accept and/or upvote.

@Quinn:有什么答案对你有效吗?如果是,接受和/或赞成票。

优秀答案推荐

We can find the person by iterating dataInduk and iterate dataAnak in every dataInduk to check if dataInduk includes idAnak

我们可以通过迭代dataInduk和迭代每个dataInduk中的dataAnak来找到Person,以检查dataInduk是否包括idAnak





const IDAnakToIDPeople = idAnak => {
const person = people.find(personIter =>
personIter.dataInduk.some(dataInduk =>
dataInduk.dataAnak.some(dataAnak => dataAnak.idAnak === idAnak),
),
)

return person.id
}

// Test cases (actual, expected)
console.log(IDAnakToIDPeople("1a1"), 1)
console.log(IDAnakToIDPeople("1a2"), 1)
console.log(IDAnakToIDPeople("1b1"), 1)
console.log(IDAnakToIDPeople("1b2"), 1)
console.log(IDAnakToIDPeople("2a1"), 2)
console.log(IDAnakToIDPeople("2a2"), 2)
console.log(IDAnakToIDPeople("2b1"), 2)
console.log(IDAnakToIDPeople("2b2"), 2)

<script>
const people = [
{
name: "James",
id: 1,
dataInduk: [
{
idInduk: "1a",
dataAnak: [
{
idAnak: "1a1",
},
{
idAnak: "1a2",
},
],
},
{
idInduk: "1b",
dataAnak: [
{
idAnak: "1b1",
},
{
idAnak: "1b2",
},
],
},
],
},
{
name: "John",
id: 2,
dataInduk: [
{
idInduk: "2a",
dataAnak: [
{
idAnak: "2a1",
},
{
idAnak: "2a2",
},
],
},
{
idInduk: "2b",
dataAnak: [
{
idAnak: "2b1",
},
{
idAnak: "2b2",
},
],
},
],
},
];
</script>




References




One liner:

一条班轮:




const people=[{name:"James",id:1,dataInduk:[{idInduk:"1a",dataAnak:[{idAnak:"1a1"},{idAnak:"1a2"}]},{idInduk:"1b",dataAnak:[{idAnak:"1b1"},{idAnak:"1b2"}]}]},{name:"John",id:2,dataInduk:[{idInduk:"2a",dataAnak:[{idAnak:"2a1"},{idAnak:"2a2"}]},{idInduk:"2b",dataAnak:[{idAnak:"2b1"},{idAnak:"2b2"}]}]}];

console.log(
people.filter(e =>
e.dataInduk.some(j => j.dataAnak.some(k => k.idAnak === '2b2')))
?.map(i => i.id)
);




更多回答

How is this any different from an answer already posted?

这与已经发布的答案有什么不同?

Return only 1 id because idAnak is always unique.

只返回1个id,因为idAnak始终是唯一的。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com