Filtering an array from another array in vue - vue.js

This might be something really simple, but I just can't figure it out. What I'm trying to do is take 2 arrays and filter out what I don't need and only
return the one array.
So what I have right now is this
let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];
and what I would like is to return array 2 with only the items that doesn't show up in array1 so that would be 4, 5,6.
This is what I have so far
return array1.forEach(a => {
array2.filter(aa => aa !== a)
});
and that doesn't return anything

let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];
let array3 = array2.filter(i => !array1.includes(i));
console.log(array3)

This might help to solve your problem.
let array1 = [1, 2, 3]
let array2 = [1, 2, 3, 4, 5, 6]
function returnList(arOne,arTwo){
return arTwo.filter(a => !arOne.includes(a))
}
let response = returnList(array1 ,array2 );

Related

How do you keep the every kth element of a list in Ramda?

How do you filter out every kth element of a list with Ramda?
input = [1, 2, 3, 4, 5, 6, 7, 8, 9]
output = keepKth(input, 3)
output = [1, 4, 7]
This seemed to work:
let k = 3;
let Kth = (value, index) => (index % k == 0)
let filterKth = R.addIndex(R.filter)(Kth);
let input = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let output = filterKth(input);
A variation based on Scott's comment:
const keepEvery = k => compose(pluck(0), splitEvery(k));
keepEvery(3)([1, 2, 3, 4, 5, 6, 7, 8, 9]);
//=> [1, 4, 7]
https://ramdajs.com/docs/#pluck
https://ramdajs.com/docs/#splitEvery
Probably not the cleanest, but point free.
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const whitelist = R.addIndex(R.reject)(R.flip(R.modulo(R.__, 3)));
console.log(
whitelist(data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.js" integrity="sha512-MEPRnhl9ArIiZuk6ikVrLzYxQm8ov1Ngkn4kIUO82hwpD7d+cwXQ7+isupqVgZ6HHtAEBDMff8eUhzixwEBSbA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
And here is another method based on R.unfold. The seed is the index (initial 0), and it's incremented by k on every iteration.
const { curry, unfold } = R
const fn = curry((k, arr) => unfold(n => n < arr.length && [arr[n], n + k], 0))
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const result = fn(3, data)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.2/ramda.js" integrity="sha512-MEPRnhl9ArIiZuk6ikVrLzYxQm8ov1Ngkn4kIUO82hwpD7d+cwXQ7+isupqVgZ6HHtAEBDMff8eUhzixwEBSbA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Mapping set of keys to a matching list of lists

What is an idiomatic way to map keys to a matching list of lists? An example - given:
val s = listOf(1, 9)
val u = listOf(listOf(1, 2, 3), listOf(1, 4, 7), listOf(1, 5, 9))
I would like to have a Map<Int, List<List<Int>>> such that every key in s is mapped to a list of lists containing that key:
{1=[ [1, 2, 3], [1, 4, 7], [1, 5, 9] ], 9=[ [1, 5, 9] ]}
The following:
s.groupBy({ it }, { x -> u.filter { it.contains(x) } })
produces:
{1=[[[1, 2, 3], [1, 4, 7], [1, 5, 9]]], 9=[[[1, 5, 9]]]}
which is not quite right and it isn't clear how to flatten the result to the expected shape.
I would recommend associateWith and use it like this:
s.associateWith { num -> u.filter { list -> num in list } }
Output:
{1=[[1, 2, 3], [1, 4, 7], [1, 5, 9]], 9=[[1, 5, 9]]}
I recommended associate at first, but you can shorten the code even further if you use associateWith. Thanks to Abhay Agarwal who recommended it.
Update
You just need to flatten the values of the result Map.
val w = s.groupBy({ it }, { x -> u.filter { it.contains(x) } })
.mapValues { it.value.flatten() }
My solution map the first collection to pairs from each element to the list where it appears, and then groupBy the result list.
Example
val w = s.map { elem -> Pair(elem, u.filter { list -> elem in list }) }
.groupBy ({ it.first }, { it.second })
.mapValues { it.value.flatten() }
check(w[1] == listOf(listOf(1, 2, 3), listOf(1, 4, 7), listOf(1, 5, 9)))
check(w[9] == listOf(listOf(1, 5, 9)))
println(w)
Output
{1=[[1, 2, 3], [1, 4, 7], [1, 5, 9]], 9=[[1, 5, 9]]}
Idiomatic to me would be s.groupBy(....) The answer by #Omar Mainegra - s.groupBy(...).mapValues( flatten ) absolutely works but it looks like a hack where the initial result needs some extra massaging.
The issue is with the implementation of groupBy and more specifically with groupByTo:
public inline fun <T, K, V, M : MutableMap<in K, MutableList<V>>> Iterable<T>.groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V): M {
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<V>() }
list.add(valueTransform(element))
}
return destination
}
The implementation wraps the values associated with a key in a list because in general multiple values can be associated with a key which is not the case here
where values in s are unique which means that groupBy is the wrong function to use. The right function is associateWith:
s.associateWith { x -> u.filter { it.contains(x) } }
produces:
{1=[[1, 2, 3], [1, 4, 7], [1, 5, 9]], 9=[[1, 5, 9]]}

