JavaScript Replace the value at a specific index (array) replace specific value in pandas dataframe,replace specific value in

Good evening everyone,

I have an array which contains 24 characters

"hours": [
        "1",
        "1",
        "1",
        "1",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0",
        "0"
    ] 

I would like to search through another array of indexes; for example [2, 5, 9, 10]

Then, go to the positions(of the first array) (2, 5, 9 and 10) of the first array and change the character to this position by a "1", and move the rest of the array to "0"

Can someone guide me?

Thank you !

Answer:1

A solution:

var hours = ["1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
    positions = [2, 5, 9, 10];

for (var i = 0; i < hours.length; i++) {
  hours[i] = positions.includes(i) ? "1" : "0";   
}

console.log(hours);

// ["0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"]

Demo: https://jsfiddle.net/gzynud0c/1/


As pointed out in the comments, the above solution works in all major browsers, including IE9+, without using a transpiler like Babel or using a polyfill.

The following works in all browsers.

for (var i = 0; i < hours.length; i++) {
  hours[i] = ~positions.indexOf(i) ? "1" : "0";   
}

Demo: https://jsfiddle.net/s59jt870/3/

Answer:2

With a sorted indices array, you could take an index j for it and check the looping index i with that value and change the indices array index and return either '1' or '0'.

var hours = ["1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
    indices = [2, 5, 9, 10],
    replaced = hours.map((j => (v, i) => indices[j] === i && ++j ? '1' : '0')(0));

console.log(replaced.join(' '));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...

I'm using JSX everywhere in my React project because of course I am. But there are a few instances where using createElement is particularly helpful. Here's one such example, which is representative ...

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

I have found similar to my issue here. But I have a little bit different scenario. I have string of html rather than just string. So, I wanted to do: Let's suppose MyComponent is just returning h3 ...

  1. convert react component to html
  2. convert react component to pdf
  3. convert react component to typescript
  4. convert react component to image
  5. convert react component to string
  6. convert react component to vue
  7. convert react component to angular
  8. convert react component to functional component
  9. convert react component to react native
  10. convert react component to web component
  11. convert react component to hooks
  12. convert react component to png
  13. convert react component to json
  14. convert react component to javascript
  15. convert react component to dom element
  16. convert react component to element
  17. convert react component to npm package

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

I did some research on how to remove logo which removes the Vimeo logo on the bottom right corner. But I want to remove the user logo and author from embedded vimeo video. Is this possible? If yes how ...

  1. vimeo hide user

I have a pretty straightforward page in WordPress, which works fine outside the WordPress framework, but once I integrate it into a WordPress theme it refuses to re-size a element. The rest of the ...

I have a pretty straightforward page in WordPress, which works fine outside the WordPress framework, but once I integrate it into a WordPress theme it refuses to re-size a element. The rest of the ...

  1. dynamic image resizing javascript
  2. javascript dynamic resizing