'Search for ID Key Value in Json Array and add Key with Values to Object array

I have Json array with two objects, in Json Array with two objects i have array with multiple objects in attachments.vaAttachments.

filename ist unique:

How can I search for specific values (attachments.vaAttachments.filename === 123456.pdf) if true add Key to object (name of key fileBase64) and value is "sahduidBASE64CODEsahuidhsauipdhasuiphd"

This is what i have now:

[{
"im": {
    "materialNumber": "A0009",
    "countryKey": "DE",
    "createdDate": "2022-03-09"
},
"attachments": {
    "vaAttachments": [
        {
            "filename": "123456.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z",

        },
        {
            "filename": "987654.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        }
    ]
}
},
{
"im": {
    "materialNumber": "A0010",
    "countryKey": "DE",
    "createdDate": "2022-03-09"
},
"attachments": {
    "vaAttachments": [
        {
            "filename": "656565.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        },
        {
            "filename": "753357.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        }
    ]
}
}]

This is what i would like to have:

[{
"im": {
    "materialNumber": "A0009",
    "countryKey": "DE",
    "createdDate": "2022-03-09"
},
"attachments": {
    "vaAttachments": [
        {
            "filename": "123456.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
            "fileBase64": "sahduidBASE64CODEsahuidhsauipdhasuiphd" 
        },
        {
            "filename": "987654.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        }
    ]
}
},
{
"im": {
    "materialNumber": "A0010",
    "countryKey": "DE",
    "createdDate": "2022-03-09"
},
"attachments": {
    "vaAttachments": [
        {
            "filename": "656565.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        },
        {
            "filename": "753357.pdf",
            "originalFilename": "Sample.pdf",
            "createdAt": "2022-03-09T14:50:55.325Z"
        }
    ]
}
}]


Solution 1:[1]

You can use array.forEach for the same, you can read about it in this link

Also here you can use this function for the same:

const searchFunction = (arr, key) => {
  arr.forEach((element) => {
    const {
      attachments: { vaAttachments = [] }
    } = element
    vaAttachments.forEach((item) => {
      const { filename = '' } = item
      if (filename === key) {
        item.fileBase64 = 'sahduidBASE64CODEsahuidhsauipdhasuiphd'
      }
    })
  })
}

searchFunction(jsonArray, '123456.pdf')
console.log('jsonArray:', JSON.stringify(jsonArray))

Try and running it yourself:

const jsonArray = [
  {
    im: {
      materialNumber: 'A0009',
      countryKey: 'DE',
      createdDate: '2022-03-09'
    },
    attachments: {
      vaAttachments: [
        {
          filename: '123456.pdf',
          originalFilename: 'Sample.pdf',
          createdAt: '2022-03-09T14:50:55.325Z'
        },
        {
          filename: '987654.pdf',
          originalFilename: 'Sample.pdf',
          createdAt: '2022-03-09T14:50:55.325Z'
        }
      ]
    }
  },
  {
    im: {
      materialNumber: 'A0010',
      countryKey: 'DE',
      createdDate: '2022-03-09'
    },
    attachments: {
      vaAttachments: [
        {
          filename: '656565.pdf',
          originalFilename: 'Sample.pdf',
          createdAt: '2022-03-09T14:50:55.325Z'
        },
        {
          filename: '753357.pdf',
          originalFilename: 'Sample.pdf',
          createdAt: '2022-03-09T14:50:55.325Z'
        }
      ]
    }
  }
]

const searchFunction = (arr, key) => {
  arr.forEach((element) => {
    const {
      attachments: { vaAttachments = [] }
    } = element
    vaAttachments.forEach((item) => {
      const { filename = '' } = item
      if (filename === key) {
        item.fileBase64 = 'sahduidBASE64CODEsahuidhsauipdhasuiphd'
      }
    })
  })
}

searchFunction(jsonArray, '123456.pdf')
console.log('jsonArray:', jsonArray)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1