gpt4 book ai didi

javascript - 通过比较具有不同键值对的对象对 Javascript 数组进行排序

转载 作者:行者123 更新时间:2023-11-29 23:08:25 24 4
gpt4 key购买 nike

我正在尝试对波纹管数组进行排序:

var joins = [
{
"joinType": "INNER JOIN",
"joinTableName": "country",
"joinColumnName": "id",
"foreignTableName": "state",
"foreignColumnName": "country_id",
"index": 1
},
{
"joinType": "INNER JOIN",
"joinTableName": "state",
"joinColumnName": "id",
"foreignTableName": "city",
"foreignColumnName": "state_id",
"index": 2
},
{
"joinType": "INNER JOIN",
"joinTableName": "city",
"joinColumnName": "id",
"foreignTableName": "address",
"foreignColumnName": "city_id",
"index": 3
},
{
"joinType": "INNER JOIN",
"joinTableName": "address",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "address_id",
"index": 4
},
{
"joinType": "INNER JOIN",
"joinTableName": "user_status",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "status_id",
"index": 5
}
]

使用下面的代码:

joins.sort((a, b) => {
if (a.foreignTableName === b.joinTableName) return 1; //b comes first
else if (a.joinTableName === b.foreignTableName) return -1; //a comes first
else return 0; //no change
});

结果是:

[
{
"joinType": "INNER JOIN",
"joinTableName": "state",
"joinColumnName": "id",
"foreignTableName": "city",
"foreignColumnName": "state_id",
"index": 2
},
{
"joinType": "INNER JOIN",
"joinTableName": "country",
"joinColumnName": "id",
"foreignTableName": "state",
"foreignColumnName": "country_id",
"index": 1
},
{
"joinType": "INNER JOIN",
"joinTableName": "address",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "address_id",
"index": 4
},
{
"joinType": "INNER JOIN",
"joinTableName": "city",
"joinColumnName": "id",
"foreignTableName": "address",
"foreignColumnName": "city_id",
"index": 3
},
{
"joinType": "INNER JOIN",
"joinTableName": "user_status",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "status_id",
"index": 5
}
]

这不是我所期望的——我希望索引为 2 和 1 的元素出现在索引为 3 的元素之后。有什么问题吗?

补充一点,这是从对象字段定义表创建MySql查询语句,该表使用另一个生产系统底层DB的数据来定义业务对象的字段。以上部分用于创建 JOIN 子句。

附言,这是我想要的:

[
{
"joinType": "INNER JOIN",
"joinTableName": "address",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "address_id",
"index": 4
},
{
"joinType": "INNER JOIN",
"joinTableName": "city",
"joinColumnName": "id",
"foreignTableName": "address",
"foreignColumnName": "city_id",
"index": 3
},
{
"joinType": "INNER JOIN",
"joinTableName": "state",
"joinColumnName": "id",
"foreignTableName": "city",
"foreignColumnName": "state_id",
"index": 2
},
{
"joinType": "INNER JOIN",
"joinTableName": "country",
"joinColumnName": "id",
"foreignTableName": "state",
"foreignColumnName": "country_id",
"index": 1
},
{
"joinType": "INNER JOIN",
"joinTableName": "user_status",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "status_id",
"index": 5
}
]

最佳答案

这里有一种方法,首先为每个对象分配一个 level,该级别由对象的根祖先的 index 定义level of deep 对象在它所属的链表中。添加这个新的 lvl 属性后,我们就可以使用这个新属性进行排序了。我不相信这种方法会在性能上如此出色,但也许可以满足您的需求。

var joins = [
{
"joinType": "INNER JOIN",
"joinTableName": "country",
"joinColumnName": "id",
"foreignTableName": "state",
"foreignColumnName": "country_id",
"index": 1
},
{
"joinType": "INNER JOIN",
"joinTableName": "state",
"joinColumnName": "id",
"foreignTableName": "city",
"foreignColumnName": "state_id",
"index": 2
},
{
"joinType": "INNER JOIN",
"joinTableName": "city",
"joinColumnName": "id",
"foreignTableName": "address",
"foreignColumnName": "city_id",
"index": 3
},
{
"joinType": "INNER JOIN",
"joinTableName": "address",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "address_id",
"index": 4
},
{
"joinType": "INNER JOIN",
"joinTableName": "user_status",
"joinColumnName": "id",
"foreignTableName": "user",
"foreignColumnName": "status_id",
"index": 5
}
];

// Find the object with the table a foreign key is referencing.

const findParent = (fTable) => joins.find(x => x.joinTableName === fTable);

// Recursive method that assigns a level to an object based on the position
// they have on the linked list they belong to.

const getLevel = (fTable, index, lvl) =>
{
let parent = findParent(fTable);
return (fTable && parent) ?
getLevel(parent.foreignTableName, parent.index, lvl + 1) :
index + "-" + lvl;
}

// Maps the input data to adds the level property to each object.

let newInput = joins.map(obj =>
{
obj.lvl = getLevel(obj.foreignTableName, obj.index, 0);
return obj;
});

// Sorts the new generated data based on the level property. Since the
// lvl property is a string, we use "localeCompare()" to compare.

let sortedInput = newInput.sort((a, b) => a.lvl.localeCompare(b.lvl));

// Shows the sorted data.

console.log(sortedInput);

关于javascript - 通过比较具有不同键值对的对象对 Javascript 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54330178/

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