Create arrays from 1D array after every nth occurrence using Angular JS - angularjs

I have an array which looks like this
Array[20]
0 : "2"
1 : "3"
2 : "4"
3 : "5"
4 : "6"
5 : "7"
6 : "8"
7 : "9"
8 : "10"
9 : "11"
10: "12"
11: "13"
12: "14"
13: "15"
14: "16"
15: "17"
16: "18"
17: "19"
18: "20"
19: "12"
Now I want to create arrays after every 4th occurrence like this
First Array
0 : "2"
1 : "6"
2 : "10"
3 : "14"
4 : "18"
Second Array
0 : "3"
1 : "7"
2 : "11"
3 : "15"
4 : "19"
and so on...
Yet I have written this code
for (var i = 0; i < $scope.data.effort.length; i = i+ 4) {
efforts.push( $scope.data.effort[i]);
};
From above code I am getting only first array what should I need to do to get remaining arrays. Please help

All you need is to run an extra loop outside that which handles your starting index. LIke this:
efforts = []
for (var j = 0; j < arr.length / 4; j++) {
efforts[j] = []
for (var i = j; i < arr.length; i = i + 4) {
efforts[j].push(arr[i]);
};
console.log(efforts[j])
}
Here's working example: (simplified in JS, without $scope and all)
var arr = [
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "12"
]
efforts = []
for (var j = 0; j < arr.length / 4; j++) {
efforts[j] = []
for (var i = j; i < arr.length; i = i + 4) {
efforts[j].push(arr[i]);
};
console.log(efforts[j])
}
Or, interestingly, you can use the beast, I mean, reduce to achieve the same. :)
var arr = [
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "12"
]
efforts = arr.reduce(function(arr, cur, curIndex) {
arr[curIndex % 4] = arr[curIndex % 4] || [];
arr[curIndex % 4].push(cur);
return arr
}, [])
console.log(efforts)

Another simple way with array filter:
var originalArray =["2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21"];
var firstArray = originalArray.filter((el, index) => (index%4 === 0));
var secondArray = originalArray.filter((el, index) => (index%4 === 1));
and so on

Related

Split the array and get value [React native]

