admin管理员组文章数量:1327064
i want to add an object into another object using typescript.
below is data1
data1 = {firstDetails: {...}}
data1 = {
firstDetails: {
id: '1',
name: 'first'
description: 'description'
//other fields,
}
}
and there is data2 like below
data2 {secondDetails: {...}}
const data2 = {
secondDetails: {
id: '1',
items: [],
values: [],
}
}
now i want to remove property id from secondDetails and then take items and values from data2 and add it to data1.
so the expected output is like below
{
firstDetails: {
id: '1',
name: 'first',
description: 'description',
items: [],
values: [],
}
}
i have tried like below,
const formattedData2 = formattedData2.secondDetails;
const without_id = omit(formattedData2, 'id')
data1.firstDetails = Object.assign(data1.firstDetails, without_id);
but this gives error in dat1.firstDetails in the last line. it says variable data has implicitly any type.
could someone help me fix this. how can i put one object into another using typescript. thanks.
i want to add an object into another object using typescript.
below is data1
data1 = {firstDetails: {...}}
data1 = {
firstDetails: {
id: '1',
name: 'first'
description: 'description'
//other fields,
}
}
and there is data2 like below
data2 {secondDetails: {...}}
const data2 = {
secondDetails: {
id: '1',
items: [],
values: [],
}
}
now i want to remove property id from secondDetails and then take items and values from data2 and add it to data1.
so the expected output is like below
{
firstDetails: {
id: '1',
name: 'first',
description: 'description',
items: [],
values: [],
}
}
i have tried like below,
const formattedData2 = formattedData2.secondDetails;
const without_id = omit(formattedData2, 'id')
data1.firstDetails = Object.assign(data1.firstDetails, without_id);
but this gives error in dat1.firstDetails in the last line. it says variable data has implicitly any type.
could someone help me fix this. how can i put one object into another using typescript. thanks.
Share Improve this question edited May 3, 2022 at 18:26 natalia asked May 3, 2022 at 17:58 natalianatalia 531 silver badge9 bronze badges 03 Answers
Reset to default 5Use destructuring with rest on second details. This way, you wont include the id from second
data1 = {
firstDetails: {
id: "1",
name: "first",
description: "description",
},
};
const data2 = {
secondDetails: {
id: "2",
items: [],
values: [],
},
};
const { id, ...restSecondDetails } = data2.secondDetails;
data1.firstDetails = { ...data1.firstDetails, ...restSecondDetails };
console.log(data1)
Using spread
and delete
operator will help you here an example
delete formattedData2.secondDetails.id;
const object = {...data1.firstDetails, ...formattedData2.secondDetails}
If you want to push the property to the first object user;
Object.assign(data1.firstDetails, data2.secondDetails)
There's multiple ways to do this. I wouldn't use delete
as this es with unnecessary overhead.
A simple general way to do this would be as follows
const binedObject = firstDetails
Object.keys(secondDetails).forEach(key => {
if (key !== 'id') {
binedObject[key] = secondDetails[key]
}
})
本文标签: javascriptHow to add another object to an object using typescriptStack Overflow
版权声明:本文标题:javascript - How to add another object to an object using typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213815a2434198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论