Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

How update the nested property in object (i.e Object also containing array of object) in javascript?

I have below mock data based on object property need to update their respective value using spread operator. I am not able to do that because it is nested.

I am able to achieve using below approach but as you can see code line is getting increased. But we can definitely reduce use spread operator. Because in "title" property i am only updating 0 index and in left column only updating the label value rest of the thing is same.

const mockData = {
  title: ["Javascript", "Selected Topic"],
  leftColumn: [
    {
      key: "name",
      label: "Javascript Topic Name",
      type: "string",
    },
    {
      key: "id",
      label: "Javasctipt Topic Id",
      type: "string",
    },
  ],
};

const handleType = (val) => {
  switch (val.type) {
    case "Javascript":
      return {
        ...mockData,
      };
    case "Angular":
      return {
        ...mockData,
        title: ["Angular", "Selected Topic"],
        leftColumn: [
          {
            key: "name",
            label: "Angular Topic Name",
            type: "string",
          },
          {
            key: "id",
            label: "Angular Topic Id",
            type: "string",
          },
        ],
      };
    case "React":
      return {
        ...mockData,
        title: ["React", "Selected Topic"],
        leftColumn: [
          {
            key: "name",
            label: "React Topic Name",
            type: "string",
          },
          {
            key: "id",
            label: "Reacr Topic Id",
            type: "string",
          },
        ],
      };
  }
};

i am trying something below approach but not getting desired result

const handleType = (val) => {
  const newArray = [mockData];
  console.log("newArray", newArray);
  //   const index = mockData.findIndex((ele) => ele);
  //   console.log("index", index);
  //   newArray["title"][0] = "React";
  //   console.log("newArray ==>", newArray);
  switch (val.type) {
    case "Javascript":
      return {
        ...mockData,
      };
    case "Angular":
      mockData.title[0] = "Angular";
      return mockData.leftColumn.map((val, index) => {
          let value = { ...val, label: "Angular",};
          return Object.assign({}, value);
      });
    case "React":
      return {
        ...mockData,
        title: ["React", "Selected Topic"],
        leftColumn: [
          {
            key: "name",
            label: "React Topic Name",
            type: "string",
          },
          {
            key: "id",
            label: "Reacr Topic Id",
            type: "string",
          },
        ],
      };
  }
};

const type = {
  type: "Angular",
};

console.log("Angular", handleType(type));
question from:https://stackoverflow.com/questions/65844124/how-update-the-nested-property-in-object-i-e-object-also-containing-array-of-ob

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...