I am developing the react native application and I want to split the array but I can't understand how to do that,
[{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
I have something like the above array, I want to get the value of the inTagId and also last integer value "3", "6", "7", "8" from this array
var a = [{"dtCreatedOn": "2021-06-01T03:28:21.450Z", "flgIsActive": true, "inTagId": 2, "stTags": "Song"}, "3", "6", "7", "8"]
var tagID = a[0].inTagId
var b = Object.keys(a)
var lastInteger = b[(b.length-1)]
var test = [{"dtCreatedOn": "2021-06-01T03:26:44.910Z", "flgIsActive": true, "inTagId": 1, "stTags": "Emotion"},"4","5","6"]
var tags = [];
test.map(function(ele) {
if(typeof ele === 'object')
{
tags.push(ele.inTagId);
}
else
{
tags.push(ele)
}
});
tags.join(',')

C printing values of array with pointer values to a char

typedef struct {
char* value;
char* type;
} CARD;
CARD cards[52];
void initializeCards() {
char types[4][10] = {"Spade", "Club", "Hearts", "Diamonds"};
char values[13][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
for (int i = 0; i < 4; i++) {
for (int e = 0; e < 13; e++) {
cards[i*13 + e].type = types[i];
cards[i*13 + e].value = values[e];
}
}
//This prints all sorts of weid characters.
for (int i=0; i<52; i++) {
printf("%s %s\n", cards[i].type, cards[i].value);
}
}//initializeCards.
I'm trying to make an array containing a deck of cards, but when I try to print the values
it prints all sorts of weird characters.
I tried using %s, %d, %c
I also tried using *cards[i].type or &cards[i].type
all without success.
What am I doing wrong?
Your array values doesn't have enough elements to store the terminating null-character, but you are trying to print that as strings (sequences of characters terminated by a null-character) later.
Allocate enough elements so that the array can hold terminating null-characters.
Wrong line:
char values[13][1] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};
Corrected:
char values[13][3] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};

Assigning values to every other index in an array

I have three arrays that correlate to one another (Users, Wins, and Lost), Wins[0] through Wins[2] stand for Users[0] through Users[2]. If Users[0] through Users[2] won and Users[3] as well as Users[4] lost, then Lost[3] and Lost[4] need to be equal to 1.
var Users = ["user1", "user2", "user3", "user4", "user5"];
var Wins = ["1", "1", "1", "0", "0"];
var Lost = ["0", "0", "0", "0", "0"]; //Lost[3] and Lost[4] need to be equal to 1
You can simply use map and a ternary operator to produce the Lost array:
var Users = ["user1", "user2", "user3", "user4", "user5"];
var Wins = ["1", "1", "1", "0", "0"];
var Lost = Wins.map((win) => win === "1" ? "0" : "1");
console.log(Lost);
const lost = wins.map(w => w === "1" ? "0" : "1");

Sort more than 10 numbers in Swift3

I try to sort a String Array with numbers but i dont get the right order.
print(alleTouren) // ["1", "3", "2", "5", "15", "4"]
alleTouren = alleTouren.sorted(by: {$0 < $1})
print(alleTouren) // ["1", "15", "2", "3", "4", "5"]
I also tried alleTouren.sort(by:<) and alleTouren.sort() but i always get back the 15 too early. What i am doing wrong?
Since all strings can be converted to Int add the conversion to the closure.
var alleTouren = ["1", "3", "2", "5", "15", "4"]
alleTouren = alleTouren.sorted(by: { Int($0)! < Int($1)! })
Alternatively use the compare function of String with numeric option which is probably more efficient.
alleTouren = alleTouren.sorted(by: { $0.compare($1, options:.numeric) == .orderedAscending} )
The problem is that you seem to be saying you want to sort them as though they are numbers, but they are strings, so "1", "15", "2"... is correct. You could try converting $0 and $1 to integers and comparing these.
I'm not a Swift expert, but this seems to work:
alleTouren = alleTouren.sorted{let s0 = Int($0)
let s1 = Int($1)
return s0! < s1!}

ruby sorting array - moving matched elements to the beginning

If I have an array: array = ["ruby", "code", "library"]. How can I move matched /^library$/ elements to the beginning. So array will look like this: array = ["library", "ruby", "code"]
it could be done in a number of ways. This is one
array = ["ruby", "code", "library"]
array.partition { |element| element.match /^library$/ }.flatten
Just out of curiosity:
[:select, :reject].map do |m|
["ruby", "code", "library"].public_send(m, &(/^library/.method(:=~)))
end.reduce :|
def move_to_front(arr, pattern)
mi = matching_indices(arr, pattern)
return arr unless mi
a = arr.dup
mi.reverse_each.with_object([]) { |i,b| b.unshift(a.delete_at(i)) }.concat(a)
end
def matching_indices(arr, pattern)
arr.each_index.select do |i|
case pattern
when Regexp then arr[i] =~ pattern
when Proc then pattern[arr[i]]
else (arr[i] == pattern)
end
end
end
move_to_front ["ruby", "code", "library"], /\Alibrary\z/
#=> ["library", "ruby", "code"]
move_to_front ["ruby", "library", "code", "library"], "library"
#=> ["library", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/
#=> ["libraries", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"], /\Alibrar/
#=> ["libraries", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"],
->(str) { str =~ /librar(?:ies|y)/ }
#=> ["libraries", "library", "ruby", "code"]
move_to_front ("1".."9").to_a, /[13579]/
#=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"]
move_to_front ("1".."9").to_a, ->(n) { n.to_i.odd? }
#=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"]
move_to_front ("1".."9").to_a, ->(n) { false }
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
move_to_front ("1".."9").to_a, ->(n) { true }
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
Note:
matching_indices ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/
#=> [1, 3]
The method move_to_front preserves the order of those elements that are moved and those that are not moved.
Three for one cent.
array.inject([]){|a,e| e[/^library/] ? a.unshift(e) : a<<e}
and
array & ["library"] | array
In case array contains the search element multiple times it becomes
array.find_all{ |e| e[/^library/] } + array.reject{ |e| e[/^library/] }
If you hate to use the array variable twice it can also like this
[array].map{|a| a & ["library"] | a}.flatten
The last one: using grep
array.grep(/library/) + array.grep( /^(?!library)/)

Resources