reactivecocoa confuse about sequence.array

I want to get grouped array from [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] to [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4]].
I've written two types of code. The first one works fine, but the second one doesn't. anyone can tell me why? thanks
RACSignal *signal = #[#1, #2, #3, #4,#2,#3,#3,#4,#4,#4].rac_sequence.signal;
// first
NSArray *arr0 = [[[signal
groupBy:^(NSNumber *object) {
return [NSString stringWithFormat:#"%#",object];
}]
map:^(RACSignal *signal) {
return signal.sequence;
}].sequence
map:^(RACSequence *sequence) {
return sequence.array;
}].array;
// second
NSArray *arr1 = [[signal
groupBy:^(NSNumber *object) {
return [NSString stringWithFormat:#"%#",object];
}]
map:^(RACSignal *signal) {
return signal.sequence.array;
}].sequence.array;

Can help make array with only odd numbers from array in Kotlin

I need help. I trying make array only with odd numbers but I don't want use arraylist because I only want array.
Input array like this: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I am trying to get odd only array like : [1, 3, 5, 7, 9]
val array = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val arraylist = arrayListOf<Int>()
for(i in 0..array.size - 1) {
if(array[i] % 2 != 0)
arraylist.add(array[i])
}
val oddarray = arraylist.toArray()
Why not just use filter:
import java.util.Arrays;
fun main(args: Array<String>) {
val numbersArray = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val oddArray = numbersArray.filter{ it % 2 != 0 }.toTypedArray()
print(Arrays.toString(oddArray)) // [1, 3, 5, 7, 9]
}

Swift: for-in with two values

I started learning C some weeks ago and today I started learning Swift. The code is the following:
import Foundation
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 8, 16, 25],
]
var largest = 0;
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number;
}
}
}
println(largest);
Why do I need kind in the for-in thingy? For "Prime", "Square", ..., right? Can I work with that somehow, too?
“Add another variable to keep track of which kind of number was the largest, as well as what that largest number was.”
How do I build that in?
import Foundation
var largest = 0;
var largestKind: String?;
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 8, 16, 25],
]
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number;
largestKind = kind;
}
}
}
println("The number \(largest) is from the type \(largestKind)");
That's my solution at the moment. However, the output is
The number 25 is from the type Optional("Square")
How do I get rid of the 'Optional("")? I just want the word Square. I tried removing the question mark (var largestKind: String?; to var largestKind: String;) but I get an error doing that.
For those who have the same question, this is another solution I've found. var largestKind is still optional because of String? but the exclamation mark at the end \(largestKind!) makes it possible to access the value without having that optional stuff around the actual content.
import Foundation
var largest = 0;
var largestKind: String?;
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 8, 16, 25],
]
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
largest = number;
largestKind = kind;
}
}
}
println("The number \(largest) is from the type \(largestKind!).");