简体   繁体   English

如何获取一个 object 的值并将它们放入另一个 object

[英]How can I take a values of one object and put them into another object

Hey guys I have the following setback, I am trying to write on empty values of an object already filled another value so I can use them later, I have defined the object and I am trying to reassign the values with the new one after I receive them from API call, the problem is that the object that I am receiving is the same as the one from the API call and I don't need that, I am in need of the same structure that I have created, for this to happen I am using useState.嘿伙计们,我遇到了以下挫折,我正在尝试在 object 的空值上写入已经填充了另一个值的空值,因此我可以稍后使用它们,我已经定义了 object,并且我正在尝试在收到后用新值重新分配值它们来自 API 调用,问题是我收到的 object 与来自 API 调用的相同,我不需要创建这个结构,我需要相同的结构,我正在使用 useState。 Could you point me in the right direction?你能指出我正确的方向吗? That is my code and after it will be the result I am always getting那是我的代码,之后我总是得到结果

The initial state of deliveryAddressInput before API call在 API 调用之前的 deliveryAddressInput 的初始 state

const [deliveryAddressInput, setDeliveryAddressInput] = useState<NewAddressDetails>({});

The result after the call of the API is setDeliveryAddressInput(adress): API调用后的结果是setDeliveryAddressInput(adress):

BuildingName: "18A"
BuildingNumber: ""
City: "Houghton Le Spring"
Line1: "18A Nesham Place"
Line2: ""
Line3: ""
PostalCode: "DH5 8AG"

Initial state of the mapped object from the top one:从顶部开始映射的 object 的初始 state:

const [newAddress, setNewAddress] = useState<{}>({
    firstName: firstNameInput,
    lastName: lastNameInput,
    houseNo: deliveryAddressInput.BuildingNumber,
    houseName: deliveryAddressInput.BuildingName,
    street: deliveryAddressInput.Line1,
    addressLine2: deliveryAddressInput.Line2,
    town: deliveryAddressInput.City,
    postCode: deliveryAddressInput.PostalCode,
  });

The useState function:使用状态function:

useEffect(() => {
    setTimeout(() => {
      setNewAddress(deliveryAddressInput);
    });
  }, [deliveryAddressInput]);

And the final result is the same as the call from the API:最终结果与来自 API 的调用相同:

BuildingName: "18A"
    BuildingNumber: ""
    City: "Houghton Le Spring"
    Line1: "18A Nesham Place"
    Line2: ""
    Line3: ""
    PostalCode: "DH5 8AG"

I am trying to receive the following which is mapping the values of the API call to the new object keys:我正在尝试接收以下内容,它将 API 调用的值映射到新的 object 键:

        firstName: "Jane",
        lastName: "Smith",
        houseNo: "",
        houseName: "18A",
        street: 18A Nesham Place,
        addressLine2: "",
        town: "Houghton Le Spring",
        postCode: "DH5 8AG",

I suppose you should convert one object into another:我想您应该将一个 object 转换为另一个:

    const convertDeliveryInputIntoAddress = (
      deliveryAddressInput, firstName, lastName
      ) => {
        const {BuildingName, BuildingNumber, City, Line1, PostalCode} = deliveryAddressInput;
        const firstLineSplitted = Line1.split(' ');
        const houseName = firstLineSplitted[0];
        const street = firstLineSplitted.slice(1).join(' ');
        return {firstName, lastName, houseNo: buildingNumber, houseName, street, town: City, postCode: PostalCode};
      }

    ...
    setNewAddress(convertDeliveryInputIntoAddress(deliveryAddressInput, firstName, lastName));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数组中的 object 中获取值并将它们放入列中的数组中? - how do i take values from an object inside an array and put them inside a array in column? 如何从一个数组中提取数组并将它们转换为对象并将它们放入一个新数组中? - how can i extract arrays from one array & convert them in object and put them in a new array? 如何获取输入的所有值并将它们放在我的对象(localStorage)中的正确位置? - How can I get all the values ​of the inputs and put them in the right place in my object (localStorage)? 如何从多个 arrays 中获取每个 object 并将它们全部放入一个数组中 - How can I get each object from multiple arrays and put them all into one array 如何获取一个大对象,将该子对象用作同一控制器的多个实例的数据,并使它们保持同步? - How can I take one large object, use a sub-object of that as the data for multiple instances of the same controller, and keep them both in sync? 我如何将一个对象的属性值分配给另一个对象? - How i can assign values to the properties of one object to another? 获取变量值并将它们放在另一个文件中 - Take variable values and put them in another file 如何将一个对象映射到另一个对象? - How can I map an Object into another one? 如何将对象数组放入另一个对象? - How can I put an array of objects into another object? 如何将对象中的值分组并将它们推送到 JS 中的数组中? - How can I group the values in the object and push them in an array in JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM