เรามีอาร์เรย์ของอ็อบเจ็กต์ที่มีสองอ็อบเจ็กต์ และเราจำเป็นต้องรวมออบเจ็กต์ทั้งสองเป็นหนึ่งเดียวและกำจัดคุณสมบัติ chk ทั้งหมด -
const err = [
{
"chk" : true,
"name": "test"
},
{
"chk" :true,
"post": "test"
}
]; ขั้นตอนที่ 1 − การรวมวัตถุให้เป็นวัตถุเดียว
const errObj = Object.assign(...err);
ขั้นตอนที่ 2 − การลบคุณสมบัติ chk
delete errObj['chk']; console.log(errObj);
ให้เราดูโค้ดทั้งหมดที่มีเอาต์พุต -
ตัวอย่าง
const err = [
{
"chk" : true,
"name": "test"
},
{
"chk" :true,
"post": "test"
}
];
const errObj = Object.assign(...err);
delete errObj['chk'];
console.log(errObj); ผลลัพธ์
เอาต์พุตในคอนโซลจะเป็น -
{ name: 'test', post: 'test' }