简体   繁体   English

如何使用 JavaScript 对包含父对象和子对象的数组进行排序?

[英]How to sort an array with parents and children objects with JavaScript?

I have an array with object, which I need to sort in a such way that first the parent object should appear, and then its children objects, and so on.我有一个包含 object 的数组,我需要对它进行排序,首先应该出现父 object,然后是其子对象,依此类推。 However, when I try to find index of a parent object in array in order to push the children object after it, the findIndex() method returns -1.但是,当我尝试在数组中查找父 object 的索引以便将子 object 推到它之后时,findIndex() 方法返回 -1。 Can somebody point to the root of this problem, as I cannot clearly see why it does that.有人可以指出这个问题的根源,因为我无法清楚地看到它为什么会这样做。 The code and data array that I'm using is written below.我正在使用的代码和数据数组写在下面。

 const data = [ { "_id": "0", "parent": null, "title": "All" }, { "_id": "61c0a9cb8f67e811d55abb2d", "parent": null, "title": "Electronics" }, { "_id": "61c0a9cb8f67e811d55abb2e", "parent": { "_id": "61c0a9cb8f67e811d55abb2d" }, "title": "Phones" }, { "_id": "61c0a9cb8f67e811d55abb2f", "parent": { "_id": "61c0a9cb8f67e811d55abb2d" }, "title": "Laptops" }, { "_id": "61c0a9cb8f67e811d55abb30", "parent": { "_id": "61c0a9cb8f67e811d55abb2d" }, "title": "TVs" }, { "_id": "61c0a9cb8f67e811d55abb31", "parent": null, "title": "Literature" }, { "_id": "61c0a9cb8f67e811d55abb32", "parent": { "_id": "61c0a9cb8f67e811d55abb31" }, "title": "Study Literature" }, { "_id": "61c0a9cb8f67e811d55abb33", "parent": { "_id": "61c0a9cb8f67e811d55abb31" }, "title": "Fictional Literature" }, { "_id": "61c0a9cb8f67e811d55abb34", "parent": { "_id": "61c0a9cb8f67e811d55abb31" }, "title": "Comic books" }, { "_id": "61c0a9cb8f67e811d55abb35", "parent": { "_id": "61c0a9cb8f67e811d55abb2e" }, "title": "Smartphones" }, { "_id": "61c0a9cb8f67e811d55abb36", "parent": { "_id": "61c0a9cb8f67e811d55abb35" }, "title": "Accessories" } ]; let parents= []; data.forEach(element => { if(element.parent == null ) { parents.push(element); } else { let parentId = element.parent._id; let index = parents.findIndex(item => { return item._id == parentId; }); parents.splice(index+1, 0, element); }; });

Using item => {parentId == item._id;} does require a 'return' to be used: item => {return parentId == item._id;} without the return the functions is basically item => null ;使用item => {parentId == item._id;}确实需要使用“return”: item => {return parentId == item._id;}没有返回功能基本上是item => null which is than seen as false by .findIndex() resulting in a -1 .findIndex()将其视为错误,导致 -1

If you use the arrow function without the curly braces a return is implied (But limits you to single line expressions as a trade-off): item => parentId == item._id如果您使用没有大括号的箭头 function 则暗示返回(但将您限制为单行表达式作为权衡): item => parentId == item._id

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions来源: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM