Home > database >  Adding elements within Object TypeScript
Adding elements within Object TypeScript

Time:08-11

Hello everyone i have dynamically expanding object which consists of a 2 x dimensional array. I try to add in field label:{ } new key:value {"hg":"Hg"} object. object JSON:

    [
        {
            "callbackQueryData": "tNLQy3VcX",
            "method": {
                "value": "",
                "property": "",
                "linkUrl": "",
                "inside": null,
                "type": "NEXT_PAGE",
                "nextId": "Cll8xZbVo6",
                "validation": null,
                "calendar": null,
                "condition": null,
                "api": null,
                "pagination": null
            },
            "label": {
                "en": "New button"
            }
        },
        {
            "callbackQueryData": "qntufUVhz",
            "label": {
                "en": "New button"
            },
            "method": {
                "value": "",
                "property": "",
                "linkUrl": "",
                "inside": null,
                "type": "NEXT_PAGE",
                "nextId": "",
                "validation": null,
                "calendar": null,
                "condition": null,
                "api": null,
                "pagination": null
            }
        }
    ],
    [
        {
            "callbackQueryData": "cx46ECYG9",
            "label": {
                "en": "New button"
            },
            "method": {
                "value": "",
                "property": "",
                "linkUrl": "",
                "inside": null,
                "type": "NEXT_PAGE",
                "nextId": "",
                "validation": null,
                "calendar": null,
                "condition": null,
                "api": null,
                "pagination": null
            }
        }
    ],
    [
        {
            "callbackQueryData": "uHp5yd3Li",
            "label": {
                "en": "New button"
            },
            "method": {
                "value": "",
                "property": "",
                "linkUrl": "",
                "inside": null,
                "type": "NEXT_PAGE",
                "nextId": "",
                "validation": null,
                "calendar": null,
                "condition": null,
                "api": null,
                "pagination": null
            }
        }
    ],
    []
]

I try cast it to simple array and via forEach() addressing everyone elements button:any and to add object which i need. But Spread syntax(...) can't find argument forEach().Or I'm doing it completely wrong.

let arrayButton:IBotButton[][] = ([] as IBotButton[][]).concat(...page.callbacks)//create simple array

arrayButton.forEach((button:any)=>{
      ...button, 
      label: { ...button.label, [languageSelect]: buttonText }
    })

CodePudding user response:

Assume your arrayButton looks like this:

const arrayButton: IBotButton[][] = [
  [
    {
      label: { "en": "new Button-1" }
      // ...
    },
    {
      label: { "en": "new Button-2" }
      // ...
    },
    //...
  ],
  [
    //...
  ]
];

And have these variables:

const languageSelect: string = "jp";
const buttonText: string = "new Button-3";

Then you could modify the label by following code:

// arrayButton is 2d array
arrayButton.forEach((array) => {
  // array is 1d array
  array.forEach((button, buttonIdx) => {
    array[buttonIdx] = {
      ...button,
      label: {
        ...button.label,
        [languageSelect]: buttonText,
      },
    };
  });
});
  • Related