javascript オブジェクト名 取得 - JavaScriptオブジェクトからプロパティを削除するにはどうすればいいですか?

JavaScript オブジェクト / javascript / javascript-objects

以下のようにオブジェクトを作成したとします。

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI",
  "regex": "^http://.*"
};
let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI"
};

Derek Wang



Answer #1

例えば

let object = { a: 1, b: 2, c: 3 };   
    const { a, ...rest } = object;
    object = rest;
    const propKey = 'a';
    const { [propKey]: propValue, ...rest } = object;
    object = rest;
    const removeProperty = (propKey, { [propKey]: propValue, ...rest }) => rest;

    object = removeProperty('a', object);
    const removeProperties = (object, ...keys) => Object.entries(object).reduce((prev, [key, value]) => ({...prev, ...(!keys.includes(key) && { [key]: value }) }), {})
object = removeProperties(object, 'a', 'b') //結果=> {c:3}
    const propsToRemove = ['a', 'b']
    object = removeProperties(object, ...propsToRemove) //結果=> {c:3}