Javascript Replacing nested array of objects with updated object

SeekerC

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.

ABDULLOKH MUKHAMMADJONOB

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}
   })

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Last Object in Array Replacing All Other Objects

Array of Objects vs Nested Object?

Create nested object from array of objects in JavaScript

Convert Array of Objects to Nested Object in Javascript

Finding a path to target object in nested array of objects in Javascript

object dynamic filtering with nested array of objects using javascript

Replacing a function deeply nested inside an object in javascript

Transform nested array of objects to an object

Turn Object of Nested Objects into an Object of Array Nested Objects

Filtering a nested array with objects by checking another nested array with object - JavaScript

Comparing two array of objects and replacing an object with another

JavaScript: Comparing arrays of objects with nested array of object

typescript/javascript remove object partial repetition in nested array of objects

Javascript Object with Array of objects

Converting an object of nested objects / arrays into something array like in javascript

Sorting an array object by nested values within objects - Javascript

how to convert objects in array which is already in a nested object in javaScript

Filter array of nested objects based on nested object

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to get array object in nested array of array of objects javascript

Javascript iterate through nested Object and return transformed and renamed Array of objects

change nested array object to objects by value type in javascript

Find object from an array of nested objects by key in JavaScript

Nested object in Javascript array

Proccessing nested object in an array of objects?

javascript group a nested array of objects by child object value

Replacing an one of the objects in an array of objects in javaScript

How to transform nested object of objects into array of objects

replacing and object inside an existing array of objects