7

Consider following array in Javascript:

var array1 = ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];

Now I want to replace all the elements at once from index 3 to 9 in following way:

array1 = ['S', 'T', 'A', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'L', 'O', 'W'];

Is it possible to achieve in javascript ?

Note: I want to perform following operation using array only

3
  • 2
    array1.forEach((a,b,c)=>array1[b]=(b<9 && b>2)?'X':a); Commented Oct 31, 2021 at 5:47
  • @dandavis your boundaries are off, 9 is inclusive Commented Oct 31, 2021 at 5:50
  • touchè. it's late here, but the main point is closure-ing the object to be mutated (array1) in the iteration callback Commented Oct 31, 2021 at 5:51

4 Answers 4

10

Use Array.fill()

var array1 = ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];

array1.fill('X', 3, 10)

console.log(array1)

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

Comments

4

Use array splice() method

var array1= ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];
// At position 3, delete 7 and add 7 elements: 
array1.splice(3, 7, "X","X","X","X","X","X","X");

console.log(array1);

2 Comments

[].splice is a good way to mutate arrays, good call!
if you wanted to make this variable you could maybe do something like: array1.splice(start, end-start, ...Array(end-start).fill("X"));
1

One way is with Array.prototype.map:

This loops through every index of the array, and if the index is between 3 and 9 (inclusive), set it to 'X', otherwise keep it as the original chr (character)

var array1 = ['S', 'T', 'A', 'C', 'K', 'O', 'V', 'E', 'R', 'F', 'L', 'O', 'W'];

var array2 = array1.map((chr, idx) => 3 <= idx && idx <= 9 ? 'X' : chr);

console.log(array2);

5 Comments

worksish, but doesn't replace anything, it copies a new array.
Functional programming is built on the idea that data shouldn't be mutated directly, it should be copied then mutated
sure, but OP wants to "replace all the elements at once", keyword replace (at least to my reading)
@dandavis okay sure, then OP could just do array1 = array1.map()
@FivePlyPaper: but that doesn't replace anything in the array either, it creates a new one. array1 could have a different name in another part of the program... I agree OP could be clearer though...
0

It sure does.

const arr = ['a', 'b', 'c', 'd', 'e']

function replaceWithX(start, end) {
  for (let i = start; i <= end; i++) {
    arr[i] = 'x'
  }
}

replaceWithX(1, 3)

console.log(arr) // ["a", "x", "x", "x", "e"]

2 Comments

I think the point of this question was to not use a for loop since OP said: "Note: I want to perform following operation using array only"
Didn't see that and thanks for mention it. In that case array.splice() is a better choice.

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.