3

I have a multidimentional array:

(3) [Array(1), Array(1), Array(1)]
0:["('idDocDetail','0','$createdBy')"]
1:["('idDocDetail','2','$createdBy'),('idDocDetail','4','$createdBy')"]
2:["('idDocDetail','0','$createdBy')"]

I need to replace the string value idDocDetail with the index number, like this.

(3) [Array(1), Array(1), Array(1)]
0:["('0','0','$createdBy')"]
1:["('1','2','$createdBy'),('1','4','$createdBy')"]
2:["('2','0','$createdBy')"]

I'm trying to use replace, but I got the replace is not a function error.

array.forEach(function(item, index) {
  return item.toString().replace('idDocDetail', index);
    });

what am I doing wrong? Replace is the right way to do this?

5 Answers 5

1

I do recommend you to learn to perform changes in immutable manner. This is where Array.prototype.map plays well

const data = [
  ["('idDocDetail','0','$createdBy')"],
  ["('idDocDetail','2','$createdBy'),('idDocDetail','4','$createdBy')"],
  ["('idDocDetail','0','$createdBy')"]
]

const modified = data.map((item, index) => 
   item.map(str => str.replace(/idDocDetail/g, index ))
)

modified.forEach(x => console.log(JSON.stringify(x)))

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

1 Comment

Thanks man, I use prototype.map to extract info from a table to that array, but I get stuck with the index replace. Thanks again!
1

Here, this works for your code structure. It uses map() to produce a new array by just replacing the string of interest with the index.

EDIT: Added a nested map for clarity + regular expression to find all instances of 'idDocDetail' in the string, not just the first one. replace method when given a raw string value only handles the first instance of a string occurring.

const array = [["('idDocDetail','0','$createdBy')"],
["('idDocDetail','2','$createdBy'),('idDocDetail','4','$createdBy')"],
["('idDocDetail','0','$createdBy')"]]

var find = 'idDocDetail';
var re = new RegExp(find, 'g');

let newArray = array.map((val, i) => val.map(string => {
  return string.replace(re, i)
}))

console.log(newArray)

3 Comments

You're loosing the embedded arrays
the second row still have the string I want to replace with the index
Code should be all set now
1

You can loop over your array and edit it.

let array = [
  ["('idDocDetail','0','$createdBy')"],
  ["('idDocDetail','2','$createdBy'),('idDocDetail','4','$createdBy')"],
  ["('idDocDetail','0','$createdBy')"],
];

array.forEach((e, i) => {
  array[i] = [e[0].replace(/idDocDetail/g, i)];
});

console.log(array);

1 Comment

the second row still have the string I want to replace with the index
0

You can not replace an item by calling a method on the item being replaced. Instead you need to call it on the array. You can do it this way:

for (var i=0; i<array.length; i++) {
    array[i][0] = i;
}

Comments

0

forEach ignores the return of the callback. You need to assign to the original array at the current index.

var array = [
  ["('idDocDetail','0','$createdBy')"],
  ["('idDocDetail','2','$createdBy'),('idDocDetail','4','$createdBy')"],
  ["('idDocDetail','0','$createdBy')"]
];

array.forEach(function(item, index) {
  array[index] = item.map(s => s.replace('idDocDetail', index));
});

console.log(array);

1 Comment

the second row still have the string 'idDocDetail'

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.