gpt4 book ai didi

javascript - jquery如何按文本输入对数组进行排序

转载 作者:行者123 更新时间:2023-11-28 17:25:42 25 4
gpt4 key购买 nike

我想根据用户输入对数组中的数据进行排序。请按照下面的示例进行操作。我必须做什么?

数组数据示例

0: Array
0: "1"
1: "text1"
2: "text2"
3: "text3"
1: Array
0: "2"
1: "text1"
2: "text2"
3: "text3"
2: Array
0: "3"
1: "text1"
2: "text2"
3: "text3"

文本输入:3, 1, 2

结果

0: Array
0: "3"
1: "text1"
2: "text2"
3: "text3"
1: Array
0: "1"
1: "text1"
2: "text2"
3: "text3"
2: Array
0: "2"
1: "text1"
2: "text2"
3: "text3"

最佳答案

    let yourObj = {0:{ 
0: "1",
1: "text1",
2: "text2",
3: "text3"},
1: {
0: "2",
1: "text1",
2: "text2",
3: "text3"},
2: {
0: "3",
1: "text1",
2: "text2",
3: "text3"}
},
input = [3,2,1];

let sortArr = (input) => {

let obj = {};

for(let i = 0; i< input.length; i++){
obj[i] = yourObj[--input[i]];

}
return obj;
}

console.log(sortArr(input)) //Call sort array here

关于javascript - jquery如何按文本输入对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51668930/

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