Search and Replace Strings in an array of objects

To anyone reading this, I'm a beginner in JavaScript, and I would be grateful for any help regarding the JS transformer I am trying to write.

Basically, I have an array of objects that each have the same format of product information. Each object is a new part with a different manufacturing part number, or MPN (a string). Most of the MPN's are filled out correctly, but occasionally there will be one that is displayed in scientific notation instead( eg. instead of Displaying 100000, it will show 1.00000E5).
The objective of the Transformer is to go through all of the MPN's and look for the ones that contain a ' . ' as the second character and an ' E ' as the second last character. Then when those are found to replace them with the same thing, not in scientific notation.

Here's what I've got so far.

const data = {{new_bom_query.data.bom.parts}};//this is referencing the array without defining which index of the array
for (let index = 0; index < data.length; index++) {//iderates through all objects in the array
	const MPN = data[index].mpn;
  var re = new RegExp("/^.*(.)(?=.*E).*/gm");//not sure if this RegEx is correct
  if (re.test(MPN) = true) {
    MPN.replace(/[^0-9]/g, '');//if the test is true then delete everything thats not a number
  }   
}
return data

Any help would be greatly appreciated!

Would map() be a better solution here?

I was a bit terse earlier. I'm going to blame it on being triggered by thoughts of excel :rofl: No math here, just cleaning data, correct?

Upon further reading, it may not be idiomatic to use map() in this situation. It seems to work fwiw.

forEach might be the more appropriate choice. Something like this? Pardon my regex, it may be a little naive.

foreach

Hi Matt,

That seems like it should work, although when I tried to run it said that " data.forEach is not a function.

This Is what I did to try and solve this issue:

ah, yes. data must be a json string. I didn't notice that. Seems to work on my end too.

No Sorry, I meant i did this and then it still did not work. The error it showed said

Can you post a minimal example of the data structure you are working with? Be careful not to expose any sensitive information. :slight_smile:

Screen Shot 2022-03-04 at 12.33.09 PM


The first photo is how I access the array to display the table shown in the second photo. As you can see many of the MPN's are containing letters and numbers, but the bottom one is a scientific notation.

For some extra details, the query I'm using called new_ bom_query has one object in it called data, which has an object called bom, which has an array called parts, which contain hundreds of objects containing the individual mpn's

I see, your response, data within the transformer, is an object but forEach is an Array method. I probably should have noticed that earlier. I'm still learning.

You can run the forEach on data.bom.parts. Hopefully I didn't miss anything again. :crossed_fingers: