0

I have below array which is present inside an object and need to update only index 0 from "ABC" to "XYZ".

const input = { 
arr: ["ABC", "FGD", "PQR", "TYS"]
};

const desiredOutput = {
arr: ["XYZ", "FGD", "PQR", "TYS"]
 }
Vickram
  • 187
  • 11
  • `const desiredOurput = { ...input, arr: ['XYZ', ...input.arr.slice(1)] }` – secan Jan 22 '21 at 14:56
  • It is possible to the same thing for array of object present inside in javascript. For Ex const input = { leftColumn: [ { key: "name", label: "Javascript Topic Name", type: "string", }, { key: "id", label: "Javasctipt Topic Id", type: "string", }, ]} and const desiredOutput = {leftColumn: [ { key: "name", label: "React Topic Name", type: "string", }, { key: "id",label:React Topic Id", type: "string", }, ]}. Only changing the label value for array of object – Vickram Jan 22 '21 at 15:05
  • sorry but I do not understand whether your last comment is a question and, if it is a question, what it is that you are asking; could you rephrase, please? – secan Jan 22 '21 at 15:30
  • Yeah last comment is an question. Whether we can perform the same way with array of object. I am finding little bit tricky – Vickram Jan 22 '21 at 16:23
  • in terms of syntax, you can do it and you will get no error but the result would be that if later on in your code you change, for example, `desiredOutput.leftColumn[1].key = 'foo'` also `input.leftColumn[1].key` will be changed. – secan Jan 22 '21 at 17:00

0 Answers0