Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

javascript - 如何访问由一个字段链接的存储在数组对象中的两个数据?(How to access two data stored in array object that are linked by one field?)

I have a REST API from which i am fetching the json data and storing them in array.(我有一个REST API,可从中获取json数据并将其存储在数组中。)

The API looks like below:(该API如下所示:) [ { "id": "100", "name": "Person1", "number": "+91-8980439023" }, { "id": "102", "name": "Person2", "number": "+91-5980339023" }, { "id": "105", "name": "Person3", "number": "+91-8980439023" }, { "id": "101", "name": "Person4", "number": "+91-8980439023", "parent": "105" }, { "id": "110", "name": "Person5", "number": "+91-8980439023" }, { "id": "115", "name": "Person6", "number": "+91-9834295899", "parent": "100" } ] Some of the data have "parent" field.The value in the "parent" field is the "id" of the other data.(某些数据具有“父”字段。“父”字段中的值是其他数据的“ id”。) Now i want to store these data which have reference in the "parent" field of other data in a separate array.(现在,我想将在其他数据的“父”字段中引用的这些数据存储在单独的数组中。) For example: The data with id=101 has "parent" key with value 105 which in turn is the id of the 3rd data.So the data with id=105 should be stored in separate array.(例如:id = 101的数据具有“ parent”键,其值为105,这又是第三数据的id。因此id = 105的数据应存储在单独的数组中。) How can i do in a simple and scalable way?(如何以简单且可扩展的方式进行操作?)   ask by Pawan Patel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try(尝试)

let parents = data.filter(d=> data.some(c=> c.parent==d.id)); let data = [ { "id": "100", "name": "Person1", "number": "+91-8980439023" }, { "id": "102", "name": "Person2", "number": "+91-5980339023" }, { "id": "105", "name": "Person3", "number": "+91-8980439023" }, { "id": "101", "name": "Person4", "number": "+91-8980439023", "parent": "105" }, { "id": "110", "name": "Person5", "number": "+91-8980439023" }, { "id": "115", "name": "Person6", "number": "+91-9834295899", "parent": "100" } ]; let parents = data.filter(d=> data.some(c=> c.parent==d.id)); console.log(parents); To have O(2n) complexity first put parents id to some hash table and then filter(为了具有O(2n)复杂度,首先将父代id放在某个哈希表中,然后进行过滤) let data = [ { "id": "100", "name": "Person1", "number": "+91-8980439023" }, { "id": "102", "name": "Person2", "number": "+91-5980339023" }, { "id": "105", "name": "Person3", "number": "+91-8980439023" }, { "id": "101", "name": "Person4", "number": "+91-8980439023", "parent": "105" }, { "id": "110", "name": "Person5", "number": "+91-8980439023" }, { "id": "115", "name": "Person6", "number": "+91-9834295899", "parent": "100" } ]; let hash = {}; data.forEach(d=> hash[d.parent]=d); let parents = data.filter(d=> d.id in hash); console.log(parents);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...