use*_*156 52 javascript arrays
我想检查一个数组是否包含"role".如果是这样,我想移动"role"到数组的前面.
var data= ["email","role","type","name"];
if ("role" in data) data.remove(data.indexOf("role")); data.unshift("role")
data;
Run Code Online (Sandbox Code Playgroud)
在这里,我得到了结果:
["role", "email", "role", "type", "name"]
我怎样才能解决这个问题?
Guf*_*ffa 50
您可以对数组进行排序,并指定值"role"在所有其他值之前,并且所有其他值都相等:
var first = "role";
data.sort(function(x,y){ return x == first ? -1 : y == first ? 1 : 0; });
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/Guffa/7ST24/
Dav*_*mas 45
我的第一个想法是:
var data= ["email","role","type","name"];
// if it's not there, or is already the first element (of index 0)
// then there's no point going further:
if (data.indexOf('role') > 0) {
// find the current index of 'role':
var index = data.indexOf('role');
// using splice to remove elements from the array, starting at
// the identified index, and affecting 1 element(s):
data.splice(index,1);
// putting the 'role' string back in the array:
data.unshift('role');
}
console.log(data);
Run Code Online (Sandbox Code Playgroud)
修改,整理一下:
if (data.indexOf('role') > 0) {
data.splice(data.indexOf('role'), 1);
data.unshift('role');
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
小智 41
在我看来,ES6中最干净的解决方案:
let data = ["email","role","type","name"];
data = data.filter(item => item !== "role");
data.unshift("role");
Run Code Online (Sandbox Code Playgroud)
Rea*_*eal 18
let data = [0, 1, 2, 3, 4, 5];
let index = 3;
data.unshift(data.splice(index, 1)[0]);
// data = [3, 0, 1, 2, 4, 5]
Run Code Online (Sandbox Code Playgroud)
小智 12
如果您不想更改现有数组,可以使用ES6解构和filter方法创建新副本,同时保持其他项的顺序.
const data = ["email", "role", "type", "name"];
const newData = ['role', ...data.filter(item => item !== 'role')];
Run Code Online (Sandbox Code Playgroud)
小智 8
如果需要,这是一个不变的解决方案:
const newData = [
data.find(item => item === 'role'),
...data.filter(item => item !== 'role'),
],
Run Code Online (Sandbox Code Playgroud)
类似于@Tandroid的答案,但更通用的解决方案:
const putItemsFirst = ({ findFunction, array }) => [
...array.filter(findFunction),
...array.filter(item => !findFunction(item)),
];
Run Code Online (Sandbox Code Playgroud)
可以这样使用
putItemsFirst({
array: ["email","role","type","name"],
findFunction: item => item === 'role',
})
Run Code Online (Sandbox Code Playgroud)
我最终使用的是与此类似的东西,
要检查数组中是否存在一个项目,您应该使用.includes()而不是in(正如这里已经指出的,in用于对象中的属性)。
此功能执行您正在寻找的内容:(从项目所在的位置移除项目并在前面阅读)
data = ["email","role","type","name"];
moveToFirst("role", data);
function moveToFirst( stringToMove, arrayIn ){
if ( arrayIn.includes(stringToMove) ){
let currentIndex = arrayIn.indexOf(stringToMove);
arrayIn.splice(currentIndex, 1);
arrayIn.unshift(stringToMove);
}
}
console.log(data);Run Code Online (Sandbox Code Playgroud)
小智 6
如果您有一组对象,您可以使用 splice 和 push 来移动起始索引。Splice 用数组中从所需索引开始的部分替换原始数组,并返回它删除的部分(索引之前的内容)您推送。
let friends = [{
id: 1,
name: "Sam",
},
{
id: 2,
name: "Steven",
},
{
id: 3,
name: "Tom",
},
{
id: 4,
name: "Nora",
},
{
id: 5,
name: "Jessy",
}
];
const tomsIndex = friends.findIndex(friend => friend.name == 'Tom');
friends.push(...friends.splice(0, tomsIndex));
console.log(friends);Run Code Online (Sandbox Code Playgroud)
我会选择这个 ES6 解决方案。它不会改变原始数组(考虑到它不是嵌套的),不会遍历数组(过滤器),并且您不仅限于移动数组项的第 0 个索引。
const moveArrayItem = (array, fromIndex, toIndex) => {
const arr = [...array];
arr.splice(toIndex, 0, ...arr.splice(fromIndex, 1));
return arr;
}
const arr = ["a", "b", "c", "d", "e", "f", "g"];
console.log(moveArrayItem(arr, 4, 0))
// [ 'e', 'a', 'b', 'c', 'd', 'f', 'g' ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43165 次 |
| 最近记录: |