How to correctly delete an object from an array by name?

Igor Shvets

How do I delete the created object "bird" from an array by name ie by the keyword "bird"?

var storage = [
    {cat: {name: "Garfild", count: 3443, price: 1000}}
];

function addProduct(newProduct) {
    storage.push(newProduct);
}

addProduct({dog: {name: "Rex", count: 1488, price: 2000}});
addProduct({bird: {name: "Eagle", count: 4042, price: 3000}});

function deleteProductByName(productName) {
    storage.remove(productName);
}
deleteProductByName("bird");

Nina Scholz

You could iterate from the end of the array and splice the found item, which uses a check if the property exists in the object.

function addProduct(newProduct) {
    storage.push(newProduct);
}

function deleteProductByName(productName) {
    var i = storage.length;
    while (i--) {
        if (productName in storage[i]) {
            storage.splice(i, 1);
        }
    }
}

var storage = [{ cat: { name: "Garfild", count: 3443, price: 1000 } }];

addProduct({ dog: { name: "Rex", count: 1488, price: 2000 } });
addProduct({ bird: { name: "Eagle", count: 4042, price: 3000 } });

deleteProductByName("bird");
console.log(storage);
.as-console-wrapper { max-height: 100% !important; top: 0; }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Delete an object from array React

Delete last object from the array of objects.

Delete keys whose name contains certain characters from an object (Coffeescript)

Delete keys whose name contains certain characters from an object (Coffeescript)

How to delete an object from vector<ptr*>?

how to call variable name in array of object in React

How can I get the key name and its value from an array within a JSON object

How to delete an element from an array in C#

How to delete items from array in .txt C

How to delete negative duplicates from an array?

How to delete particular no of elements from array in mongodb

how to delete array value from table in jquery

Delete a object from array upto given date javascript

How to correctly pass object by type from C# to VBScript?

Delete a member array correctly in C++

How to delete one character/letter from file name

Instantiate JS Object that take array of arguments from String name

How to delete a value from a dynamic array from any position in VBA?

How to access Array Object without key name in HTML with Angular

How to identify object that is deleted from a collection (array)?

How to separate the difference from an array object?

How to return matched document from Array of object

How to correctly mock an ObjectMapper object

How to correctly mock an ObjectMapper object

How to delete object in json

How to grab object id from UITableView and delete object based on object id?

How to delete items from an array that have an index that is a multiple of N

How do you delete a multidimentional array from localstorage on button click?

How to define object in array in Mongoose schema correctly with 2d geo index

TOP 一覧

  1. 1

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  2. 2

    どのように関係なく、それがどのように「悪い」、すべてのSSL証明書でのHttpClientを使用しないように

  3. 3

    Modbus Python Schneider PM5300

  4. 4

    Ansibleで複数行のシェルスクリプトを実行する方法

  5. 5

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  6. 6

    System.Data.OracleClient.OracleException:ORA-06550:行1、列7:

  7. 7

    インデックス作成時のドキュメントの順序は、Elasticsearchの検索パフォーマンスを向上させますか?

  8. 8

    scala.xmlノードを正しく比較する方法は?

  9. 9

    NGX-ブートストラップ:ドロップダウンに選択したアイテムが表示されない

  10. 10

    Elasticsearch - あいまい検索は、提案を与えていません

  11. 11

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  12. 12

    Elasticsearchの場合、間隔を空けた単語を使用したワイルドカード検索

  13. 13

    Elasticsearchでサーバー操作を最適化:低いディスク透かしに対処する

  14. 14

    ラベルとエントリがpythontkinterに表示されないのはなぜですか?

  15. 15

    変数値を含むElasticSearch検索結果

  16. 16

    グラフ(.PNG)ファイルをエクスポートするZabbix

  17. 17

    STSでループプロセス「クラスパス通知の送信」のループを停止する方法

  18. 18

    Audacity:プロジェクトではなく、サウンドファイルのみを保存します

  19. 19

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  20. 20

    Excelは、メモ帳データの複数の列を1つの列として解釈します

  21. 21

    ブラウザがHTMLテンプレートを解釈しない

ホットタグ

アーカイブ