JavaScript-匹配数组中的对象并删除或添加
我试图从数组中查找并删除一个对象,或者根据它是否已经存在将其推送到数组中。我尝试了一个for-if循环和forEach循环,但似乎无法破解它。以下是我目前掌握的情况:JavaScript-匹配数组中的对象并删除或添加,javascript,arrays,vue.js,Javascript,Arrays,Vue.js,我试图从数组中查找并删除一个对象,或者根据它是否已经存在将其推送到数组中。我尝试了一个for-if循环和forEach循环,但似乎无法破解它。以下是我目前掌握的情况: // object in store to be modified this.sorts = [ { field: "title", direction: "asc" }, { field: "value", direction: "asc" }, // re
// object in store to be modified
this.sorts = [
{ field: "title", direction: "asc" },
{ field: "value", direction: "asc" }, // remove if exists, add if not
{ field: "quality", direction: "asc" },
];
<button @click="handleCheckbox('value', 'asc')">Value</button>; // example
handleCheckbox(field, dir) {
this.sorts.forEach((field, i) => {
if (this.sorts[i].field === field) {
this.sorts = this.sorts.splice(i, 1); // remove if passed field is found in array
console.log("deleted=", this.sorts[i]);
return;
} else {
this.sorts.push({ field: field, direction: dir }); // add if passed field is not found
console.log("pushed=", this.sorts[i]);
return;
}
});
// state.commit("setSorts", this.sorts);
}
您可以使用findIndex,然后将对象推入相应的数组中
var arr = [
{ field: "title", direction: "asc" },
{ field: "value", direction: "asc" }, // remove if exists, add if not
{ field: "quality", direction: "asc" },
];
function findObject(obj, value) {
var index = arr.findIndex(function(item) {
if (item.field === value) {
return true;
}
});
if (!index) {
arr.push(obj);
}
}
findObject({ field: "value", direction: "asc" }, 'value');
您可以使用findIndex,然后将对象推入相应的数组中
var arr = [
{ field: "title", direction: "asc" },
{ field: "value", direction: "asc" }, // remove if exists, add if not
{ field: "quality", direction: "asc" },
];
function findObject(obj, value) {
var index = arr.findIndex(function(item) {
if (item.field === value) {
return true;
}
});
if (!index) {
arr.push(obj);
}
}
findObject({ field: "value", direction: "asc" }, 'value');
可以使用findIndex获取对象的索引,该对象的字段作为传递给函数的字段参数,并根据索引进行推送或拼接。 下面是一个使用vanillaJs的解决方案的可能实现 变量排序=[ {字段:标题,方向:asc}, {字段:值,方向:asc}, {字段:质量,方向:asc}, ]; 函数句柄checkbox字段,方向{ var index=sorts.findIndexfunctionsort{ sort.field==字段 //测试检查:console.logsort.field,field }; //测试检查:console.logindex 如果索引<0{ 排序。推{字段,方向}; }否则{ 分类指数1; } }
可以使用findIndex获取对象的索引,该对象的字段作为传递给函数的字段参数,并根据索引进行推送或拼接。 下面是一个使用vanillaJs的解决方案的可能实现 变量排序=[ {字段:标题,方向:asc}, {字段:值,方向:asc}, {字段:质量,方向:asc}, ]; 函数句柄checkbox字段,方向{ var index=sorts.findIndexfunctionsort{ sort.field==字段 //测试检查:console.logsort.field,field }; //测试检查:console.logindex 如果索引<0{ 排序。推{字段,方向}; }否则{ 分类指数1; } } 试试这个:
let sorts = [
{ field: "title", direction: "asc" },
{ field: "value", direction: "asc" },
{ field: "quality", direction: "asc" },
];
function handleCheckbox(field, direction) {
let index = sorts.findIndex(sort => sort.field == field);
if (index === -1) {
sorts.push({ field: field, direction: direction });
return;
}
sorts.splice(index, 1);
}
handleCheckbox("value", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" } ];
handleCheckbox("new", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" }, { field: "new", direction: "asc" } ];
试试这个:
let sorts = [
{ field: "title", direction: "asc" },
{ field: "value", direction: "asc" },
{ field: "quality", direction: "asc" },
];
function handleCheckbox(field, direction) {
let index = sorts.findIndex(sort => sort.field == field);
if (index === -1) {
sorts.push({ field: field, direction: direction });
return;
}
sorts.splice(index, 1);
}
handleCheckbox("value", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" } ];
handleCheckbox("new", "asc");
console.log(sorts); // sorts = [ { field: "title", direction: "asc" }, { field: "quality", direction: "asc" }, { field: "new", direction: "asc" } ];
使用.findIndex查找条目。如果索引不是-1,则将其拼接出来;否则,推送新条目。使用.findIndex查找条目。如果索引不是-1,则将其拼接出来;否则,推送新条目。