I have two arrays of objects and I want to update one depending on other's propery value.
If driveTypeArray.multiAssign is true and driveTypeArray.type === drive.type, drive.ready should become true.
In array driveTypeArray there are two types with multiAssign: true and in results it gives me only one drive with ready:true
I tried with forEach but I think my logic is incorrect.
const driveTypeArray = [{
type: "HDD",
multiAssign: false,
amount: 0,
},
{
type: "SSD",
multiAssign: true,
amount: 0,
},
{
type: "EXT",
multiAssign: false,
amount: 0,
},
{
type: "NVMe",
multiAssign: true,
amount: 0,
},
]
const drives = [{
label: "SanDisk SDFJGKHR234234",
serial: "",
size: "255 GB",
type: "SSD",
ready: false,
inProgress: false,
progress: 0
},
{
label: "TOSHIBA HDWD130 - TOSHIBA PRODUCT",
serial: "",
size: "2.4 TB",
type: "HDD",
ready: false,
inProgress: false,
progress: 0
},
{
label: "SanDisk SDFJGKHR234234",
serial: "",
size: "255 GB",
type: "NVMe",
ready: false,
inProgress: false,
progress: 0
},
{
label: "TOSHIBA HDWD130 - TOSHIBA PRODUCT",
serial: "",
size: "2.4 TB",
type: "EXT",
ready: false,
inProgress: false,
progress: 0
},
]
let driveState = {};
driveTypeArray.map(dType =>
driveState = {
drives: drives.map(drive => (drive.type === dType.type ? {
...drive,
ready: true
} : drive))
}
)
console.log(driveState)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>