0

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"
        }
    ]
}
}]

1 Answer 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)

Sign up to request clarification or add additional context in comments.

8 Comments

There's one thing I don't like about this: The function returns a value, which makes it look as if it's a clone of the original structure when in fact the original has been mutated. I think that's a bad style of doing things, since other developers might write code on the assumption that jsonArray has not changed. You should either (a) mutate the original and return nothing or (b) return a deep clone with the required change.
@DavidKnipe I have updated the confusing part, now i am mutating the original array so that people wont get confused about the mutation part.
try running the code, i have already mentioned in the code for adding key and value pairs. If you run it, you can see the fileBase64 will get added in 123456.pdf filename.
@Ashishssoni Tnx a lot! Can you pls tell me what is solution if i only have one Json object not array like: { 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' } ] } },
@user1862965 there is no need to use await since its a simple synchronous function, but if you want to use it anyway then you can do await searchFunction(jsonArray, '123456.pdf') or if you want run multiple functions you can do something like await Promise.all([ searchFunction(jsonArray, '123456.pdf'), searchFunction(jsonArray, '656565.pdf') ]).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.