我有 2 个数组。 首先看起来像:
const arrOjObjects = [
{text: "HUF", value: 300.3803314317},
{text: "GBP", value: 0.7925201485},
{text: "IDR", value: 14127.9996377796},
{text: "ILS", value: 3.5020374898},
{text: "DKK", value: 6.7633795164}]
第二个看起来像:
const arr = ["ILS, "DKK"]
我需要按某种规则对arrOjObjects进行排序,如果对象文本的元素与数组arr的元素匹配,则该对象必须位于第一个,所有其他对象位于其后面
我需要一些排序规则,它将我的 arr 排序为这种状态:
const arrOjObjects = [
{text: "ILS", value: 3.5020374898}, // ILS is in arr
{text: "DKK", value: 6.7633795164} // DKK is in arr
{text: "HUF", value: 300.3803314317},
{text: "GBP", value: 0.7925201485},
{text: "IDR", value: 14127.9996377796},
]
感谢您的解答!
最佳答案
您可以使用Array.prototype.sort
const arrOjObjects = [
{text: "HUF", value: 300.3803314317},
{text: "GBP", value: 0.7925201485},
{text: "IDR", value: 14127.9996377796},
{text: "ILS", value: 3.5020374898},
{text: "DKK", value: 6.7633795164}]
const arr = ["ILS", "DKK"];
arrOjObjects.sort((a, b) => arr.includes(b.text) - arr.includes(a.text));
console.log(arrOjObjects);
关于javascript - 根据与另一个数组的重合对对象数组进行排序 JS REACT REDUX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58371385/