Reputation: 115
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 !
Upvotes: 2
Views: 93
Reputation: 4175
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; }
Upvotes: 2
Reputation: 92461
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)
Upvotes: 1
Reputation: 407
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/
Upvotes: 2
Reputation: 386858
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; }
Upvotes: 1