In this loop object.otherarray.array
you use id
property to to ensure to have necessary match. So I assume this item.fields.image.sys.id === asset.sys.id
always should have only one match. And it resulted in the following code:
object.array.forEach((item) => {
item.fileds.image.fields = object.otherarray.array.filter(
(asset) => item.fields.image.sys.id === asset.sys.id
)[0].fields;
});
Some explanation:
object.otherarray.array.filter(
(asset) => item.fields.image.sys.id === asset.sys.id
)[0];
is an equivalent of your item.fields.image.sys.id === asset.sys.id
. In other words, firstly we find necessary asset. Then when we have it we can use fields
property to assign it to item.fileds.image.fields
.
I also have another variant of my remake:
object.array.forEach((item) => {
const imageId = item.fields.image.sys.id;
item.fileds.image.fields = object.otherarray.array
.filter((asset) => imageId === asset.sys.id)
[0].fields;
});
While it is more verbose version, it is more clear and structive so I would recommend to use this one.
I am open to your comments as I suppose I could not understand your correctly.
Edit: as @MarcRohloff said, you can use find
instead of filter
. But note, that `find has poor support in browsers:
object.array.forEach((item) => {
const imageId = item.fields.image.sys.id;
item.fileds.image.fields = object.otherarray.array
.find((asset) => imageId === asset.sys.id)
.fields;
});
object.array.forEach((item) => { item.fileds.image.fields = object.otherarray.array.filter( (asset) => item.fields.image.sys.id === asset.sys.id )[0].fields; });
orobject.array.forEach((item) => { const imageId = item.fields.image.sys.id; item.fileds.image.fields = object.otherarray.array .filter((asset) => imageId === asset.sys.id)[0].fields; });
\$\endgroup\$