2

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 !

4
  • Have you at least tried to get it working yourself? Commented Aug 19, 2018 at 19:28
  • Please provide a better example of the second array, and how do you get 2, 5, 9 and 10 from this second array? Commented Aug 19, 2018 at 19:32
  • You know that JS have bit operators, do you? Commented Aug 19, 2018 at 19:54
  • Yes for sure Martin, I tried for (var i = 0; i < hours.length; i++) { if(i === positions[i]) { hours[i] = "1" } else { hours[i] = "0" } } the problem is that after, indexArray went to undefined:/ Commented Aug 19, 2018 at 20:44

4 Answers 4

2

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/

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

5 Comments

+1 because unless the OP is using a transpiler like Babel or polyfills for ES6 features, this is the only answer that will work in IE (9+).
I'm guessing by the experience level of the question, the chances of the OP using ES6 are slim. I've updated my answer to include a cross-browser solution, too.
Remember, searching each index in the lookup array will be performed for each index of the actual array and for those where the indexes will be missing it will search the entire lookup array.
Totally agreed... but the "entire lookup array" will only ever be 24 long. If it were unlimited, I'd be more concerned.
lookup array length is 5 and actual array lengthis 24, so approx 24*5, assume a 50*20 and not bigger one. so 1000 isn't it? where it can be achieved by only 50+10 i.ie 60 operations
2

I feel the most simple approach here will be to set all the values of hours array (mutate or not depends on requirement) to "0" first, and then visit only those locations we have to set "1" and set it there!

indices.reduce((h,v)=>(h[v]="1") && h, hours.map(()=>"0"))

let 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];

let res = indices.reduce((h,v)=>(h[v]="1") && h, hours.map(()=>"0"));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

5 Comments

Nice! I'd say shortest not simplest :)
this will just perform minimal number of operations as well!
@Miguelit0 why not?
@Koushik Chatterjee Oh no no it's perfect as a solution! It's just that I'm not yet at your level! he he he he
This is the most efficient answer for sure. However, Miguelit0 and other users will need to be using ES6 features to use those arrow functions otherwise they won't work in any version of IE. And reduce() only works in >IE9. I'm guessing Miguelit0 is not yet using ES6. Excellent answer though +1
1

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; }

Comments

1

It sounds like you want to look at all the items in the first item and forEach item if it's index in it in the array of index's change the value to 1 otherwise 0, right?. You can almost translate the sentence into code:

let 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" ]
let indexes = [2, 5, 9, 10]

hours.forEach((item, index, self) => self[index] = indexes.includes(index) ? '1' : '0' )

console.log(hours)

This mutates the original array. If you wanted a new array you could do something similar with map():

let 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" ]
let indexes = [2, 5, 9, 10]

// leave hours as is and create a new array
let newArray = hours.map((item, index, self) =>  indexes.includes(index) ? '1' : '0' )

console.log(newArray)

If your arrays are very large, this won't be efficient since you loop through the indexes array with every iteration. If that's the case you can convert your indexes array to something like set that allows for constant time lookups. Alternatively you could just start with an array of zeros and set the ones:

let 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" ]
let indexes = [2, 5, 9, 10]

let newHours = indexes.reduce((a, index) => (a[index] = '1', a), Array.from(hours).fill("0"))

console.log(newHours)

1 Comment

Thank you ! It gives me a good idea of ​​how to work with the map

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.