[英]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.