0

How does the following Javascript replace method work ?

records.replace(index, 1, this.get(`model.data.${index}`))

I have normally seen JS replace() with 2 parameters, but not sure how does 3 parameters work ?

records is an array of objects (for individual table row rendering)

7
  • What do you mean by three parameters, replace function signature is this str.replace(regexp|substr, newSubstr|function) Commented Feb 18, 2019 at 12:39
  • 3
    What is records? Commented Feb 18, 2019 at 12:39
  • records is an array of objects (for individual table row rendering) Commented Feb 18, 2019 at 12:40
  • As TKoL asked, what is records? String? Array? Something completely different? Give us come context, without more information this is difficult to answer. Commented Feb 18, 2019 at 12:40
  • That is very odd, arrays in JavaScript do not have a replace method by default. What libraries/frameworks are you using? Commented Feb 18, 2019 at 12:43

2 Answers 2

1

I have normally seen JS replace() with 2 parameters

The replace method you've seen was probably the string's one.

My educated guess is that records is not a string, then.

So, you should check what's records actually is. The method is not part of JS language, so it's either records an Object defined by the dev, or is extending the prototype of some built-in type (brrr).

To me, it looks like this method shared similarity with array's splice

If records is an array, on an array-like object, the developer could actually added replace as alias of splice, maybe because it seemed to him more clear about what it does (again, I'm just guessing, I don't have enough data).

If my guess is correct, this line would replace in the records array / array-like, 1 element, at the index pointed by index, with the value returned by this.get(…).

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

4 Comments

the replace method you've seen was probably the string's one. even that accepts only two parameters mate not three, if you pass the third one with comma it doesn't have any impact
@CodeManiac Yes, that's what I'm saying, probably I wasn't clear enough :) The OP said I have normally seen JS replace() with 2 parameters, so I replied the replace method you've seen was probably the string's one. (that, in fact, takes 2 parameters)
@ZERO well yeah with the amount of data provided in question it's not clear, but to me that line doesn't make sense so i pointed out. P.s:- i didn't downvoted :)
@CodeManiac it might be my english but I don't understand what doesn't make sense on that sentence: the OP said that have seen only replace methods with two args till now, and I said that probably the replace method he seen was the String ones, since the string's replace takes two args. :) You said that "it doesn't make sense" since String's replace takes only two parameters, but that's exactly what I'm also saying, so I honestly don't understand. P.S. I don't care about the downvote anyway, np :)
1

Considering that you are using Ember.js as stated in a comment, I would assume that you are referring to Ember.js' replace method. It takes three parameters: index to start replacing at, number of elements that should be removed from the array, and finally an array of elements to add to your array.

For more information, check this out.

Comments

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.