Home > Enterprise >  Javascript Replacing nested array of objects with updated object
Javascript Replacing nested array of objects with updated object

Time:09-27

I have 2 array of objects. I'd like to update/replace the nested object "comments" of collection1 with the matching object of collection2 where collection1.comments._id === collection2.cid in the same order.

collection1: [
  0:  {
        "_id": "6104844e42c23e6d215651cd",
        "comments": [
            {
                "_id": "6143e24273c10e4658852063",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Beautiful Day Outside"
            },
            {
                "_id": "6143e24673c10e4658852065",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Let us go for a picnic"
            },
            {
                "_id": "6145d58519a1d70d89512c9c",
                "user": "6144eef7d01acc2a77f4219c",
                "comment": "taking time to smell the flowers"
            }...
        ]
    },
 1:   {
        "_id": "6104842e42c23e6d215651ca",
        "comments": [
            {
                "_id": "61472dab0224a10e11aa45f8",
                "user": "6144eef7d01acc2a77f4219c",
                "comment": "Baking cookies for the party"
            },
            {
                "_id": "61472ecb9c2ece100a525c55",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Listening to the waves by the shore"
            }......
        ]
    }
]

collection2: [
  0:  {
        "cid": "6143e24273c10e4658852063",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "mom",
        "lastName": "mom",
        "comment": "Beautiful Day Outsite"
    },
  1:  {
        "cid": "6143e24673c10e4658852065",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "mom",
        "lastName": "mom",
        "comment": "Let us go for a picnic"
    },
  2:  {
        "cid": "61472dab0224a10e11aa45f8",
        "uid": "6144eef7d01acc2a77f4219c",
        "firstName": "james",
        "lastName": "james",
        "comment": "Baking cookies for the party"
    },
  3:  {
        "cid": "61472ecb9c2ece100a525c55",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "james",
        "lastName": "james",
        "comment": "Listening to the waves by the shore"
    },
    ...
]

I've tried the map function:

 collection1.map(obj => collection2.find(o => o.cid === obj.comments._id) || obj);

But I get: TypeError: Cannot read property '_id' of undefined. I'm not sure what to do next. Any assistance will be greatly appreciated.

CodePudding user response:

This is relatively easily implemented with a recursive function that uses Object.entries on your source, and tries to assign values "in parallel" to your target:

function setDataFrom(source, target) {
  for (const [key, val] of Object.entries(source)) {
    if (val !== null && typeof val === `object`) {
      if (target[key] === undefined) {
        target[key] = {};
      }
      setDataFrom(val, target[key]);
    } else {
      target[key] = val;
    }
  }
  return target;
}

And that gets you a deep copy for free, too:

function copyFromSource(source) {
  return setDataFrom(source, {});
}

CodePudding user response:

You could map through the comments and use find to check where the id matches.

const collection1 = [{
    "_id": "6104844e42c23e6d215651cd",
    "comments": [{
        "_id": "6143e24273c10e4658852063",
        "user": "6138b154e4c6a30dc5661da7",
        "comment": "Beautiful Day Outside"
      },
      {
        "_id": "6143e24673c10e4658852065",
        "user": "6138b154e4c6a30dc5661da7",
        "comment": "Let us go for a picnic"
      },
      {
        "_id": "6145d58519a1d70d89512c9c",
        "user": "6144eef7d01acc2a77f4219c",
        "comment": "taking time to smell the flowers"
      }
    ]
  },
  {
    "_id": "6104842e42c23e6d215651ca",
    "comments": [{
        "_id": "61472dab0224a10e11aa45f8",
        "user": "6144eef7d01acc2a77f4219c",
        "comment": "Baking cookies for the party"
      },
      {
        "_id": "61472ecb9c2ece100a525c55",
        "user": "6138b154e4c6a30dc5661da7",
        "comment": "Listening to the waves by the shore"
      }
    ]
  }
]

const collection2 = [{
    "cid": "6143e24273c10e4658852063",
    "uid": "6138b154e4c6a30dc5661da7",
    "firstName": "mom",
    "lastName": "mom",
    "comment": "Beautiful Day Outsite"
  },
  {
    "cid": "6143e24673c10e4658852065",
    "uid": "6138b154e4c6a30dc5661da7",
    "firstName": "mom",
    "lastName": "mom",
    "comment": "Let us go for a picnic"
  },
  {
    "cid": "61472dab0224a10e11aa45f8",
    "uid": "6144eef7d01acc2a77f4219c",
    "firstName": "james",
    "lastName": "james",
    "comment": "Baking cookies for the party"
  },
  {
    "cid": "61472ecb9c2ece100a525c55",
    "uid": "6138b154e4c6a30dc5661da7",
    "firstName": "james",
    "lastName": "james",
    "comment": "Listening to the waves by the shore"
  },
]

const result = collection1.flatMap((c) => c.comments).map((c) => {
  return {
    commentPart1: c,
    commentPart2: collection2.find((x) => x.cid === c._id)
  }
});

console.log(result);

CodePudding user response:

const collection1 = [
  {
        "_id": "6104844e42c23e6d215651cd",
        "comments": [
            {
                "_id": "6143e24273c10e4658852063",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Beautiful Day Outside"
            },
            {
                "_id": "6143e24673c10e4658852065",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Let us go for a picnic"
            },
            {
                "_id": "6145d58519a1d70d89512c9c",
                "user": "6144eef7d01acc2a77f4219c",
                "comment": "taking time to smell the flowers"
            }
        ]
    },



     {
        "_id": "6104842e42c23e6d215651ca",
        "comments": [
            {
                "_id": "61472dab0224a10e11aa45f8",
                "user": "6144eef7d01acc2a77f4219c",
                "comment": "Baking cookies for the party"
            },
            {
                "_id": "61472ecb9c2ece100a525c55",
                "user": "6138b154e4c6a30dc5661da7",
                "comment": "Listening to the waves by the shore"
            }
        ]
    }
]

const collection2 = [
  {
        "cid": "6143e24273c10e4658852063",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "mom",
        "lastName": "mom",
        "comment": "Beautiful Day Outsite"
    },
  {
        "cid": "6143e24673c10e4658852065",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "mom",
        "lastName": "mom",
        "comment": "Let us go for a picnic"
    },
  {
        "cid": "61472dab0224a10e11aa45f8",
        "uid": "6144eef7d01acc2a77f4219c",
        "firstName": "james",
        "lastName": "james",
        "comment": "Baking cookies for the party"
    },
  {
        "cid": "61472ecb9c2ece100a525c55",
        "uid": "6138b154e4c6a30dc5661da7",
        "firstName": "james",
        "lastName": "james",
        "comment": "Listening to the waves by the shore"
    }
]


const updatedCollection1 = collection1.map(col1 => {
  const updatedComments = col1.comments.map((col1_comment => {
      const matched_comment = collection2.find(el => el.cid === col1_comment._id)
      return matched_comment
  }))
  return {...col1, comments: updatedComments}
})

console.log(updatedCollection1)

Array.prototype.map() approach can be as follows :

const updatedCollection1 = collection1.map(col1 => {
       const updatedComments = col1.comments.map((col1_comment => {
           const matched_comment = collection2.find(el => el.cid === col1_comment._id)
           return matched_comment
       }))
       return {...col1, comments: updatedComments}
   })
  • Related