[英]Remove Object from multidimensional Array
我正在嘗試刪除數組中的對象。
情況如下。
在代碼中,我生成了一個新的Post對象,它將推送到post_array
var field = "Kommentar";
var objectKey = 1;
var new_post_obj = {objectKey: objectKey, field: field, value: new_value, type: "text"};
post_array.push(new_post_obj);
那很好。 我不想在post_array中具有相同的objectKey和相同的字段的new_post_obj。 為此,我想刪除post_array中與我要發布的新對象具有相同objectKey和字段的發布對象。 然后推送new_post_obj。
例如,post_array具有以下數據:
[
{objectKey: "1", field: "Kommentar1", value: "kommentar123", type: "text"},
{objectKey: "1", field: "Color", value: "red", type: "text"}
]
我的new_post_obj有這個新數據
{objectKey: "1", field: "Kommentar1", value: "kommentar123456", type: "text"}
現在,我需要使用objectKey:1和字段:“ Kommentar1”刪除舊數據
我嘗試了以下代碼:
post_array = $.grep( post_array, function(n) {
return (n.field != field && n.objectKey != objectKey);
});
之后,我期望我的post_array僅具有以下值:
{objectKey: "2", field: "Color", value: "red", type: "text"}
但是它是空的。 我該如何解決?
您可以使用.filter
過濾掉先前具有相同objectKey
和field
:
const input = [ {objectKey: "1", field: "Kommentar1", value: "kommentar123", type: "text"}, {objectKey: "2", field: "Color", value: "red", type: "text"} ] const newObj = {objectKey: "1", field: "Kommentar1", value: "kommentar123456", type: "text"}; const output = input .filter(({ objectKey, field }) => objectKey !== newObj.objectKey && field !== newObj.field) .concat(newObj); console.log(output);
通常,從數組中刪除元素並不容易,除非它是最后一個元素(在這種情況下,您可以簡單地使用pop()
函數)。 這就是數組的工作方式。
刪除任意位置的元素的方法是創建另一個數組,該數組包含原始數組的所有元素,但要刪除的元素除外。 為此,一種簡單的方法是遍歷原始數組中的每個項目,將其與要刪除的項目進行比較,如果比較結果為假,則將其推入結果數組。
let original = [{objectKey: "1", field: "Kommentar1", value: "kommentar123", type: "text"},
{objectKey: "2", field: "Color", value: "red", type: "text"}];
let result = [];
for (element of original) {
if (!(element.objectKey === "1" && element.field === "Kommentar1")) {
result.push(element);
}
}
現在,您當然可以將其放入函數中,該函數將原始數組和刪除條件作為參數。 像deleteElement(arr, id, field)
,但是一旦您了解它的作用,就很容易實現。
已經有一個內置函數,它執行類似的操作(但功能更強大),稱為filter()
。 您可以在這里閱讀有關內容: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
您可以使用some(),
將新帖子推送到數組中之前檢查新帖子是否已包含some(),
例如
let posts = [
{ objectKey: "1", field: "1", value: "", type: "text"},
{ objectKey: "2", field: "2", value: "", type: "text"},
{ objectKey: "3", field: "3", value: "", type: "text"}
];
let newPost = { objectKey: "2", field: "2", value: "", type: "text"};
let isAlreadyInserted = posts.some((p) => {
return p.objectKey === newPost.objectKey && p.filter === newPost.filter;
});
if (!isAlreadyInserted) {
posts.push(newPost);
}
如果您要刪除具有相同字段的商品,則只需在grep中進行比較,即可將具有不同值的商品放入“ field”中
考慮到必須將Kommentar1
放在新字段中
var post_array = [ {objectKey: "1", field: "Kommentar1", value: "kommentar123", type: "text"}, {objectKey: "1", field: "Color", value: "red", type: "text"} ] var field = "Kommentar1" var objectKey = "1" post_array = $.grep( post_array, function(n) { return (n.field != field); }); console.log(post_array)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
擁有posts
和newPost
變量,您可以使用Array.prototype.reduce()和Spread語法在同一行中完成所有操作
post.reduce((a, c) => [...a, c.objectKey === newPost.objectKey && c.field === newPost.field ? c : newPost], []);
代碼示例:
const posts = [{objectKey: "1", field: "Kommentar1", value: "kommentar123", type: "text"}, {objectKey: "2",field: "Color", value: "red", type: "text"}]; const newPost = {objectKey: "1", field: "Kommentar1", value: "kommentar123456", type: "text"}; const result = posts.reduce((a, c) => [...a, c.objectKey === newPost.objectKey && c.field === newPost.field ? c : newPost], []); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.