我想要检查一个数组是否包含"role"
。如果有,我想把"role"
移到数组的最前面。
var data= ["email","role","type","name"];
if ("role" in data) data.remove(data.indexOf("role")); data.unshift("role")
data;
这里是我的结果:
["role", "email", "role", "type", "name"]
我该怎么解决这个问题?
我想要检查一个数组是否包含"role"
。如果有,我想把"role"
移到数组的最前面。
var data= ["email","role","type","name"];
if ("role" in data) data.remove(data.indexOf("role")); data.unshift("role")
data;
这里是我的结果:
["role", "email", "role", "type", "name"]
我该怎么解决这个问题?
你可以对数组进行排序,并指定值"role"
在所有其他值之前,而所有其他值都相等:
var first = "role";
data.sort(function(x,y){ return x == first ? -1 : y == first ? 1 : 0; });
在我看来,ES6中最简洁的解决方案:
let data = ["email","role","type","name"];
data = data.filter(item => item !== "role");
data.unshift("role");
role
已经存在,保留其他元素,然后强制在开头添加一个硬编码的 "role"。 - Hugolpzlet data = [0, 1, 2, 3, 4, 5];
let index = 3;
data.unshift(data.splice(index, 1)[0]);
// data = [3, 0, 1, 2, 4, 5]
data.findIndex(value)
替换index
),都可以使用它。 - goodvibration我的第一个想法是:
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);
进行修订和整理:
if (data.indexOf('role') > 0) {
data.splice(data.indexOf('role'), 1);
data.unshift('role');
}
参考文献:
Array.indexOf()
函数,因为这意味着它会对整个数组进行两次搜索。const roleIndex = data.indexOf('role');
if (roleIndex > 0) {
data.splice(roleIndex, 1);
data.unshift('role');
}
修改后的代码如下所示:const roleIndex = data.indexOf('role');
if (roleIndex > 0) {
data.splice(roleIndex, 1);
data.unshift('role');
}
- digiwanddata.splice
returns just removed part, which you can immediately use to insert, making it 1 line instead of 2, and also making it useful for more complicated cases like for an array of objects: data.unshift(data.splice(roleIndex, 1)[0])
- Maxim Georgievskiy如果需要,这里有一个不可变的解决方案:
const newData = [
data.find(item => item === 'role'),
...data.filter(item => item !== 'role'),
],
if ("role" in data)
。我的回答不够详细,但它隐含着在更新数组之前需要进行检查的要求。 - Maldororconst sortedCards = [...data.filter((item) => item === 'NORI'),...cards.filter((item) => item !== 'NORI')];
- Ramy El-Basyouni如果您不想更改现有数组,可以使用ES6解构和filter方法创建一个新副本,同时保持其他项的顺序。
const data = ["email", "role", "type", "name"];
const newData = ['role', ...data.filter(item => item !== 'role')];
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);
.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);
const putItemsFirst = ({ findFunction, array }) => [
...array.filter(findFunction),
...array.filter(item => !findFunction(item)),
];
可以像这样使用
putItemsFirst({
array: ["email","role","type","name"],
findFunction: item => item === 'role',
})
我最终使用的是类似于这样的东西,
我会选择这个ES6的方式。它不会改变原始数组(只考虑没有嵌套的情况),也不会遍历数组(filter),你不仅仅只能使用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' ]
remove
方法是什么?你是在使用Prototype.js或类似的东西吗? - Bergiif('role' in data)
可以翻译为:如果数据中包含“角色”。data.remove()
可以翻译为:删除数据。 - jraede